Sorting an XML file through XSL

Posted by jbugeja on Stack Overflow See other posts from Stack Overflow or by jbugeja
Published on 2010-05-25T18:53:37Z Indexed on 2010/05/25 19:41 UTC
Read the original article Hit count: 219

Filed under:
|

I have an XML file that I want to sort by an attribute. The file is structured as shown below:

<wb xmlns:cf="http://www.macromedia.com/2004/cfform">

    <a:form name="chart">  

        <a:input FIELDNUMBER="09" INDEX="2" LEFT="200" />
        <a:input FIELDNUMBER="08" INDEX="3" LEFT="200" />

        <a:fieldset FIELD="a" FIELDNAME="FieldSet1">                              
            <a:input FIELDNUMBER="02" INDEX="4" LEFT="200" />
            <a:select1  FIELDNUMBER="01" />
        </a:fieldset>

        <a:fieldset FIELD="b" FIELDNAME="FieldSet1">                              
            <a:input FIELDNUMBER="04" INDEX="7" LEFT="200" />
            <a:select1  FIELDNUMBER="03" />
            <a:fieldset FIELD="c" FIELDNAME="FieldSet1">                              
                <a:input FIELDNUMBER="06" INDEX="8" LEFT="200" />
                <a:input FIELDNUMBER="05" INDEX="6" LEFT="200" />
            </a:fieldset>
       </a:fieldset>

    </a:form>

</wb> 

I would like to sort the above XML all throughout by @fieldnumber, but at the same I want to keep the same structure of the XML. I have managed to sort other XML file but they did not have such nesting levels. Is this possible with XSL alone and if so how can this be done?

The output should be as follows:

<wb xmlns:cf="http://www.macromedia.com/2004/cfform">

    <a:form name="chart">  

        <a:input FIELDNUMBER="08" INDEX="3" LEFT="200" />
        <a:input FIELDNUMBER="09" INDEX="2" LEFT="200" />

        <a:fieldset FIELD="a" FIELDNAME="FieldSet1">                              
            <a:select1  FIELDNUMBER="01" />
            <a:input FIELDNUMBER="02" INDEX="4" LEFT="200" />
        </a:fieldset>

        <a:fieldset FIELD="b" FIELDNAME="FieldSet1">                              
            <a:select1  FIELDNUMBER="03" />
            <a:input FIELDNUMBER="04" INDEX="7" LEFT="200" />
            <a:fieldset FIELD="c" FIELDNAME="FieldSet1">                              
                <a:input FIELDNUMBER="05" INDEX="6" LEFT="200" />
                <a:input FIELDNUMBER="06" INDEX="8" LEFT="200" />
            </a:fieldset>
       </a:fieldset>

    </a:form>

</wb> 

As another example, should the FIELDNUMBER 04 be changed to a value greater than 7 such as 10 (let's assume 10 in this example) then the output of the fieldset with FIELD value b becomes:

        <a:fieldset FIELD="b" FIELDNAME="FieldSet1">                              
            <a:select1  FIELDNUMBER="03" />
            <a:fieldset FIELD="c" FIELDNAME="FieldSet1">                              
                <a:input FIELDNUMBER="05" INDEX="6" LEFT="200" />
                <a:input FIELDNUMBER="06" INDEX="8" LEFT="200" />
            </a:fieldset>
            <a:input FIELDNUMBER="10" INDEX="7" LEFT="200" />
       </a:fieldset>

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xsl