简体   繁体   English

使用PYXB解析XML。 无法获得嵌套的匿名元素

[英]Parsing XML using PYXB. Can't get to nested anonymous elements

I've created Python bindings using PYXB(pyxbgen) on the following xsd files using the commands: 我使用命令在以下xsd文件上使用PYXB(pyxbgen)创建了Python绑定:

pyxbgen -u Event.xsd -m Event
pyxbgen -u Shape.xsd -m Shape
pyxbgen -u flow-tags.xsd -m flow-tags

Using the generated bindings, I can access all the "event" data but I can't see how to access the "detail" elements and data. 使用生成的绑定,我可以访问所有“事件”数据,但我看不到如何访问“详细”元素和数据。 Specifically, the ellipse and polygon (with vertex data) from the shape element. 具体来说,形状元素的椭圆和多边形(带顶点数据)。 The detail element is an anonymous type. detail元素是匿名类型。 Here is the code I have thus far: 这是我到目前为止的代码:

    import Shape, Event, flow-tags

    xml = '''
    <event how="h-p" opex="e-TEST" qos="7-r-g" stale="2014-04-21T20:50:01.85Z" start="2014-04-21T20:48:01.85Z" time="2014-04-21T20:48:01.85Z" type="a-n-A-M-F-R-Z" uid="6716" version="2.0"><detail><_flow-tags_ debug="2014-04-21T20:48:01.00Z" /><shape><ellipse angle="33" major="44" minor="22" /><polyline closed="1"><vertex hae="1" lat="44" lon="-77" /></polyline></shape></detail><point ce="122.8" hae="817.2" lat="42.5612194" le="431.3" lon="-71.302077" /></event>
    '''

    event = Event.CreateFromDocument(xml)
    print event.uid
    print event.point.lon

    detail = event.detail
    #not sure what goes after this to get content of detail data?????

Here are the Event.xsd and Shape.xsd files I am using. 这是我正在使用的Event.xsd和Shape.xsd文件。 They are not mine so I don't have the option to modify them if there is a problem with them. 他们不是我的,所以如果他们有问题,我没有选择修改它们。

Event.xsd: Event.xsd:

 <?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="event">
        <xs:annotation>
            <xs:documentation>Event Definition</xs:documentation>
        </xs:annotation>
        <xs:complexType>
        <xs:all>
            <xs:element ref="point" />
            <xs:element ref="detail" minOccurs="0" />
        </xs:all>
        <xs:attribute name="version" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:decimal">
                    <xs:minInclusive value="2" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="type" use="required">
            <xs:annotation>
                <xs:documentation>
                    The "type" attribute
                </xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="\w+(-\w+)*(;[^;]*)?" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="access" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation>
                    The access field
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="qos" use="optional">
            <xs:annotation>
                <xs:documentation>
                    format - digit-character-character
                </xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="\d-\w-\w" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="opex" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation>
                    The opex field
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="uid" type="xs:string" use="required">
            <xs:annotation>
                <xs:documentation>
                    The "uid" attribute
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="time" type="xs:dateTime" use="required">
            <xs:annotation>
                <xs:documentation>
                    The XML schema includes three time values:
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="start" type="xs:dateTime" use="required">
            <xs:annotation>
                <xs:documentation>
                    format - DTG
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="stale" type="xs:dateTime" use="required">
            <xs:annotation>
                <xs:documentation>
                    The "stale" attribute
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="how" use="required">
            <xs:annotation>
                <xs:documentation>
                    format = character-character
                </xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="\w(-\w+)*" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>
<xs:element name="detail">
    <xs:annotation>
        <xs:documentation>
            format
        </xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:anyAttribute processContents="skip" />
    </xs:complexType>
