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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:00:50+00:00 2026-05-28T08:00:50+00:00

I hate to even ask this as there are plenty of examples with regard

  • 0

I hate to even ask this as there are plenty of examples with regard to the xsl:sort functionality however I’ve followed them and am still, inexplicably, stuck.

I have a sort within a for-each, as seems to be the requirement. I then wish to take all the sorted data and apply a template.

Here is a sample XML. I have a group of log elements and result elements, at separate paths, that I need sorted together based on a numeric time stamp value. In this example everything is already naturally sorted except the last element which has a numerictimestamp value greater than the three result elements meaning it should appear last in the output. As written, the output will display all log elements followed by all result elements in the order they appear in the input XML.

<output>
  <logs>
    <log logtext="Username = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428014" />
    <log logtext="Password = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428027" />
    <log logtext="ServerURI = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428042" />
    <log logtext="TRACE TEST" loglevel="TRACE" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428084" />
    <log logtext="DEBUG TEST" loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428096" />
    <log logtext="INFO TEST" loglevel="INFO" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428109" />
    <log logtext="WARNING TEST" loglevel="WARN" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428120" />
    <log logtext="ERROR TEST" loglevel="ERROR" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428133" />
    <log logtext="Post-Result INFO Test" loglevel="INFO" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428353" />
  </logs>
  <results>
    <result name="Passed Test" resultformat="TEXT" resulttype="PASS" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352" />
    <result name="Failed Test" resultformat="TEXT" resulttype="FAIL" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352" />
    <result name="Running Processes" resultformat="TABLE" resulttype="NONE" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352">
    </result>
  </results>
</output>

Here are the relevant parts of the XSL:

<xsl:template match="/">       
    <html>
       <body>                
        <xsl:for-each select="//logs/log | //results/result">
          <xsl:sort data-type="number" select="@numerictimestamp"/>
          <xsl:apply-templates select="self::node()" />
        </xsl:for-each>
      </body>
</html>
 </xsl:template>

  <xsl:template match="log">
    .... handle formatting of the log elements
  </xsl:template>

  <xsl:template match="result">
    .... handle formatting of the result elements
  </xsl:template>

I guess this is a scope issue, that the sort is being applied only to the single item in the for-each, but I’m not sure what to change to resolve it and achieve the same result. With the exception of the sorting this XSL gives me exactly the output I desire.

EDIT1:

Updating question to include modified working code incorporating answers & comments below. The sort was failing due to the fact the numbers to be sorted were too large. They’re timestamp values that include milliseconds, specifically to be precise, so it is ironic that that precision was the cause of the issue. That said even with shorter numbers placing the sort within a for-each wasn’t actually sorting more than one item.

Most examples using xsl:sort do so within a for-each. I was unaware you could include it within the apply-templates.

The following modification works correctly: (Note that I explicitly specified the data type as text for clarity, I realize it is the default)

  <xsl:apply-templates select="//logs/log | //results/result">       
    <!--The data-type value for the sort MUST be text, due to rounding errors introduced
    attempting to sort the numerictimestamp-->
    <xsl:sort data-type="text" select="@numerictimestamp"/>          
  </xsl:apply-templates>

I was able to keep the rest of the XSL unchanged.

  • 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-28T08:00:51+00:00Added an answer on May 28, 2026 at 8:00 am

    Does this do what you want?

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
        <xsl:template match="/">
            <output>
                <xsl:apply-templates select="//log | //result">
                    <xsl:sort select="@numerictimestamp"/>
                </xsl:apply-templates>
            </output>
        </xsl:template>
    
        <xsl:template match="log | result">
            <xsl:copy-of select="."/>
        </xsl:template>
    </xsl:stylesheet>
    

    I think that you were correct that the for-each was reducing the scope to just the current item

    The other thing that is happening is the converting your numerictimestamp attributes to numbers is causing rounding.

    If I do this:

    <xsl:template match="log | result">
        <entry>
        <number><xsl:value-of select="number(@numerictimestamp)"/></number>
        <string><xsl:value-of select="@numerictimestamp"/></string>
        </entry>
    </xsl:template>
    

    I see this:

    <entry>
        <number>20120123170428132</number>
        <string>20120123170428133</string>
    </entry>
    <entry>
        <number>20120123170428352</number>
        <string>20120123170428353</string>
    </entry>
    <entry>
        <number>20120123170428352</number>
        <string>20120123170428352</string>
    </entry>
    

    As I understand things, the XSLT number data type is according to IEEE 754 and wikipedia tells me that double precision is accurate to 15.95 digits. Your numbers are 17 digits

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

Sidebar

Related Questions

I hate to ask this question as I am aware that there are plenty
I hate even bringing this up b/c there are so many hundreds of pages
I hate to be the third person to ask this, but the previous two
I hate ask stupid questions like this, but why doesn't my code do what
even though there are many examples out there, i can't find what I need?
I hate to post this but I am on a time crunch and need
I hate Wordpress for numerous reasons, but clients love it because it gives them
I hate to ask but I've searched all over the internet trying to figure
I hate asking questions like this - they're so undefined... and undefinable, but here
I hate to post this but I am seeing unwanted animation in an iPhone

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.