views:

2470

answers:

3

This is an XML schema question.

I know that xsd:all element can't appear in a sequence (must be the top level element of its type).

That is, I cannot use the following:

<xsd:complexType name="Application">
  <xsd:sequence>
    <xsd:element ref="Name"></xsd:element>
    <xsd:all>
      <xsd:element ref="ADD"></xsd:element>
      <xsd:element ref="DELETE"></xsd:element>
    </xsd:all>
  </xsd:sequence>
</xsd:complexType>

My question is how to declare the "ADD" and "DELETE" elements above in any order (unordered set) but still make sure that the element "Name" would be the first and always appear. (Think of the situation where I have not only "ADD" and "DELETE" but about 10 or more unordered elements set: ADD, DELETE, EDIT etc...)

IMPORTANT NOTE: the ADD and DELETE may appear only ONCE, but their order is not matter:

<Application>
  <NAME>
   <DELETE>
   <ADD>
</Application>

but NOT:

<Application>
  <NAME>
  <DELETE>
  <ADD>
  <DELETE> <!--cannot appear twice-->
</Application>
+2  A: 

If I understand your request you are right on track, the only thing you're missing is maxOccurs="unbounded" on your choice.

I created the following schema:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://someNamespace" xmlns="http://someNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="Root" type="Application">
  </xs:element>

  <xs:complexType name="Application">
    <xs:sequence>
      <xs:element ref="Name"></xs:element>
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="ADD"></xs:element>
        <xs:element ref="DELETE"></xs:element>
      </xs:choice>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Name"/>
  <xs:element name="ADD"/>
  <xs:element name="DELETE"/>
</xs:schema>

And it worked well for

<ns0:Root xmlns:ns0="http://someNamespace"&gt;
  <ns0:Name />
  <ns0:ADD />
  <ns0:ADD />
  <ns0:DELETE />
  <ns0:ADD />
  <ns0:DELETE />
  <ns0:DELETE />
</ns0:Root>

but not for

<ns0:Root xmlns:ns0="http://someNamespace"&gt;
  <ns0:ADD />
  <ns0:ADD />
  <ns0:DELETE />
  <ns0:ADD />
  <ns0:DELETE />
  <ns0:DELETE />
</ns0:Root>
Yossi Dahan
Thanks a lot for your answer. I fine tunned my question above.I meant that the ADD and DELETE should not appear 0 or 1 times, but their order is not matter."all" can't be used this case, because the NAME mandatory element can't come with "all".Do you have any other solution?Thank
Ohad
+1  A: 

I don't believe this can be done without enumerating every combination of ADD, DELETE etc. Generally speaking unordered lists don't play well, either with DTDs or schemas.

Alohci
+1  A: 

I think you're looking for the "all" element, it allows for unordered list. However there are restrictions on the elements you can put in there. See http://www.w3.org/2005/07/xml-schema-patterns.html#Collection

Dirkjan Krijnders