DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Sort by Date in XSL

  1. #1
    Nafisa Khan Guest

    Sort by Date in XSL


    I was trying to sort document by date in XSL.
    I know how to sort by ascending and descending order.
    But I don't know how to sort by date in xsl.Does XSLT supports it?
    Is anyone can help me with that?

    I used the following xsl code to sort document by ascending/descending order

    Ascending : <xsl:for-each select="/folder_tree/folder/document" order-by="+
    @object_name">

    Descending: <xsl:for-each select="/folder_tree/folder/document" order-by="
    - @object_name">

    Need to know the code to sort by date.


  2. #2
    Jay Guest

    Re: Sort by Date in XSL


    This may seem like a hack, but it's worked well for me. If you can, change
    to date format to YYYYMMDD, that way you're technically sorting a numeric
    value that sorts very nicely. You can use the substring functions in XSLT
    to convert the data back to MM/DD/YYYY or whatever format you wish to use
    during the transformation. If anyone knows of a better way, I'd like to
    hear it.

    Jay

    "Nafisa Khan" <nafkhan@hotmail.com> wrote:
    >
    >I was trying to sort document by date in XSL.
    >I know how to sort by ascending and descending order.
    >But I don't know how to sort by date in xsl.Does XSLT supports it?
    >Is anyone can help me with that?
    >
    >I used the following xsl code to sort document by ascending/descending order
    >
    >Ascending : <xsl:for-each select="/folder_tree/folder/document" order-by="+
    >@object_name">
    >
    >Descending: <xsl:for-each select="/folder_tree/folder/document" order-by="
    >- @object_name">
    >
    >Need to know the code to sort by date.
    >



  3. #3
    peter baker Guest

    Re: Sort by Date in XSL


    XSLT sorts dates alphaberically, so to get around the problem use the following
    example as a guide.

    XML code:
    <Employee startdate="11/20/1999">
    <name>John</name>
    </Employee>
    <Employee startdate="07/06/2001">
    <name>John</name>
    </Employee>
    <Employee startdate="04/23/1999">
    <name>John</name>
    </Employee>

    xslt code to sort by date:
    <xsl:template match="Employee">
    <xsl:apply-templates>
    <xsl:sort select="substring(@startdate,7,4)"/> <!-- year sort -->
    <xsl:sort select="substring(@startdate,1,2)"/> <!-- day sort -->
    <xsl:sort select="substring(@startdate,4,2)"/> <!-- month sort -->
    </xsl:apply-templates>
    </xsl:apply-templates>

    This will allow you to sort by date.

  4. #4
    Join Date
    Jan 2011
    Posts
    1

    Thumbs up Nit about the comments

    First, thank you for a wonderful, helpful, and very clear article.

    The xsl code is quite right for a date sort, when the date text is formatted mm/dd/yyyy . But not the comments! correct is

    .......
    <xsl:sort select="substring(@startdate,7,4)"/> <!-- year sort -->
    <xsl:sort select="substring(@startdate,1,2)"/> <!-- month sort -->
    <xsl:sort select="substring(@startdate,4,2)"/> <!-- day sort -->
    .......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center