0

How do I define schema for the following XML:

<root>
    <elements>
        <element1/>
        <element2/>
        <element1/>
    </elements>
    <elements>
        <element2/>
        <element1/>
    </elements>
    <elements>
        <element1/>
        <element2/>
    </elements>
</root>

where the number of elements element1 and element2 is unlimited and they can be intermixed in a varying order. Is it at all possible? Or should I just redefine XML as:

<root>
    <elements>
        <element type="1"/>
        <element type="2"/>
        <element type="1"/>
    </elements>
    <elements>
        <element type="2"/>
        <element type="1"/>
    </elements>
    <elements>
        <element type="1"/>
        <element type="2"/>
    </elements>
</root>
1
  • 1
    Just a side note, none of the codes you posted is a valid XML, the closing tags have to have /. Commented Jul 8, 2011 at 19:19

2 Answers 2

1

Schema of your first structure

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="elements">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:choice maxOccurs="unbounded">
                                <xs:element name="element1" />
                                <xs:element name="element2" />
                            </xs:choice>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Schema definition of your second structure

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="elements">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element maxOccurs="unbounded" name="element">
                                <xs:complexType>
                                    <xs:attribute name="type" type="xs:unsignedByte" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Sign up to request clarification or add additional context in comments.

Comments

1

According to the Visual Studio schema generator, you can do it like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="elements">
          <xs:complexType>
            <xs:sequence>
              <xs:choice maxOccurs="unbounded">
                <xs:element name="element1" />
                <xs:element name="element2" />
              </xs:choice>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.