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 8251597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:17:25+00:00 2026-06-08T00:17:25+00:00

I have the following XML document: <?xml version=1.0 encoding=UTF-8?> <objects> <object>Clutch</object> <object>Gearbox</object> <object>Cylinder head</object>

  • 0

I have the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
    <object>Clutch</object>
    <object>Gearbox</object>
    <object>Cylinder head</object>
    <object>Starter</object>
    <object>Airbox</object>
    <object>Inlet manifold</object>
</objects>

And the following XSLT document:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="objects">
        <parts>
            <xsl:apply-templates>
                <xsl:sort select="object"/>
            </xsl:apply-templates>
        </parts>
    </xsl:template>

    <xsl:template match="object">
        <part>
            <xsl:apply-templates/>
        </part>
    </xsl:template>

</xsl:stylesheet>

When applied I am getting the following output as expected, but it is not being sorted:

<?xml version="1.0" encoding="UTF-8"?>
<parts>
    <part>Clutch</part>
    <part>Gearbox</part>
    <part>Cylinder head</part>
    <part>Starter</part>
    <part>Airbox</part>
    <part>Inlet manifold</part>
</parts>

Why is the <xsl:sort select="object"/> not being applied ?

  • 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-06-08T00:17:27+00:00Added an answer on June 8, 2026 at 12:17 am

    The reason is here:

          <parts>
              <xsl:apply-templates>
                  <xsl:sort select="object"/>
              </xsl:apply-templates>
          </parts>
    

    This applies templates to the children (object) of the current node (objects) and sorts them by the string value of their first object child.

    However in the provided XML document an object doesn’t have any object children — so they all have the same sort-key — the empty string — and their original order isn’t changed by the sort operation.

    Solution:

            <parts>
                <xsl:apply-templates>
                    <xsl:sort select="."/>
                </xsl:apply-templates>
            </parts>
    

    The complete transformation becomes:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="objects">
        <parts>
         <xsl:apply-templates>
            <xsl:sort select="."/>
         </xsl:apply-templates>
        </parts>
     </xsl:template>
    
     <xsl:template match="object">
      <part>
         <xsl:apply-templates/>
      </part>
     </xsl:template>
    </xsl:stylesheet>
    

    and when it is applied to the provided XML document:

    <objects>
        <object>Clutch</object>
        <object>Gearbox</object>
        <object>Cylinder head</object>
        <object>Starter</object>
        <object>Airbox</object>
        <object>Inlet manifold</object>
    </objects>
    

    the wanted, correct result is produced:

    <parts>
       <part>Airbox</part>
       <part>Clutch</part>
       <part>Cylinder head</part>
       <part>Gearbox</part>
       <part>Inlet manifold</part>
       <part>Starter</part>
    </parts>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML Document: <?xml version=\1.0\ encoding=\UTF-8\?> <atom:entry xmlns:atom=\http://www.w3.org/2005/Atom\ xmlns:apps=\http://schemas.google.com/apps/2006\ xmlns:gd=\http://schemas.google.com/g/2005\> <apps:property
I have the following string loaded to an XML document: <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope
I have an xml document in the following format. <?xml version=1.0 encoding=UTF-8 ?> <Rows>
I have the following XML document <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009' gd:etag='W/DEAERH47eCl7ImA9WhZTFEQ.'><id>http://code.google.com/feeds/issues/p/chromium/issues/full/921</id><published>2008-09-03T22:51:22.000Z</published><updated>2011-03-19T01:05:05.000Z</updated><title>Incorrect rendering</title><content
I have the following XML document : <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE inventory SYSTEM books.dtd>
I have the following XML document: <?xml version=1.0 encoding=UTF-8?> <Offices id=0 enabled=false> <office />
I have an xml document with the following structure: <?xml version=1.0 encoding=UTF-8?> <items> <item>
I have the following xml file: <?xml version=1.0 encoding=UTF-8?> <c1> <c2 id=0001 n=CM urlget=/at/CsM
Lets say I have the following XML file: <?xml version=1.0 encoding=utf-8?> <Customers xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=Customer.xsd>
I have the following xml:- <?xml version=1.0 encoding=UTF-8?> <patent-assignment> <assignment-record> <correspondent> <name>NORTH AMERICA INTERNATIONAL

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.