2016-11-18 12:50:30 |
Andrew Pashkin |
description |
Here is a simple schema, which declares an element `root` with a single boolean attribute, named `foo`:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:attribute name="foo" type="xs:boolean" default="false"/>
</xs:complexType>
</xs:element>
</xs:schema>
Here is the XML, that complies to the schema:
<?xml version="1.0"?>
<root
foo="true"
/>
And here is the Python code, that parses the XML and validates it against the schema:
import lxml
import lxml.etree
schema_parser = lxml.etree.XMLParser(load_dtd=True)
schema_doc = lxml.etree.parse(open('test.xsd'), parser=schema_parser)
schema = lxml.etree.XMLSchema(schema_doc)
parser = lxml.etree.XMLParser(
load_dtd=True,
dtd_validation=False,
attribute_defaults=True,
schema=schema
)
settings = lxml.etree.parse(open('test.xml'), parser=parser)
It produces a strange exception: `XMLSyntaxError: Element 'root', attribute 'foo': '' is not a valid value of the atomic type 'xs:boolean'.`.
Notice `'foo': ''` part - it means, that LXML think, that the attribute is empty, for some reason.
Why it happens?
p.s.
I use LXML 3.6.4
p.p.s.
Versions info:
Python : sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
lxml.etree : (3, 6, 4, 0)
libxml used : (2, 9, 4)
libxml compiled : (2, 9, 4)
libxslt used : (1, 1, 29)
libxslt compiled : (1, 1, 29) |
Here is a simple schema, which declares an element `root` with a single boolean attribute, named `foo`:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:attribute name="foo" type="xs:boolean" default="false"/>
</xs:complexType>
</xs:element>
</xs:schema>
Here is the XML, that complies to the schema:
<?xml version="1.0"?>
<root
foo="true"
/>
And here is the Python code, that parses the XML and validates it against the schema:
import lxml
import lxml.etree
schema_parser = lxml.etree.XMLParser(load_dtd=True)
schema_doc = lxml.etree.parse(open('test.xsd'), parser=schema_parser)
schema = lxml.etree.XMLSchema(schema_doc)
parser = lxml.etree.XMLParser(
load_dtd=True,
dtd_validation=False,
attribute_defaults=True,
schema=schema
)
settings = lxml.etree.parse(open('test.xml'), parser=parser)
It produces a strange exception: `XMLSyntaxError: Element 'root', attribute 'foo': '' is not a valid value of the atomic type 'xs:boolean'.`.
Notice `'foo': ''` part - it means, that LXML think, that the attribute is empty, for some reason.
p.s.
Versions info:
Python : sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
lxml.etree : (3, 6, 4, 0)
libxml used : (2, 9, 4)
libxml compiled : (2, 9, 4)
libxslt used : (1, 1, 29)
libxslt compiled : (1, 1, 29) |
|