6

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

1 Answer 1

9

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>]
Sign up to request clarification or add additional context in comments.

2 Comments

nice library. but i am not sure it does what i need. Is very nice for validation...but in this moment i Just need to access some data inside the XSD... it sound strange but i am writing a script that has TO READ some data from an xsd...;) I ma not using it for validation at this point.. :)
@Stefano: lxm is not good for validating only but also using xpath. Posting a complete (stripped down) xsd file to make tests onto would help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.