我有一些类似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>
和相应的document.xml
:
<?xml version="1.0" ?>
<example:Element xmlns:example="http://www.example.com/" attribute="AttributeValue">ElementValue</example:Element>
这有效吗?我在规范中找不到任何禁止此类声明的内容,xmllint
对其进行了验证:
$ 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
但是我确实看到了this question,这似乎表明它是不正确的,而且我使用的库(PyXB)似乎无法处理该问题:
>>> 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
我会很高兴有人指出我的规范来解释为什么它不正确,因为我必须处理使用该构造的第三方XSD。