I need to parse a file .xsd in Python as i would parse an XML.
I am using libxml2.
I have to parse an xsd that look as follow:
<xs:complexType name="ClassType">
<xs:sequence>
<xs:element name="IeplcHeader">
<xs:complexType>
<xs:sequence>
<xs:element name="device-number" type="xs:integer" fixed="1"/>
</xs:sequence>
<xs:attribute name="version" type="xs:integer" use="required" fixed="0"/>
</xs:complexType>
</xs:element>
when i access with
doc.xpathEval('//xs:complexType/xs:sequence/xs:element[@name="IeplcHeader"]'):
tells me that cannot find the path.
while if i remove all the xs: as follow
<complexType name="ClassType">
<sequence>
<element name="IeplcHeader">
<complexType>
<sequence>
<element name="device-number" type="xs:integer" fixed="1"/>
</sequence>
<attribute name="version" type="xs:integer" use="required" fixed="0"/>
</complexType>
</element>
in this way it works
doc.xpathEval('//complexType/sequence/element[@name="IeplcHeader"]'):
Does anyone knows how can i get read of this problem fixing a prefix? righ now i am preparsing the file removing the xs: but it's an orrible solution and i really hope to be able to find a better solution.
(I did not try with py-dom-xpath yet and i do not know if may work even with the xs:)
thanks, ste
If you have to deal with xsd files, maybe also using them to validate xml files I suggest you to pass to lxml that has a good support for XMLSchema files.
example code:
from lxml import etree
from cStringIO import StringIO
f = StringIO()
f = StringIO('''\
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="a" type="AType"/>
<xsd:complexType name="AType">
<xsd:sequence>
<xsd:element name="b" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
''')
xmlschema_doc = etree.parse(f)
xmlschema_doc.xpath('xsd:element',
namespaces={"xsd": "http://www.w3.org/2001/XMLSchema"})
results in:
[<Element {http://www.w3.org/2001/XMLSchema}element at 0x9a17f2c>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With