[xsd-users] Preserving XML element order

Barrie Kovish barrie.kovish at singularsoftware.com
Thu Feb 11 08:14:28 EST 2010


Hi Boris,

Thanks very much for your quick reply.  Here is a sample schema fragment.  I have changed the element names to be something generic:

<xs:element name='A' type='xs:string'>

 <xs:element name='W'>
  <xs:complexType>
   <xs:choice minOccurs='0' maxOccurs='unbounded'>
     <xs:element ref='A'/>
     <xs:element ref='B'/>
   </xs:choice>
  </xs:complexType>
 </xs:element>

 <xs:element name='U'>
  <xs:complexType>
   <xs:choice minOccurs='0' maxOccurs='unbounded'>
     <xs:element ref='W'/>
     <xs:element ref='V'/>
   </xs:choice>
    <xs:attribute name='id' type='xs:string'/>  
  </xs:complexType>
 </xs:element>

I would say that 90% of the schema is structure like this.  There are probably 50-100 choice elements in the schema and another 100-200 string and integer elements.

thanks,

Barrie

On 2010-02-11, at 4:12 AM, Boris Kolpackov wrote:

> Hi Barrie,
> 
> Barrie Kovish <barrie.kovish at singularsoftware.com> writes:
> 
>> I am reading an XML file in and writing it out using classes generated 
>> by XSD.  In general I need to make a few simple modifications to the XML 
>> file. But even if I don't modify the XML file the order of the elements 
>> is not preserved.  i.e. if the XSD classes read in
>> 	...
>> 	<X>1</X>
>> 	<Y>2</Y>
>> 
>> The XSD classes might write out 
>> 	...
>> 	<Y>2</Y>
>> 	<X>1</X>
> 
> There are certain XML Schema use-cases where this can happen due to the 
> flattening of the compositor structure in the C++/Tree mapping. For 
> previous discussions of this issue see:
> 
> http://www.codesynthesis.com/pipermail/xsd-users/2008-May/001697.html
> http://www.codesynthesis.com/pipermail/xsd-users/2008-June/001739.html
> 
> 
>> This is a big problem for me because the program which uses these XML 
>> files is sensitive to the order of the elements.  Note that the schema 
>> does note impose an order on the elements and the program which 
>> generates the XML files does not order the elements in a specific way.
>> However it is important for a specific document that the order not 
>> change.
> 
> Can you show the relevant schema fragments? 
> 
> 
>> Is there a solution to this?
> 
> There are several ways in which this can be worked around. But I will
> need to see the schema to be able to make a suggestion. 
> 
> There is also the C++/Hybrid[1] mapping in XSD/e[2] which recreates the
> compositor structure precisely and preserves the element order in all
> cases. While XSD/e was designed primarily for mobile/embedded systems,
> if configured to use STL, etc., it can also be used in general-purpose
> applications.
> 
> [1] http://www.codesynthesis.com/products/xsde/c++/hybrid/
> [2] http://www.codesynthesis.com/products/xsde/
> 
> Boris



More information about the xsd-users mailing list