Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 827825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:37:56+00:00 2026-05-15T03:37:56+00:00

I have an XML file that I want to sort by an attribute. The

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:37:56+00:00Added an answer on May 15, 2026 at 3:37 am

    Despite the inconsistency pointed to by Jim Garrison, I tried to come up with something that matches your description:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:template match="*">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:for-each select="*">
            <xsl:sort select="@FIELDNUMBER|.//@FIELDNUMBER"/>
            <xsl:apply-templates select="." />
          </xsl:for-each>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    The sort works first on the element’s FIELDNUMBER attribute, or on the first FIELDNUMBER attribute it finds on the element’s children if the element doesn’t have one itself. Here’s the output (xmlns:a was added to the source document, so it got carried to the output):

    <?xml version="1.0" encoding="utf-8"?>
    <wb xmlns:cf="http://www.macromedia.com/2004/cfform" xmlns:a="urn:dummy">
      <a:form name="chart">
        <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:input FIELDNUMBER="08" INDEX="3" LEFT="200" />
        <a:input FIELDNUMBER="09" INDEX="2" LEFT="200" />
      </a:form>
    </wb>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that contains price value.I want to sort my table
I have an xml file that I want to configure using a bash script.
I have a single XML file that I want to index using Lucene.NET. The
I have a flash applet that I want to grab a remote XML file
I have a ListView that is populated using an XML file. However, I want
I have an XML file whose contents I want to sort by document order
I have a big xml file that I want to get transferred from c:
I have an XML file that is only available by going to domain.com/file.xml. When
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.