タグに属性のセットまたは別のセットが必要なXMLドキュメントを使用しています。たとえば、次のように見える必要があり<tag foo="hello" bar="kitty" />
ます<tag spam="goodbye" eggs="world" />
。
<root>
<tag foo="hello" bar="kitty" />
<tag spam="goodbye" eggs="world" />
</root>
したがって、xs:choice
要素を使用して2つの異なる属性グループから選択するXSDスキーマがあります。
<xsi:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="tag">
<xs:choice>
<xs:complexType>
<xs:attribute name="foo" type="xs:string" use="required" />
<xs:attribute name="bar" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType>
<xs:attribute name="spam" type="xs:string" use="required" />
<xs:attribute name="eggs" type="xs:string" use="required" />
</xs:complexType>
</xs:choice>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsi:schema>
ただし、lxmlを使用してこのスキーマを読み込もうとすると、次のエラーが発生します。
>>> from lxml import etree
>>> etree.XMLSchema( etree.parse("schema_choice.xsd") )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "xmlschema.pxi", line 85, in lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:118685)
lxml.etree.XMLSchemaParseError: Element '{http://www.w3.org/2001/XMLSchema}element': The content is not valid. Expected is (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))., line 7
エラーはxs:choice
要素の配置にあるため、別の場所に配置しようとしましたが、何を試しても、1セットの属性(foo
およびbar
)を持つタグを定義するために使用できないようです。または別の(spam
およびeggs
)。
これも可能ですか?もしそうなら、正しい構文は何ですか?