</xs:element>
<xs:element name="point">
    <xs:complexType>
        <xs:attribute name="lat" use="required">
            <xs:annotation>
                <xs:documentation>Latitude based on WGS-84 ellipsoid in signed
                    degree-decimal format (e.g. -33.350000). Range -90 -> +90.
                </xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:decimal">
                    <xs:minInclusive value="-90" />
                    <xs:maxInclusive value="90" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="lon" use="required">
            <xs:annotation>
                <xs:documentation>Longitude based on WGS-84 ellipsoid in signed
                    degree-decimal format (e.g. 44.383333). Range -180 -> +180.
                </xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:decimal">
                    <xs:minInclusive value="-180" />
                    <xs:maxInclusive value="180" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="hae" type="xs:decimal" use="required">
            <xs:annotation>
                <xs:documentation>HAE acronym for Height above Ellipsoid based on
                    WGS-84 ellipsoid (measured in meters).
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="ce" type="xs:decimal" use="required">
            <xs:annotation>
                <xs:documentation>
                    Circular Error
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="le" type="xs:decimal" use="required">
            <xs:annotation>
                <xs:documentation>
                    Linear Error
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
</xs:element>

Shape.xsd: Shape.xsd:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3.org/2001/XMLSchema      http://www.w3.org/2001/XMLSchema.xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="nonNegativeDecimal">
    <xs:restriction base="xs:decimal">
        <xs:minInclusive value="0" />
    </xs:restriction>
    <!-- Defined as global type for XML Gallery manifest reference than 
        any other reason -->
</xs:simpleType>
<xs:element name="shape">
    <xs:annotation>
        <xs:documentation></xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="ellipse" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>The "ellipse" is a common shape abstraction used
                        by many geomanipulation applications; it is supported natively.
                    </xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                    <xs:attribute name="major" type="nonNegativeDecimal"
                        use="required">
                        <xs:annotation>
                            <xs:documentation>Ellipse major axis (meters)</xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                    <xs:attribute name="minor" type="nonNegativeDecimal"
                        use="required">
                        <xs:annotation>
                            <xs:documentation>Ellipse minor axis (meters)</xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                    <xs:attribute name="angle" type="xs:decimal" use="required">
                        <xs:annotation>
                            <xs:documentation>Orientation of major axis with respect to true
                                north.
                            </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                    <xs:attribute name="level" type="xs:integer" use="optional">
                        <xs:annotation>
                            <xs:documentation>"level"
                            </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                    <xs:attribute name="extrude" type="xs:nonNegativeInteger"
                        use="optional">
                        <xs:annotation>
                            <xs:documentation>A "Height" of the ellipse used to make the
                                flat object encompas a volume.
                            </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                </xs:complexType>
            </xs:element>
            <xs:element name="polyline" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>The poly line provides a mechanism to express
                        arbitrarily complex two-dimenstional shapes.
                    </xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="vertex" minOccurs="2" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:attribute name="lat" use="required">
                                    <xs:annotation>
                                        <xs:documentation>Latitude based on WGS-84 ellipsoid in
                                            signed degree-decimal format (e.g. -33.350000). Range -90 ->
                                            +90. Positive values denote north.
                                        </xs:documentation>
                                    </xs:annotation>
                                    <xs:simpleType>
                                        <xs:restriction base="xs:decimal">
                                            <xs:minInclusive value="-90" />
                                            <xs:maxInclusive value="90" />
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="lon" use="required">
                                    <xs:annotation>
                                        <xs:documentation>Longitude based on WGS-84 ellipsoid in
                                            signed degree-decimal format (e.g. 44.383333). Range -180 ->
                                            +180. Positive values denote east.
                                        </xs:documentation>
                                    </xs:annotation>
                                    <xs:simpleType>
                                        <xs:restriction base="xs:decimal">
                                            <xs:minInclusive value="-180" />
                                            <xs:maxInclusive value="180" />
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="hae" type="xs:decimal" use="optional">
                                    <xs:annotation>
                                        <xs:documentation>Height Above Ellipsoid (HAE) in Meters.
                                        </xs:documentation>
                                    </xs:annotation>
                                </xs:attribute>
                            </xs:complexType>
                        </xs:element>
                        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                    <xs:attribute name="level" type="xs:integer">
                        <xs:annotation>
                            <xs:documentation>"level"
                            </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                    <xs:attribute name="closed" type="xs:boolean"
                        default="true">
                        <xs:annotation>
                            <xs:documentation>True if the list of verticies should be
                                considered a closed polygon (an implicit line will be added
                                from vertex N to vertex 0).
                            </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                </xs:complexType>
            </xs:element>
            <xs:element name="dxf" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>This is a hook for an arbitrary 3D DXF
                        description of a volume of space.
                    </xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                    <xs:attribute name="level" type="xs:integer">
                        <xs:annotation>
                            <xs:documentation>"level"  </xs:documentation>
                        </xs:annotation>
                    </xs:attribute>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="version" type="xs:decimal" use="optional">
            <xs:annotation>
                <xs:documentation>Version tag for this sub schema. Can be used to
                    ensure upward compatibility with future revisions.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
