Search code examples
xmlxsdxsd-validationpyxb

Suitable 'Union' member type not correctly recognized when parsing XML


I have been provided with an XSD with the following definition of the type AnySimple (I do not know why this type has been defined, but in any case it is distinct from 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>
        ...

After generating Python code from this XSD with pyxb, when I parse an XML document which I know to be valid, I get the following error which states that an xsd:int cannot be an 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

What has gone wrong?


Edit:

For reference, here is the generated code for the AnySimple type, where the list of members seems to be correct:

# 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

Solution

  • This seems to be related to this issue, found in the documentation of PyXB: http://pyxb.sourceforge.net/userref_usebind.html#coping-with-wrong-xsi-type-attributes

    The error can be solved by adding a line in the code previous to the parsing of the XML file, that relaxes the strictness of the XML interpretation:

    pyxb.namespace.builtin.XMLSchema_instance.ProcessTypeAttribute(
        pyxb.namespace.builtin._XMLSchema_instance.PT_lax)