0

I need to write an XML schema that accept XMLs such as:

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <NodeA>something</NodeA>
  <NodeB>something</NodeB>
  <NodeC>something</NodeC>
  <NodeD>something</NodeD>
</Data>

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <NodeA>something</NodeA>
  <NodeC>something</NodeC>
  <NodeB>something</NodeB>
  <NodeD>something</NodeD>
</Data>

So in general, I want the elements in the list to be sequenced, except that a part of the list can appear in any order.

However I tried several appoaches for the xsd file and none of them works, e.g.

<xs:complexType name="Data">
  <xs:sequence>
    <xs:element name="NodeA"/>
    <xs:all xmlns:xs="">
      <xs:element name="NodeB"/>
      <xs:element name="NodeC"/>
    </xs:all>
    <xs:element name="NodeD"/>
  </xs:sequence>
</xs:complexType>

Putting NodeB and NodeC in a group doesn't work either.

I've googled those error messages but couldn't find anything useful...Why these xsds fails and how should I write it? Thanks!

GoCurry
  • 899
  • 11
  • 31
  • find some other threads with different problems but caused by same issue. seems like it's a shortcoming of XML schema http://stackoverflow.com/questions/839079/middle-way-between-xsd-all-and-xsd-sequence http://stackoverflow.com/questions/2408095/xml-schema-putting-both-sequence-and-all-under-one-complextype-node http://stackoverflow.com/questions/7420512/how-to-mix-xsdsequence-with-xsdall – GoCurry Apr 04 '12 at 17:44

1 Answers1

1

How about this?

<?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 name="NodeA"/>
                <xs:choice>
                    <xs:sequence>
                        <xs:element name="NodeB"/>
                        <xs:element name="NodeC"/>
                    </xs:sequence>
                    <xs:sequence>
                        <xs:element name="NodeC"/>
                        <xs:element name="NodeB"/>
                    </xs:sequence>
                </xs:choice>
                <xs:element name="NodeD"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Nick Ryan
  • 2,662
  • 1
  • 17
  • 24
  • Thanks! However I also tried this one before and the editor also throws an error... And a problem with this one is that the number of choices will increase in combination speed, so if I have more nodes I will be in trouble – GoCurry Apr 04 '12 at 17:08
  • xs:choice is the correct way. Try – Nick Ryan Apr 04 '12 at 17:57