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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:20:09+00:00 2026-05-15T15:20:09+00:00

I am trying to sort an xml using xsl. Got some sample from xml.com.

  • 0

I am trying to sort an xml using xsl. Got some sample from xml.com. Its seems logical and intuitive. I tried, some how its not sorting. Its hard getting my head around this.

Here is the Xsl I am using for sorting

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>
 <xsl:template match="SharePointSites">
  <xsl:copy>
   <xsl:apply-templates>
    <xsl:sort select="Document/@Name"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*">
  <xsl:copy>
   <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>


</xsl:stylesheet>

Below, is the XML I am trying to sort. Output is also the same. Its not obvious missing of hierarchy of tags. As I understand from xml.com sample, I have also tried specifying complete hierarchy of tags using match and select tags above.

 <SharePointSites>

<Site Url="http://workspace.imperial.ac.uk/Activities/default.aspx" Name="Activities">

<Directory Name="Public">
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Imperial Activities Limited reg no etc.doc" Name="Imperial Activities Limited reg no etc.doc"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Property Enqiry Form.DOC" Name="Property Enqiry Form.DOC"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/New Property Enquiry Form.doc" Name="New Property Enquiry Form.doc"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/52 Princes Gardens.pdf" Name="52 Princes Gardens.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Silwood Web site Photo's.ppt" Name="Silwood Web site Photo's.ppt"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Service charge.pdf" Name="Service charge.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/SPIP-G.pdf" Name="SPIP-G.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Silwood Business Park pictures.doc" Name="Silwood Business Park pictures.doc"/>
</Directory>
<Directory Name="Internal"/>
</Site>
</SharePointSites>

outup is still same. Here is how I am applying transform on XML document.

XslCompiledTransform myXslTrans = new XslCompiledTransform();

            //load the Xsl 
            myXslTrans.Load(@"C:\My code\dotNet Development\SharepointXML\WebService1\SharepointSiteContent.xslt");                

            //do the actual transform of Xml document
            myXslTrans.Transform(aDoc, null, TransformedxmlWriter);

            // Set to document
            aTransforemdDoc.InnerXml = aTransformedStrbulider.ToString();
  • 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-15T15:20:09+00:00Added an answer on May 15, 2026 at 3:20 pm

    You’re sorting at the wrong level. If you want to sort the documents then you need a template that matches <Directory> and contains the apply-templates with the sort.

    If all you’re doing is copying the input to the output with sorting, do a google search for “xsl identity transform” and add a template matching “Directory”.

    Here’s a solution

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="Directory">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()">
            <xsl:sort select="@Name"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    At the point where you’ve matched Directory, and inside apply-templates, the context node is the each Document in turn. So the sort needs to be just @Name.

    Detailed explanation:

    1. The first template is the ‘identity template’ (memorize this form, you’ll use it a lot).
    2. The second template specializes handling for “Directory” nodes only.
      • The first apply-templates copies any attributes
      • The second apply-templates copies child nodes after sorting
      • Both of these templates reuse the identity transform template implicitly
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a XML doc using xsl:sort My requirement is to
(Using the latest MVC 2 RC 2) I'm trying to sort some XML in
I am trying to sort an array sent from an XML feed. The Array
I am having a problem trying to sort with an XSL file using the
I'm trying to extract some data from various HTML pages using a python program.
I am trying to sort a xml using linq queries using the following code
I am trying to figure out how to sort the XML list of employees
I am trying to sort pixel values of an image (example 80x20) from lowest
I am trying to make the transition from using call-template to using applay templates
I'm trying to unmarshal a simple xml document from a public api from Convio.

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.