1
votes

I am trying to check the order of the child elements in an xml,

My xml is

<main>
<col name="name1">test1</col>
<col name="name2">test2</col>
<col name="name3">test3</col>
<col name="name4">test4</col>
<col name="num1">true</col>
</main>

and I need to verify if the child elements are getting displayed in the correct order. The child elements in my file all have the same name but different attributes.

<xs:element minOccurs="0" maxOccurs="unbounded" name="col">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type ="OrderCheck" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

I would ideally like to do

<xs:complexType name="OrderCheck">
    <xs:sequence>
    <xs:element name="name1" type="xs:string"/>
    <xs:element name="name2" type="xs:string"/>
    <xs:element name="name3" type="xs:string"/>
    <xs:element name="name4" type="xs:string"/>
    <xs:element name="num1" type="xs:boolean"/>
    </xs:sequence>  
    </xs:complexType>

For me to be able to use xs:sequence I need to define a xs:complexType but I am not able to define a complexType under the "attribute" type, I can define only a simple type.But I cannot use xs:sequence under simpleType. How can I fix this?

2

2 Answers

3
votes

If you can use XSD 1.1 (implemented in Xerces beta and in Saxon-EE) then you can do this with assertions.

Another technique that can sometimes be useful in cases like this is to implement your validation process as a two-stage pipeline, consisting of a transformation followed by a validation step. The transformation would typically convert

<col name="name1">test1</col>

to

<name1>test1</name1>

and then you have a structure that is readily amenable to XSD 1.0 validation.

1
votes

Unfortunately what you want to do is not possible with XSD 1.0. With sequence you can specify the required order of different types, but not the same type with different data.

I believe that Schematron can be used for this.