Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD - How to describe an unordered set of element types where the first element must appear first in the sequence?

Tags:

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> 
like image 721
ogee Avatar asked Jan 06 '09 09:01

ogee


1 Answers

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">   <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">   <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">   <ns0:ADD />   <ns0:ADD />   <ns0:DELETE />   <ns0:ADD />   <ns0:DELETE />   <ns0:DELETE /> </ns0:Root> 
like image 52
Yossi Dahan Avatar answered Sep 17 '22 16:09

Yossi Dahan