XML Schema: disjunctive attributes for element possible ?

Discussion in 'XML' started by Gehirnloser Zombie, Jan 12, 2004.

  1. Hello folks,

    Short question: Is it possible to express with XML-Schema that for an
    element some attributes may apear only disjunctivly (i.e. an element
    <E> can either carry the attribute "a1" OR "a2", but never both of
    them)?

    Thanks in advance,

    Puck.
     
    Gehirnloser Zombie, Jan 12, 2004
    #1
    1. Advertising

  2. Gehirnloser Zombie wrote:

    > Short question: Is it possible to express with XML-Schema that for an
    > element some attributes may apear only disjunctivly (i.e. an element
    > <E> can either carry the attribute "a1" OR "a2", but never both of
    > them)?


    I know one way to do that, by defining a base type and deriving two
    types from and then use xsi:type on the instance data:

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test20040112Xsd.xml">
    <element xsi:type="elType1" a1="value" />
    <element xsi:type="elType2" a2="value" />
    </root>


    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="root">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="element" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="element" type="elType" />

    <xs:complexType name="elType" />

    <xs:complexType name="elType1">
    <xs:complexContent>
    <xs:extension base="elType">
    <xs:attribute name="a1" type="xs:string" />
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="elType2">
    <xs:complexContent>
    <xs:extension base="elType">
    <xs:attribute name="a2" type="xs:string" />
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>

    </xs:schema>


    I don't think you can do it without xsi:type.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 12, 2004
    #2
    1. Advertising

  3. Thanks, although the need to specify the "xsi:type" in the XML is not
    an acceptable solution for me.

    Puck.
     
    Gehirnloser Zombie, Jan 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page