解析 XML 时无法正确识别合适的 'Union' 成员类型
Suitable 'Union' member type not correctly recognized when parsing XML
我得到了一个 XSD 类型的定义如下 AnySimple
(我不知道为什么定义了这个类型,但无论如何它都不同于 xsd:anySimpleType
):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:Core="http://www.foo.com/Core/PrimitiveTypes" targetNamespace="http://www.foo.com/Core/PrimitiveTypes" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:simpleType name="AnySimple">
<xsd:annotation>
<xsd:documentation>container for any simple type</xsd:documentation>
</xsd:annotation>
<xsd:union memberTypes="xsd:string xsd:boolean xsd:byte xsd:short xsd:int xsd:long xsd:unsignedByte xsd:unsignedShort xsd:unsignedInt xsd:unsignedLong xsd:float xsd:double xsd:dateTime xsd:duration"/>
</xsd:simpleType>
...
使用 pyxb 从这个 XSD 生成 Python 代码后,当我解析一个我知道有效的 XML 文档时, 我得到以下错误其中指出 xsd:int
不能是 AnySimple
:
File "/usr/lib/python3/dist-packages/pyxb/namespace/builtin.py", line 133, in _InterpretTypeAttribute
raise pyxb.BadDocumentError('%s value %s is not subclass of element type %s' % (type_name, type_en, type_class._ExpandedName))
pyxb.exceptions_.BadDocumentError: xsd:int value {http://www.w3.org/2001/XMLSchema}int is not subclass of element type {http://www.foo.com/Core/PrimitiveTypes}AnySimple
出了什么问题?
编辑:
作为参考,这里是 AnySimple
类型的生成代码,其中成员列表似乎是正确的:
# Union simple type: {http://www.foo.com/Core/PrimitiveTypes}AnySimple
# superclasses pyxb.binding.datatypes.anySimpleType
class AnySimple (pyxb.binding.basis.STD_union):
"""container for any simple type"""
_ExpandedName = pyxb.namespace.ExpandedName(Namespace, 'AnySimple')
_XSDLocation = pyxb.utils.utility.Location('/home/user/myproject/xsd/Core/PrimitiveTypes.xsd', 36, 3)
_Documentation = 'container for any simple type'
_MemberTypes = ( pyxb.binding.datatypes.string, pyxb.binding.datatypes.boolean, pyxb.binding.datatypes.byte, pyxb.binding.datatypes.short, pyxb.binding.datatypes.int, pyxb.binding.datatypes.long, pyxb.binding.datatypes.unsignedByte, pyxb.binding.datatypes.unsignedShort, pyxb.binding.datatypes.unsignedInt, pyxb.binding.datatypes.unsignedLong, pyxb.binding.datatypes.float, pyxb.binding.datatypes.double, pyxb.binding.datatypes.dateTime, pyxb.binding.datatypes.duration, )
AnySimple._CF_pattern = pyxb.binding.facets.CF_pattern()
AnySimple._CF_enumeration = pyxb.binding.facets.CF_enumeration(value_datatype=AnySimple)
AnySimple._InitializeFacetMap(AnySimple._CF_pattern,
AnySimple._CF_enumeration)
Namespace.addCategoryObject('typeBinding', 'AnySimple', AnySimple)
_module_typeBindings.AnySimple = AnySimple
这似乎与这个问题有关,在 PyXB 的文档中找到:http://pyxb.sourceforge.net/userref_usebind.html#coping-with-wrong-xsi-type-attributes
可以通过在解析 XML 文件之前的代码中添加一行来解决错误,这放宽了 XML 解释的严格性:
pyxb.namespace.builtin.XMLSchema_instance.ProcessTypeAttribute(
pyxb.namespace.builtin._XMLSchema_instance.PT_lax)
我得到了一个 XSD 类型的定义如下 AnySimple
(我不知道为什么定义了这个类型,但无论如何它都不同于 xsd:anySimpleType
):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:Core="http://www.foo.com/Core/PrimitiveTypes" targetNamespace="http://www.foo.com/Core/PrimitiveTypes" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:simpleType name="AnySimple">
<xsd:annotation>
<xsd:documentation>container for any simple type</xsd:documentation>
</xsd:annotation>
<xsd:union memberTypes="xsd:string xsd:boolean xsd:byte xsd:short xsd:int xsd:long xsd:unsignedByte xsd:unsignedShort xsd:unsignedInt xsd:unsignedLong xsd:float xsd:double xsd:dateTime xsd:duration"/>
</xsd:simpleType>
...
使用 pyxb 从这个 XSD 生成 Python 代码后,当我解析一个我知道有效的 XML 文档时, 我得到以下错误其中指出 xsd:int
不能是 AnySimple
:
File "/usr/lib/python3/dist-packages/pyxb/namespace/builtin.py", line 133, in _InterpretTypeAttribute
raise pyxb.BadDocumentError('%s value %s is not subclass of element type %s' % (type_name, type_en, type_class._ExpandedName))
pyxb.exceptions_.BadDocumentError: xsd:int value {http://www.w3.org/2001/XMLSchema}int is not subclass of element type {http://www.foo.com/Core/PrimitiveTypes}AnySimple
出了什么问题?
编辑:
作为参考,这里是 AnySimple
类型的生成代码,其中成员列表似乎是正确的:
# Union simple type: {http://www.foo.com/Core/PrimitiveTypes}AnySimple
# superclasses pyxb.binding.datatypes.anySimpleType
class AnySimple (pyxb.binding.basis.STD_union):
"""container for any simple type"""
_ExpandedName = pyxb.namespace.ExpandedName(Namespace, 'AnySimple')
_XSDLocation = pyxb.utils.utility.Location('/home/user/myproject/xsd/Core/PrimitiveTypes.xsd', 36, 3)
_Documentation = 'container for any simple type'
_MemberTypes = ( pyxb.binding.datatypes.string, pyxb.binding.datatypes.boolean, pyxb.binding.datatypes.byte, pyxb.binding.datatypes.short, pyxb.binding.datatypes.int, pyxb.binding.datatypes.long, pyxb.binding.datatypes.unsignedByte, pyxb.binding.datatypes.unsignedShort, pyxb.binding.datatypes.unsignedInt, pyxb.binding.datatypes.unsignedLong, pyxb.binding.datatypes.float, pyxb.binding.datatypes.double, pyxb.binding.datatypes.dateTime, pyxb.binding.datatypes.duration, )
AnySimple._CF_pattern = pyxb.binding.facets.CF_pattern()
AnySimple._CF_enumeration = pyxb.binding.facets.CF_enumeration(value_datatype=AnySimple)
AnySimple._InitializeFacetMap(AnySimple._CF_pattern,
AnySimple._CF_enumeration)
Namespace.addCategoryObject('typeBinding', 'AnySimple', AnySimple)
_module_typeBindings.AnySimple = AnySimple
这似乎与这个问题有关,在 PyXB 的文档中找到:http://pyxb.sourceforge.net/userref_usebind.html#coping-with-wrong-xsi-type-attributes
可以通过在解析 XML 文件之前的代码中添加一行来解决错误,这放宽了 XML 解释的严格性:
pyxb.namespace.builtin.XMLSchema_instance.ProcessTypeAttribute(
pyxb.namespace.builtin._XMLSchema_instance.PT_lax)