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

  • Home
  • SEARCH
  • 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 6140937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:10:55+00:00 2026-05-23T18:10:55+00:00

I am wondering whether XSLT makes it possible to sort an XML file if

  • 0

I am wondering whether XSLT makes it possible to sort an XML file if I don’t know the entire XML-schema.

For example I would like to sort the following XML file.
Sort /CATALOG/CD elements by /CATALOG/CD/TITLE

<CATALOG attrib1="value1">
  <DVD2>
    <TITLE>The Godfather2</TITLE>
  </DVD2>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD attrib4="value4">
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>
      <CATALOG>
        <CD><TITLE>E</TITLE></CD>
        <CD><TITLE>I</TITLE></CD>
        <CD><TITLE>D</TITLE></CD>
      </CATALOG>
    </PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD attrib2="value2">
    <TITLE attrib3="value3">Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
  <DVD>
    <TITLE>The Godfather1</TITLE>
  </DVD>
</CATALOG>

The output should be:

<CATALOG attrib1="value1">
  <CD attrib4="value4">
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>
      <CATALOG>
        <CD><TITLE>E</TITLE></CD>
        <CD><TITLE>I</TITLE></CD>
        <CD><TITLE>D</TITLE></CD>
      </CATALOG>
    </PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD attrib2="value2">
    <TITLE attrib3="value3">Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <DVD2>
    <TITLE>The Godfather2</TITLE>
  </DVD2>
  <DVD>
    <TITLE>The Godfather1</TITLE>
  </DVD>
</CATALOG>

The following is one of the many tries I did:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <!--<CATALOG>-->
    <xsl:for-each select="CATALOG/CD">
      <xsl:sort select="TITLE" />
      <xsl:copy-of select="."/>
    </xsl:for-each>
    <!--</CATALOG>-->
  </xsl:template>
</xsl:stylesheet>

The problem is that, with this XSLT, XML parts outside the CD list are not displayed.
I could uncomment the two commented-out parts of code, but that’s exactly what I want to avoid.
In that case if any attributes are added to the CATALOG element, they would not be copied to output XML.
I don’t want to re-build the XML file: I just want to do a sort knowing exact information only about some part of the XML-schema.

This functionality is easy to implement for example using .NET (with XmlDocument and XmlNode objects), or Python’s lxmx library, but is it possible with XSLT?

Thanks!

Note: It is not easy to find a sample input XML which will avoid misunderstanding the question in all cases. But I will try to detail the problem as much as I can:

  • only CD elements right under CATALOG should be sorted (for example CD elements under the Bob Dylan section should be left untouched)
  • it is all the same whether elements other than CD (for example DVD and DVD2) are in the beginning or end of the list
  • no elements, attributes, values, comments, so nothing should be missing from the output XML
  • non-CD elements (for example DVD and DVD2) should not be sorted by the TITLE subelement
  • 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-23T18:10:55+00:00Added an answer on May 23, 2026 at 6:10 pm

    Keeping on the line of just modifying the identity transformation (which might not be really safe), I think that the following should be equivalent to @Tim’s answer.

    NOTE I’m not promoting this technique at all, unless you understand what’s the general behavior of the identity transformation.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* 
                    | node()[not(self::CD[parent::CATALOG])]"/>
                <xsl:apply-templates select="CD[parent::CATALOG]">
                    <xsl:sort select="TITLE"/>
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    or, if you care about the other elements DVD and DVD2, you can do:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="CD[parent::CATALOG]">
                    <xsl:sort select="TITLE"/>
                </xsl:apply-templates>
                <xsl:apply-templates select="node()
                    [not(self::CD[parent::CATALOG])]"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering whether something like this is possible (and relatively easy to do), and
Was wondering whether anyone would know why do we use the parentheses in this
I was wondering how I can possible extend XSLT 1.0 so that I can
I am wondering whether it is possible to create a hasMany Relationship in a
I was wondering whether the instant search using jQuery would cause a massive load
I was wondering whether it is possible to programmatically change the name of an
I was just wondering whether if it was possible to replace recursion with an
I am wondering whether there is twitter like open source platform written in .Net
I was wondering whether it is possible to assign a value to an HTML
I am wondering whether it is possible to change the action of a Facebook

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.