Thank you Holger for your reply.
I have further refined the schemas for validation - in an attempt to better
organize reusable files.
Now I have this...
entities.dtd
<!ENTITY digit "0-9">
<!ENTITY uword
"([&digit;]{1,4}|[1-5][&digit;]{4}|6[0-4][&digit;]{3}|65[0-4][&digit;]{2}|655[0-2][&digit;]|6553[0-5])">
<!ENTITY Port ":&uword;">
1-base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE schema SYSTEM "entities.dtd">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:paul:types1"
targetNamespace="urn:paul:types1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:simpleType name="PortType">
<xs:restriction base="xs:string">
<xs:pattern value="&Port;"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
1-main.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:paul:main1"
xmlns:t="urn:paul:types1" targetNamespace="urn:paul:main1"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="urn:paul:types1" schemaLocation="1-base.xsd"/>
<xs:element name="Port" type="t:PortType"/>
</xs:schema>
1.xml
<?xml version="1.0" encoding="UTF-8"?>
<Port xmlns="urn:paul:main1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:paul:main1 1-main.xsd">:10100</Port>
With XMLspy, 1.xml validates OK and proper range checks on <Port> occur, but
the following lxml sctipt fails
import lxml
from lxml import etree
with open('1-main.xsd', 'r') as schema_file:
mainschema=etree.parse(schema_file)
my_schema=etree.XMLSchema(mainschema)
with open('1.xml') as file:
my_xml=etree.parse(file)
my_schema.assertValid(my_xml)
the error message given is
Traceback (most recent call last):
File "G:\lxml-test\test1.py", line 19, in <module>
my_schema.assertValid(my_xml)
File "src\lxml\etree.pyx", line 3623, in lxml.etree._Validator.assertValid
lxml.etree.DocumentInvalid: Element '{urn:paul:main1}Port': [facet 'pattern']
The value ':10100' is not accepted by the pattern ''., line 2
so it appears that the &Port; entity is not being read in "<!DOCTYPE schema
SYSTEM "entities.dtd">"
Thanks
Paul
From: [email protected] <[email protected]>
Sent: 24 March 2021 16:47
To: [email protected]
Subject: [lxml] Re: Using DTD ENTITY with lxml
Hi,
I have created the following example files to test this
2-base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE schema [
<!ENTITY lowalpha "a-z">
<!ENTITY hialpha "A-Z">
<!ENTITY alpha "&lowalpha;&hialpha;">
<!ENTITY digit "0-9">
<!ENTITY uword
"([&digit;]{1,4}|[1-5][&digit;]{4}|6[0-4][&digit;]{3}|65[0-4][&digit;]{2}|655[0-2][&digit;]|6553[0-5])">
<!ENTITY Port ":&uword;">
]>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:paul:types2"
targetNamespace="urn:paul:types2" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:simpleType name="PortType">
<xs:restriction base="xs:string">
<xs:pattern value="&Port;"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
2-main.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:paul:main2"
xmlns:t2="urn:paul:types2" targetNamespace="urn:paul:main2"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="urn:paul:types2" schemaLocation="2-base.xsd"/>
<xs:element name="Port" type="t2:PortType"/>
</xs:schema>
2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Port xmlns="urn:paul:main2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:paul:main2 2-main.xsd">:10100</Port>
2.xml validates OK in XMLspy and if the value is changed to :101001 for
example, a match error is given so we know the pattern is progressing through
the xs:import etc
2.xml does not validate when used against lxml
from lxml import etree
with open('2-main.xsd', 'r') as schema_file:
my_schema=etree.XMLSchema(etree.parse(schema_file))
filename='2.xml'
with open(filename) as file:
my_xml=etree.parse(file)
my_schema.assertValid(my_xml)
This gives the following error
Traceback (most recent call last):
File "G:\lxml-test\test1b.py", line 11, in <module>
my_schema.assertValid(my_xml)
File "src\lxml\etree.pyx", line 3623, in lxml.etree._Validator.assertValid
lxml.etree.DocumentInvalid: Element '{urn:paul:main1}Port': [facet 'pattern']
The value ':10100' is not accepted by the pattern '[]+'., line 2
Is this something that 'should' be supported by lxml? If so, am I missing
something in the validation?
Should work and I can't reproduce your problem i.e. your given sample files
work for me just fine
(lxml 4.6.2, libxml2 2, 9, 7, libxslt 1, 1, 29).
But I think you're actually using different data according to this output:
Element '{urn:paul:main1}Port'
Note the differing namespace as opposed to the 2.xml sample.
Maybe some typo / difference in your runs with XMLSpy vs lxml is causing the
headaches?
Sidenote (but you probably know this): No need to open() files yourself,
lxml.etree does
it just fine:
# validate.py
from lxml import etree
schema_tree = etree.parse('2-main.xsd')
my_schema = etree.XMLSchema(schema_tree)
my_xml = etree.parse('2.xml')
result = my_schema.assertValid(my_xml)
Best regards,
Holger
[cid:[email protected]]<https://www.lbbw.de/>
[cid:[email protected]]<https://twitter.com/lbbw>
[cid:[email protected]] <https://www.linkedin.com/company/lbbw>
[cid:[email protected]] <https://www.xing.com/company/lbbw>
[cid:[email protected]] <https://www.facebook.com/LBBW.Stuttgart/>
[cid:[email protected]] <https://www.youtube.com/user/LBBWDirekt>
[cid:[email protected]] <https://www.instagram.com/lbbw_karriere/>
Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart
HRA 4356, HRA 104 440
Amtsgericht Mannheim
HRA 40687
Amtsgericht Mainz
Die LBBW verarbeitet gemaess Erfordernissen der DSGVO Ihre personenbezogenen
Daten.
Informationen finden Sie unter https://www.lbbw.de/datenschutz.
_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: [email protected]