Dynamic sort order or sort datatype
Hi,
In reading the XSLT 2.0 book by M. Kay, I saw that a conditional expression can be used as a sort key. I was able to implement that however, I also need to have a dynamic sort order as well as a dynamic sort datatype because one of the sort keys is a number. I tried applying the conditional sort order and datatype and Saxon 8 complained. Here is my code, the xml to be transformed is below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="LANG">E</xsl:param>
<xsl:param name="sort">T</xsl:param>
<xsl:param name="order">A</xsl:param>
<xsl:template match="/page">
<table border="1" cellspacing="0" width="100%">
<tr>
<xsl:choose>
<xsl:when test="$LANG='E'">
<th>
User Name
</th>
<th>Account Status</th>
<th>Table Space Name</th>
<th>
Used Space in KBytes
</th>
</xsl:when>
<xsl:otherwise>
<th>Nom d'utilisateur</th>
<th>Statut du compte</th>
<th>Nom de l'ensemble des tables</th>
<th>Espace utilisé en KOctets</th>
</xsl:otherwise>
</xsl:choose>
</tr>
<xsl:for-each-group select="table/tablespace" group-by="OWNER">
<xsl:sort order="if ($order eq 'D') then descending else ascending"
select="if ($sort eq 'T') then sum(current-group()/KBYTES_USED) else current-grouping-key()"
data-type="if ($sort eq 'T') then number else 'text'"/>
<xsl:variable name="count" select="count(current-group())"/>
<tr>
<td>
<xsl:attribute name="rowspan"><xsl:value-of select="$count+2"/></xsl:attribute>
<b>
<xsl:value-of select="current-grouping-key()"/>
</b>
</td>
<td>
<xsl:attribute name="rowspan"><xsl:value-of select="$count+2"/></xsl:attribute>
<xsl:variable name="status" select="ACCOUNT_STATUS"/>
<xsl:choose>
<xsl:when test="$status ='OPEN'">
<xsl:value-of select="$status"/>
</xsl:when>
<xsl:otherwise>
<b style="color:rgb(255,0,0);">
<xsl:value-of select="$status"/>
</b>
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
<xsl:for-each select="current-group()">
<tr>
<td>
<xsl:value-of select="TABLESPACE_NAME"/>
</td>
<td align="right">
<xsl:value-of select="format-number(KBYTES_USED,'###,###,###,###')"/>
</td>
</tr>
</xsl:for-each>
<tr>
<th>Total</th>
<td align="right" bgcolor="#f0f0f0">
<b>
<xsl:value-of select="format-number(sum(current-group()/KBYTES_USED),'###,###,###,###')"/>
</b>
</td>
</tr>
</xsl:for-each-group>
</table>
</xsl:template>
</xsl:stylesheet>
--------
<?xml version="1.0" encoding="ISO-8859-1"?>
<page>
<table>
<tablespace num="1">
<OWNER>USER1</OWNER>
<TABLESPACE_NAME>TABSPACE_DATA</TABLESPACE_NAME>
<ACCOUNT_STATUS>OPEN</ACCOUNT_STATUS>
<KBYTES_USED>24940120</KBYTES_USED>
</tablespace>
<tablespace num="2">
<OWNER>USER1</OWNER>
<TABLESPACE_NAME>TABSPACE_INDEX</TABLESPACE_NAME>
<ACCOUNT_STATUS>OPEN</ACCOUNT_STATUS>
<KBYTES_USED>2364880</KBYTES_USED>
</tablespace>
<tablespace num="3">
<OWNER>SYS</OWNER>
<TABLESPACE_NAME>TABSPACE_DATA</TABLESPACE_NAME>
<ACCOUNT_STATUS>OPEN</ACCOUNT_STATUS>
<KBYTES_USED>336</KBYTES_USED>
</tablespace>
<tablespace num="4">
<OWNER>SYS</OWNER>
<TABLESPACE_NAME>TABSPACE_INDEX</TABLESPACE_NAME>
<ACCOUNT_STATUS>OPEN</ACCOUNT_STATUS>
<KBYTES_USED>80</KBYTES_USED>
</tablespace>
<tablespace num="5">
<OWNER>T_USER</OWNER>
<TABLESPACE_NAME>TABSPACE_INDEX</TABLESPACE_NAME>
<ACCOUNT_STATUS>OPEN</ACCOUNT_STATUS>
<KBYTES_USED>16480</KBYTES_USED>
</tablespace>
</table>
</page>
------
Is there a way to change the sort order as well as the sort datatype dynamically?
Thanks,
Francine
|