user2054153
user2054153

Reputation: 23

How to write a valid xsd for elements with different names

I would like to validate xml with xsd schema, but i do not know how to create correct xsd schema for elements which would have changable names. My xml document :

<Settings xmlns="main">
  <Object>
    <Rules>
      <Rule0>
        <Range>0 to 0</Range>
        <String>SomeString</String>
        <Integer>80</Integer>
        <Enabled>No</Enabled>
      </Rule0>
      <Rule0>
        <Range>0 to 0</Range>
        <String>SomeString</String>
        <Integer>80</Integer>
        <Enabled>No</Enabled>
      </Rule0>
    </Rules>
    <ObjectEnabled>No</ObjectEnabled>
  </Object>
</Settings

And my xsd document:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"           targetNamespace="main" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Settings">
<xs:complexType>
  <xs:sequence>
    <xs:element name="Object">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Rules">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="?" maxOccurs="unbounded" minOccurs="0">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element type="xs:string" name="Range"/>
                      <xs:element type="xs:string" name="String"/>
                      <xs:element type="xs:byte" name="Integer"/>
                      <xs:element type="xs:string" name="Enabled"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element type="xs:string" name="ObjectEnabled"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

Off course instead of "Rule1" it could be "Blablabla". I know that i cannot put in name wildcard or question mark, so i kindly ask you for help.

Upvotes: 2

Views: 137

Answers (1)

martinstoeckli
martinstoeckli

Reputation: 24071

You will have to reorganise the elements a bit, i think.

Instead of having different elements...

<Rule0 />
<Rule1 />

you could write...

<Rule name="rule0" />
<Rule name="rule1" />

Think of elements as classes that describe the properties of all rules, and not of objects, that describe the properties of a single rule.

Upvotes: 1

Related Questions