tags:

views:

43

answers:

2

I have an XML Schema that looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" minOccurs="0" maxOccurs="1"/>
                <xs:element name="B" minOccurs="0" maxOccurs="1"/>
                <xs:element name="C" minOccurs="0" maxOccurs="32"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The following is valid according to this schema:

<root xsi:noNamespaceSchemaLocation="MySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
</root>

However, I'd like to make the above XML invalid.

More specifically, I'd like to require: 1. that the <root> have at least one child element, be it an <A>, a <B>, or a <C>, and 2. that the <root> have at most one <A> child, and at most one <B> child.

Suggestions?


Solution is:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xs:element name="root">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element name="A"/>
                    <xs:element name="B" minOccurs="0"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="B"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="C" minOccurs="1" maxOccurs="32"/>
                </xs:sequence>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>
A: 

Have you tried adding minOccurrs=1 to the xs:sequence?

Jim Garrison
A: 

How about using exactly one xs:choice of A, B, or C followed by 0 or more of each of them?

Ah, with your edit it would have to be something like a choice of A, AB, or B followed by 0 to 32 Cs. Yes?

Kate Gregory
Your suggestions led me to the answer, Kate. Thank you!
JaysonFix