Search code examples
pythonpyxb

Pyxb wrongly recognises simple type


I am having problem with pyxb once building xml on the base of schema biddings.
I found out that depending on the method of assingning value to some simple ('An atomic simple type') element i get different type assigned.

Here is what i mean i details:
Python 2.7
PyXB version 1.2.5
OS: Windows 7

part of the schema:

<xs:simpleType name="Max140Text_DE_customized">
    <xs:restriction base="Max140Text">
        <xs:minLength value="1"/>
        <xs:maxLength value="140"/>
        <xs:pattern value="[ ]*[A-Za-z0-9+?/:()\.,&apos;\-][A-Za-z0-9+?/:()\.,&apos; \-]*"/>
    </xs:restriction>
</xs:simpleType>  

(...)

<xs:simpleType name="Max140Text">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:maxLength value="140"/>
    </xs:restriction>
</xs:simpleType>

update:
schema element that uses Max140Text_DE_customized:

 <xs:complexType name="PartyIdentification32_CH_pacs008">
    <xs:complexContent>
      <xs:restriction base="PartyIdentification32">
        <xs:sequence>
          <xs:element name="Nm" type="Max140Text_DE_customized" minOccurs="0"/>
          <xs:element name="PstlAdr" type="PostalAddress6_customized" minOccurs="0"/>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>

generated biddings by pyxb:

# Atomic simple type: {http://www.schema-uri/de/MySchema}Max140Text
class Max140Text (pyxb.binding.datatypes.string):

    """An atomic simple type."""

    _ExpandedName = pyxb.namespace.ExpandedName(Namespace, 'Max140Text')
    _XSDLocation = pyxb.utils.utility.Location('C:\\Python27\\Scripts\\MySchema.xsd', 1032, 2)
    _Documentation = None
    Max140Text._CF_minLength = pyxb.binding.facets.CF_minLength(value=pyxb.binding.datatypes.nonNegativeInteger(1))
    Max140Text._CF_maxLength = pyxb.binding.facets.CF_maxLength(value=pyxb.binding.datatypes.nonNegativeInteger(140))
    Max140Text._InitializeFacetMap(Max140Text._CF_minLength,
    Max140Text._CF_maxLength)
    Namespace.addCategoryObject('typeBinding', 'Max140Text', Max140Text)
    _module_typeBindings.Max140Text = Max140Text      


# Atomic simple type: {http://www.schema-uri/de/MySchema}Max140Text_DE_customized
class Max140Text_DE_customized (Max140Text):

    """An atomic simple type."""

    _ExpandedName = pyxb.namespace.ExpandedName(Namespace, 'Max140Text_DE_customized')
    _XSDLocation = pyxb.utils.utility.Location('C:\\Python27\\Scripts\\MySchema.xsd', 1038, 2)
    _Documentation = None
    Max140Text_DE_customized._CF_minLength = pyxb.binding.facets.CF_minLength(value=pyxb.binding.datatypes.nonNegativeInteger(1))
    Max140Text_DE_customized._CF_pattern = pyxb.binding.facets.CF_pattern()
    Max140Text_DE_customized._CF_pattern.addPattern(pattern="[ ]*[A-Za-z0-9+?/:()\\.,'\\-][A-Za-z0-9+?/:()\\.,' \\-]*")
    Max140Text_DE_customized._CF_maxLength = pyxb.binding.facets.CF_maxLength(value=pyxb.binding.datatypes.nonNegativeInteger(140))
    Max140Text_DE_customized._InitializeFacetMap(Max140Text_DE_customized._CF_minLength, 
    Max140Text_DE_customized._CF_pattern, Max140Text_DE_customized._CF_maxLength)
    Namespace.addCategoryObject('typeBinding', 'Max140Text_DE_customized', Max140Text_DE_customized)
    _module_typeBindings.Max140Text_DE_customized = Max140Text_DE_customized

When I assign value to a complex element that is Max140Text_DE_customized type (by schema) just passing a string, the type of this element is recognized incorrectly.
This does not performs pattern validation once building document (due to wrong type).

my_elem = myschema_biddings()        # some complex element
my_elem.Nm = "Foo"                   # no pattern validation !
type(my_elem.Nm) = <class'myschema_bidddings.Max140Text'>  # wrong
my_elem.Nm = myschema_biddings.Max140Text_DE_customized("Bar")
type(my_elem.Nm) = <class'myschema_bidddings.Max140Text_DE_customized'>   # correct 

Solution

  • This behavior is due to a bug in PyXB which does not correctly detect that you've overridden the base class element declaration for Nm.