Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2006, 04:23 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default sort by expression

Hi,

I am developing a report generator based on xslt.
I am trying to sort by expression.
To make it simple I have wrote an example of what I am trying to do.

I need to sort by date.
In order to do so I am taking the date (dd/mm/yyyy) and changing it to yyyymmdd and then sorting this expression by regular number sort.

this can be easily done with the following expression (assuming @th is the date):

<xsl:sort select="concat(substring(@th,7,4),substring(@th,4, 2),substring(@th,1,2))" data-type="number"/>

here is the full example:

xml
-----------------------------------

<?xml version="1.0"?>
<root>
  <Chanich FullName="firstName1 lastName1" id="29152485" th="15/11/1999" />
  <Chanich FullName="firstName2 lastName2" id="32036758" th="01/10/2003" />
  <Chanich FullName="firstName3 lastName3" id="32273716" th="15/08/2000" />
  <Chanich FullName="firstName4 lastName4" id="27425586" th="15/11/1999" />
  <Chanich FullName="firstName5 lastName5" id="58829979" th="14/08/1994" />
  <Chanich FullName="firstName6 lastName6" id="24354292" th="13/02/2000" />
</root>


xslt
-----------------------------------
<xsl:template match="/">
    <html>
    <body>
      <table border="1">
      <xsl:for-each select="root/Chanich">
        <xsl:sort select="concat(substring(@th,7,4),substring(@th,4, 2),substring(@th,1,2))" data-type="number"/>
        <tr>
          <td><xsl:value-of select="@FullName"/></td>
          <td><xsl:value-of select="@id"/></td>
          <td><xsl:value-of select="@th"/></td>
        </tr>
      </xsl:for-each>
      </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>

It is working fine.


my problem is that the sorting expression needs to come dynamically form variable.
so lets say that I want to rewrite the xslt that the sorting expression will come from a variable.
it will look like this:

<xsl:template match="/">
    <html>
    <body>
      <table border="1">
      <xsl:variable name="sortSelect" select="concat(substring(@th,7,4),substring(@th,4, 2),substring(@th,1,2))"/>
      <xsl:for-each select="root/Chanich">
        <xsl:sort select="$sortSelect"/>
        <tr>
          <td><xsl:value-of select="@FullName"/></td>
          <td><xsl:value-of select="@id"/></td>
          <td><xsl:value-of select="@th"/></td>
        </tr>
      </xsl:for-each>
      </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>


1. How can I make it Work ???
2. Is there any "elegant" way to sort by dates?

I am writing xslt ver 2.0 using saxon.

thanx,
Roni.

 
Old December 17th, 2006, 02:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A number of products have an extension function that allows you to construct an expression dynamically as a string (or read it from a source document) and then evaluate it. In Saxon it's called saxon:evaluate(). You need to read the rules carefully concerning the static and dynamic context for the expression. You can't do this in standard XSLT, even in 2.0.

I would recommend always storing dates in XML in the standard international YYYY-MM-DD format. YOu'll then be able to sort them directly, simply by casting to the xs:date data type.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
Message> in query expression <expression>. (Error ybg1 Access 5 July 15th, 2007 05:42 AM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
SORT pallone XSLT 13 February 25th, 2005 11:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.