代码之家  ›  专栏  ›  技术社区  ›  Silverspur

分析XML时无法正确识别合适的“Union”成员类型

  •  0
  • Silverspur  · 技术社区  · 6 年前

    已经向我提供了一个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 不能是 任何简单的 :

    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
    

    出什么事了?


    编辑:

    作为参考,下面是为 任何简单的 类型,其中成员列表似乎正确:

    # 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
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Silverspur    6 年前

    这似乎与此问题有关,可在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)