Order definition in XML Schema

Go To StackoverFlow.com

2

I want to define that the first element will be of a specific type, and the following will be some elements in any order. Like this:

<node>
<!-- always at first -->
<node-data>
...
</node-data>

<!-- other nodes in any order-->
<node3></node3>
<node1></node1>
<node2></node2>
</node>

I can't use <xsd:sequence> because it will force all nodes to be in order.

Thanks for your help. Sorry on my english.

2012-04-04 17:11
by RoadBump


2

A good starting layout could look like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="node">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="node-data"/>
        <xs:choice minOccurs="0" maxOccurs="unbounded">         
          <xs:element name="node3" />
          <xs:element name="node1" />
          <xs:element name="node2" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

You could then refine the types, add more nodes under the choice, etc. If you want to support wildcards, then use of xs:any would be required.

2012-04-04 17:18
by Petru Gardea
Thank you very much, it helped me. It's strange that the authors of xsd not gave a easier way to do this basic feature - RoadBump 2012-04-04 23:11
Ads