Problem with xsl:sort and apply-templates

Posted on 2005-05-11
Last Modified: 2009-07-29

I simply need to select the most recent list of item (by date). The XML contains
multiple lists, in any order and will contain "future" items. The dates are converted
to UTC basic format (CCYYMMDD) so they can be treated as ordinary numbers. So the
logic is essentially, sort the list by "date", then select the last() item
where date is less than "now".

I can only get it to work if I do it in two steps (See Method #2 below). But this
means that each list node needs to be tested. Does this create any additional
overhead? Can it be done in one step?

   <list date="20041231">
      <item title="Ancient #1"/>
      <item title="Ancient #2"/>
      <item title="Ancient #3"/>

   <list date="20050515">
      <item title="Future #1"/>
      <item title="Future #2"/>
      <item title="Future #3"/>
   <list date="20050507">
      <item title="Current #1"/>
      <item title="Current #2"/>
      <item title="Current #3"/>

   <list date="20050505">
      <item title="Recent #1"/>
      <item title="Recent #2"/>
      <item title="Recent #3"/>

<xsl:stylesheet version="1.0" xmlns:xsl="" >
<xsl:param name="now">20050511</xsl:param>

   <xsl:template match="//schedule">

      Method #1 Doesn't work because the sort happens after the select<br/>
      <xsl:apply-templates select="list[@date &lt; $now][last()]/item">
         <xsl:sort select="@date"/>


      Method #2 Works: but it needs two steps. Can it be done together?<br/>
      <xsl:apply-templates select="list[@date &lt; $now]">
         <xsl:sort select="@date"/>


   <xsl:template match="list">
      Test if current node is last().
      <xsl:if test="position() = last()">
         <xsl:apply-templates select="item"/>

   <xsl:template match="item">
      <xsl:value-of select="@title"/><br/>

Question by:cklanac
    LVL 23

    Accepted Solution

    it cannot be done in one single step because you have to
    1. Get the resulting node-set (collection of nodes)
    2. Sort the node-set
    3. Retrieve the last position.

    <xsl:for-each select="list[@date &lt; $now][last()]/item">
    <xsl:sort select="@date"/>
    <xsl:if test="position() = last()">
    <xsl:apply-templates select="." />

    Your attempt
    <xsl:apply-templates select="list[@date &lt; $now][last()]/item">
    simply means that you are retrieving the last node from the nodeset against the document heirarchy. Typically this would mean that the last node in the tree that matches the criteria, not sorted of course.
    LVL 1

    Author Comment

    Thanks for the info...

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now