使用生成的和外部引用解析 xml
xml 有效,当我尝试使用生成的类解析 xml 时,出现错误。
因此,我正在为具有外部 xmlns 的 xsd 生成类。我以这种方式生成类:
python generateDS.py -o cc.py --external-encoding='utf-8' --export="write etree" cc.xsd
我生成一个简约的 xml 来验证 xsd,并且 xml 验证架构(我使用 lxml 验证它),
因此当我尝试解析 xml 时会发生错误。并且仅当我包含外部参考元素时。它给了我下一个错误:
类型错误:预期的字符串或类似字节的对象
我尝试在创建类时使用 --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>
The xml is VALID, the error I get is when I try to parse the xml with the classes generated with Generateds.
So, I´m generating classes for an xsd that has external xmlns. I generate the classes this way:
python generateDS.py -o cc.py --external-encoding='utf-8' --export="write etree" cc.xsd
I generate an minimalistic xml to valid the xsd and the xml valids the schema ( I validate it with lxml)
So the error occurs when I try to parse the xml. And ONLY if I include an element of the external reference. It gives me the next error:
TypeError: expected string or bytes-like object
I tried using --namespacedef= in the creation of the classes but the error is the same.
The external 'types' needed to parse the xml are generated in the .py.
I read that 'one per' may be a solution but I don´t quite get it.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论