parsing .xsd in python

半世苍凉 提交于 2021-02-07 07:14:39

问题


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:


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


来源:https://stackoverflow.com/questions/6775521/parsing-xsd-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!