2

タグに属性のセットまたは別のセットが必要な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)。

これも可能ですか?もしそうなら、正しい構文は何ですか?

4

1 に答える 1

5

残念ながら、XML スキーマの属性で選択を使用することはできません。この検証をより高いレベルで実装する必要があります。

于 2010-05-10T22:43:17.997 に答える