0

xml 是有效的,我得到的错误是当我尝试使用 Generateds 生成的类解析 xml 时。

因此,我正在为具有外部 xmlns 的 xsd 生成类。我以这种方式生成类:

python generateDS.py -o cc.py --external-encoding='utf-8' --export="write etree" cc.xsd

我生成了一个简约的 xml 来验证 xsd 并且 xml 验证了架构(我使用 lxml 验证它)

因此,当我尝试解析 xml 时会发生错误。并且仅当我包含外部参考的元素时。它给了我下一个错误:

TypeError:预期的字符串或类似字节的对象

我尝试在创建类时使用 --namespacedef= 但错误是一样的。

解析 xml 所需的外部“类型”在 .py 中生成。

我读到“一个人”可能是一个解决方案,但我不太明白。

xml:

<?xml version='1.0' encoding='UTF-8'?>
<dcc:digitalCalibrationCertificate xmlns:dcc="https://ptb.de/dcc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:si="https://ptb.de/si" xmlns:ext="extension" schemaVersion="3.0.0" xsi:schemaLocation="https://ptb.de/dcc https://ptb.de/dcc/v3.0.0/dcc.xsd">
    <dcc:result>        
        <dcc:data>
            <dcc:quantity>
                <si:real>
                    <si:value>150.16</si:value>
                    <si:unit>\degreeCelsius</si:unit>
                </si:real>
            </dcc:quantity>
        </dcc:data>
    </dcc:result>
</dcc:digitalCalibrationCertificate>

xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="3.0.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:dcc="https://ptb.de/dcc"
           xmlns:si="https://ptb.de/si"
           targetNamespace="https://ptb.de/dcc"
           elementFormDefault="qualified">

    <xs:import
            namespace="https://ptb.de/si"
            schemaLocation="https://ptb.de/si/v2.0.0/SI_Format.xsd"/>

    <xs:element name="digitalCalibrationCertificate" type="dcc:digitalCalibrationCertificateType"/>

    <xs:complexType name="digitalCalibrationCertificateType">
        <xs:sequence>
            <xs:element name="result" type="dcc:resultType"/>
        </xs:sequence>

        <xs:attribute name="schemaVersion" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="3\.0\.0"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="resultType">
        <xs:annotation>
            <xs:documentation>
                The actual result of the calibration.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="data" type="dcc:dataType"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="optional"/>
        <xs:attribute name="refId" type="xs:IDREF" use="optional"/>
        <xs:attribute name="refType" type="xs:string" use="optional"/>
    </xs:complexType>

    <xs:complexType name="dataType">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="quantity" type="dcc:quantityType"/>
        </xs:choice>
        <xs:attribute name="id" type="xs:ID" use="optional"/>
        <xs:attribute name="refId" type="xs:IDREF" use="optional"/>
        <xs:attribute name="refType" type="xs:string" use="optional"/>
    </xs:complexType>

    <xs:complexType name="quantityType">
        <xs:sequence>
            <xs:choice>
                <xs:element ref="si:real"/>
                <xs:element ref="si:list"/>
                <xs:element ref="si:hybrid"/>
                <xs:element ref="si:complex"/>
                <xs:element ref="si:constant"/>
            </xs:choice>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="optional"/>
        <xs:attribute name="refId" type="xs:IDREF" use="optional"/>
        <xs:attribute name="refType" type="xs:string" use="optional"/>
    </xs:complexType>
</xs:schema>
4

0 回答 0