How do I require an element to have either one set of attributes or a different one in the XSD schema?

I am working with an XML document where a tag must have either one set of attributes or another. For example, it should look like <tag foo="hello" bar="kitty" />

or <tag spam="goodbye" eggs="world" />

eg.

<root>
    <tag foo="hello" bar="kitty" />
    <tag spam="goodbye" eggs="world" />
</root>

      

So, I have an XSD schema where I use an element xs:choice

to choose between two different attribute groups:

<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>

      

However, when using lxml to load this schema, I get the following error:

>>> 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

      

Since the error is associated with the placement of my element xs:choice

, I tried to put it in different places, but no matter what I try, I can not use it to determine the tag to have a set of attributes ( foo

and bar

) or the other ( spam

and eggs

).

Is it possible? And if so, what is the correct syntax?

+2


a source to share


1 answer


Unfortunately, it is not possible to use selection with attributes in XML Schema. You will need to perform this check at a higher level.



+4


a source







All Articles