xsi:nil for nillable element of enum type raises SimpleFacetValueError
Brought to you by:
pabigot
Reading xml instance data raises SimpleFacetValueError for an element of an Enum type that is xsi:nil in the instance, even though it is nillable in the XML Schema (and thus valid as it is).
Test schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="TestEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="foo"/>
<xs:enumeration value="bar"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="root" type="TestEnum" nillable="true"/>
</xs:schema>
Sample instance:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:test_enum.xsd" xsi:nil="true"/>
Code:
$ PYTHONPATH=build/lib python2.7 build/scripts-2.7/pyxbgen -u test_enum.xsd -m test_enum
$ PYTHONPATH=build/lib python2.7 -i -c 'from test_enum import *; f = open("test_enum2.xml"); instance = CreateFromDocument(f.read())'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "test_enum.py", line 48, in CreateFromDocument
saxer.parse(StringIO.StringIO(xml_text))
File "/apps/prod/gcc/4.6.1/lib/python2.7/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/apps/prod/gcc/4.6.1/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
self.feed(buffer)
File "/apps/prod/gcc/4.6.1/lib/python2.7/xml/sax/expatreader.py", line 207, in feed
self._parser.Parse(data, isFinal)
File "/apps/prod/gcc/4.6.1/lib/python2.7/xml/sax/expatreader.py", line 349, in end_element_ns
self._cont_handler.endElementNS(pair, None)
File "pyxb/binding/saxer.py", line 377, in endElementNS
binding_object = this_state.endBindingElement()
File "pyxb/binding/saxer.py", line 222, in endBindingElement
self.__constructElement(self.__delayedConstructor, self.__attributes, args)
File "pyxb/binding/saxer.py", line 116, in __constructElement
self.__bindingInstance = new_object_factory(*content, **kw)
File "pyxb/binding/basis.py", line 1587, in __call__
rv = self.typeDefinition().Factory(*args, **kw)
File "pyxb/binding/basis.py", line 307, in Factory
rv = cls._DynamicCreate(*args, **kw)
File "pyxb/binding/basis.py", line 680, in _DynamicCreate
return ctor(*args, **kw)
File "pyxb/binding/basis.py", line 921, in __init__
self.xsdConstraintsOK(location)
File "pyxb/binding/basis.py", line 1048, in xsdConstraintsOK
return self.XsdConstraintsOK(self, location)
File "pyxb/binding/basis.py", line 1043, in XsdConstraintsOK
raise pyxb.SimpleFacetValueError(cls, value, f, location)
pyxb.exceptions_.SimpleFacetValueError: Type TestEnum enumeration constraint violated by value
>>>
Regards,
Holger
Thanks for the test case. Fixed in next branch.