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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:27:17+00:00 2026-05-18T02:27:17+00:00

I am having a problem trying to sort with an XSL file using the

  • 0

I am having a problem trying to sort with an XSL file using the XslCompiledTransform in CLR4.0. Here is my sample XML file (Note: there is a space after the second <foo> element):

<?xml version="1.0" encoding="utf-8"?>
<reflection> 
  <apis>
    <api id="A">
      <foos>
        <foo/>
      </foos>
    </api>
    <api id="B">
      <foos>
        <foo/> 
      </foos>
    </api>     
  </apis>
</reflection>

When I apply the following XSL file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
  <xsl:template match="/">
    <html>
      <body>
        <table>
          <xsl:apply-templates select="/reflection/apis/api">
                        <xsl:sort select="@id" />
                    </xsl:apply-templates>          
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="api">
    <tr>
      <td>
        <xsl:value-of select="@id" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

I get the following result:

<html>
  <body>
    <table>
      <tr>
        <td>B</td>
      </tr>
      <tr>
        <td>A</td>
      </tr>
    </table>
  </body>
</html>

However, if I remove the space after the second <foo> element, the resulting file is sorted correctly. This seems like it’s probably a bug in the XslCompiledTransform, but I was hoping someone might have a workaround.

Edit: If anyone is having trouble reproducing it, here is the code I am using:

XslCompiledTransform xslt = new XslCompiledTransform();
XsltSettings transformSettings = new XsltSettings(true, true);
xslt.Load("CreateVSToc.xsl", transformSettings, new XmlUrlResolver());

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreWhitespace = true;
Stream readStream = File.Open("reflection.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using (XmlReader reader = XmlReader.Create(readStream, readerSettings))
{
    Stream outputStream = File.Open("toc.xml", FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete);
    using (XmlWriter writer = XmlWriter.Create(outputStream, xslt.OutputSettings))
    {

        XsltArgumentList arguments = new XsltArgumentList();
        xslt.Transform(reader, arguments, writer);
    }
}
  • 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-18T02:27:18+00:00Added an answer on May 18, 2026 at 2:27 am

    @Russ Ferri, thanks for your answer. It pointed me in the right direction. It appears the bug in the XslCompiledTransform is that when you want to sort by an attribute of an element, it actually sorts by the value of the first child element of that element. So as Russ pointed out, this will sort correctly with my original transform file:

    <reflection>
      <apis>
        <api id="C">
          <id>C</id>
          <foos>
            <foo/>
          </foos>
        </api>
        <api id="B">
          <id>B</id>
          <foos>
            <foo/>
          </foos>
        </api>
      </apis>
    </reflection>
    

    But so will this:

    <reflection>
      <apis>
        <api id="C">
          <anyElementName>C</anyElementName>
          <foos>
            <foo/>
          </foos>
        </api>
        <api id="B">
          <anyElementName>B</anyElementName>
          <foos>
            <foo/>
          </foos>
        </api>
      </apis>
    </reflection>
    

    In fact, the attribute name is completely ignored, so if I run the transform on something like this:

    <reflection>
      <apis>
        <api id="C">
          <anyElementName>Z</anyElementName>
          <foos>
            <foo/>
          </foos>
        </api>
        <api id="A">
          <anyElementName>Y</anyElementName>
          <foos>
            <foo/>
          </foos>
        </api>
        <api id="B">
          <anyElementName>X</anyElementName>
          <foos>
            <foo/>
          </foos>
        </api>
      </apis>
    </reflection>
    

    The result looks like this:

    <html>
      <body>
        <table>
          <tr>
            <td>B</td>
          </tr>
          <tr>
            <td>A</td>
          </tr>
          <tr>
            <td>C</td>
          </tr>
        </table>
      </body>
    </html>
    

    Which is the correct sorting if you were sorting by the <anyElementName> elements

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
Ok, so here's the full description of the problem I'm having: I am trying
Having a problem trying to create a function, as part of a BizTalk helper
I am trying to convert this test code to C# and having a problem
I'm trying to find an answer for a problem my developers are having. I
I have been trying to tackle this problem , but I am having difficulty
I'm trying to do some basic paging in MSSQL. The problem I'm having is
I'm having a logic problem trying to return a collection of objects from a
I have been having some problems trying to get my PHP running. When I
I've been having a lot of problems trying to figure out how to use

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.