Parsing .xsd in python

I need to parse the .xsd file in Python as I will parse the XML.
I am using libxml2.
I need to parse xsd, which looks like this:

<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 using

doc.xpathEval('//xs:complexType/xs:sequence/xs:element[@name="IeplcHeader"]'):

tells me that he cannot find the way.

and if I delete all xs: as it should

<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>

that way it works

doc.xpathEval('//complexType/sequence/element[@name="IeplcHeader"]'):

Does anyone know how I can read this problem with prefix fix? righ now I am preparing a file that removes xs: but this is an orrible solution, and I really hope to find a better solution.

(I haven't tried with py-dom-xpath yet, and I don't know if it can even work with xs :)

thanks Ste

+5
source share
1 answer

xsd, , XML , lxml, XMLSchema.

:

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"})

:

[<Element {http://www.w3.org/2001/XMLSchema}element at 0x9a17f2c>]
+8

All Articles