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.
|