0

I have something along the lines of schema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/" xml:lang="en">
  <xsd:element name="Element" fixed="ElementValue">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute name="attribute" type="xsd:string" use="required" fixed="AttributeValue"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

and the corresponding document.xml:

<?xml version="1.0" ?>
<example:Element xmlns:example="http://www.example.com/" attribute="AttributeValue">ElementValue</example:Element>

Is this valid? I could not find anything in specs that would forbid such declaration, and xmllint validates it fine:

$ xmllint --schema schema.xsd document.xml --noout
document.xml validates
$ xmllint --schema http://www.w3.org/2001/XMLSchema.xsd schema.xsd --noout
schema.xsd validates

I did however see this question which seems to suggest that it is not correct, and the library I use (PyXB) doesn't seem to handle that:

>>> import example
>>> xml = open('document.xml').read()
>>> data = example.CreateFromDocument(xml)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 65, in CreateFromDocument
    saxer.parse(io.BytesIO(xmld))
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 110, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
    self.feed(buffer)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 213, in feed
    self._parser.Parse(data, isFinal)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 365, in end_element_ns
    self._cont_handler.endElementNS(pair, None)
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 388, in endElementNS
    binding_object = this_state.endBindingElement()
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 226, in endBindingElement
    self.__constructElement(self.__delayedConstructor, self.__attributes, args)
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 116, in __constructElement
    self.__bindingInstance = new_object_factory(*content, **kw)
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 1631, in __call__
    args = [ self.compatibleValue(args[0], **kw) ]
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 1654, in compatibleValue
    raise pyxb.ElementChangeError(self, value)
pyxb.exceptions_.ElementChangeError: Value ElementValue for element {http://www.example.com/}Element incompatible with fixed content

I would appreciate someone pointing me to the specs that explain why it's not correct, as I have to deal with a third-party XSD that uses that construct.

Maciek Wawro
  • 844
  • 6
  • 7
  • 1
    Both your schema and XML are correct. I suggest you, first, to check if you feed to PyXB exactly what you think you do. I mean, you may be running it with some settings pointing to a different version of that schema, in which that `fixed` constraint specified differently (such things sometimes happens even with me). If that doesn't help, then, one has to admit, it is a bug of this particular PyXB version. Bugs do happen (in anything). – ColdFusion Feb 13 '19 at 22:24
  • Thanks. I am getting the above error right after invoking `pyxbgen --schema-location=schema.xsd -m example` in the same directory, so I'm pretty certain it's not user error. I thought it could be a bug, but then I got confused by the answer in the question I linked to. I'll submit the issue for PyXB. – Maciek Wawro Feb 13 '19 at 22:37

0 Answers0