</xs:element>

flow-tags.xsd: 流tags.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="_flow-tags_">
    <xs:annotation>
        <xs:documentation>This is a detail sub-schema</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:decimal" use="optional" />
        <xs:anyAttribute processContents="lax">
            <xs:annotation>
                <xs:documentation>A system-specific flowtag identifier.
                </xs:documentation>
            </xs:annotation>
        </xs:anyAttribute>
    </xs:complexType>
</xs:element>

Thanks in advance for any help! 在此先感谢您的帮助!

The element children of details that are not recognized get added as wildcards, and can be accessed this way: 未识别的细节元素子项将添加为通配符,并且可以通过以下方式访问:

detail = event.detail
for elt in detail.wildcardElements():
    print elt

However, those elements are both DOM instances (one for _flow-tags_, one for shape). 但是,这些元素都是DOM实例(一个用于_flow-tags_,一个用于形状)。 Normally PyXB will convert elements for any bindings that have been loaded into the application, though they'll still be stored in the wildcardElements() array. 通常,PyXB将转换已加载到应用程序中的任何绑定的元素,尽管它们仍将存储在wildcardElements()数组中。 That's not happening here because the two schemas you're using both have absent namespaces, but are being translated as independent schema. 这不会发生在这里,因为您使用的两个模式都缺少名称空间,但正在被翻译为独立模式。 There is no element _flow-tags_ in either schema, but the shape is not being converted to a binding because the elements from the Shape schema cannot be recognized within the absent namespace used for Event. 在任一模式中都没有元素_flow-tags_,但是形状没有被转换为绑定,因为在用于Event的缺少名称空间内无法识别Shape模式中的元素。

Not knowing where those schema came from I can't tell how they're supposed to be used, but one approach would be to combine them into a parent schema as in: 不知道这些模式来自哪里我不知道它们应该如何被使用,但是一种方法是将它们组合成父模式,如下所示:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Shape.xsd"/>
<xs:include schemaLocation="Event.xsd"/>
</xs:schema>

Then build bindings from this: 然后从这个构建绑定:

pyxbgen -u combine.xsd -m combine

Then you can import those bindings, which have both Event and Shape components, thusly: 然后你可以导入那些具有Event和Shape组件的绑定,因此:

import pyxb
import combine

xml = '''
<event how="h-p" opex="e-TEST" qos="7-r-g" stale="2014-04-21T20:50:01.85Z" start="2014-04-21T20:48:01.85Z" time="2014-04-21T20:48:01.85Z" type="a-n-A-M-F-R-Z" uid="6716" version="2.0"><detail><_flow-tags_ debug="2014-04-21T20:48:01.00Z" /><shape><ellipse angle="33" major="44" minor="22" /><polyline closed="1"><vertex hae="1" lat="44" lon="-77" /></polyline></shape></detail><point ce="122.8" hae="817.2" lat="42.5612194" le="431.3" lon="-71.302077" /></event>
'''

try:
    event = combine.CreateFromDocument(xml)
except pyxb.IncompleteElementContentError as ex:
    print ex.details()

You'll see that the example document is not valid because of the following error: 由于以下错误,您将看到示例文档无效:

The containing element polyline is defined at Shape.xsd[65:12].
The containing element type <class 'combine.CTD_ANON_2'> is defined at Shape.xsd[71:16]
The <class 'combine.CTD_ANON_2'> automaton is not in an accepting state.
Any accepted content has been stored in instance
The following element and wildcard content would be accepted:
        An element vertex per Shape.xsd[73:24]
No content remains unconsumed

If you look at Shape.xsd line 73 you'll see that a polyline requires at least two vertex elements. 如果你看一下Shape.xsd第73行,你会发现折线需要至少两个顶点元素。 If you then correct the document to have a second vertex, then the first member of details.wildcardElements() will still be a DOM Element instance, but the second will be a shape binding. 如果您然后更正文档以具有第二个顶点,那么details.wildcardElements()的第一个成员仍将是DOM元素实例,但第二个将是形状绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM