Reputation: 3956
I create xml manually and then try to validate it with xsd scheme. Validation doesn't pass at first, but if I convert xml to a string and back - then new xml passes validation.
from lxml import etree
xsd = etree.fromstring("""
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="some_namespace">
<element name="el"></element>
</schema>""")
schema = etree.XMLSchema(xsd)
xml1 = etree.Element('el', nsmap={None: "some_namespace"})
xml2 = etree.fromstring(etree.tostring(xml1))
schema.assertValid(xml2) # this passes
schema.assertValid(xml1) # this fails
I see that xml1 and xml2 have different tags:
print xml1.tag # --> el
print xml2.tag # --> {some_namespace}el
But why xml1 and xml2 have such a difference? Looks like they should be the same.
Upvotes: 4
Views: 287
Reputation: 50947
Here you create an el
element (no namespace):
xml1 = etree.Element('el', nsmap={None: "some_namespace"})
Using a nsmap
parameter does not bind the element to a namespace; it just provides a mapping for serialization.
When etree.tostring(xml1)
is executed, the serialization behaviour "kicks in". When the serialized result has been parsed, xml2
is an {some_namespace}el
element instead of el
.
To make it work, change the line to:
xml1 = etree.Element('{some_namespace}el', nsmap={None: "some_namespace"})
Upvotes: 4