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 September 18th, 2007, 01:05 PM
Registered User
 
Join Date: Mar 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old September 18th, 2007, 01:19 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

order and data-type and are attribute value templates, so to use XPath expression you need to surround them with braces, {}. You also need number in quotes:
Code:
data-type="{if ($sort eq 'T') then 'number' else 'text'}"/>
--

Joe (Microsoft MVP - XML)
 
Old September 18th, 2007, 02:10 PM
Registered User
 
Join Date: Mar 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it to work!
Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort order in local language. surjit singh .NET Framework 2.0 2 July 21st, 2007 01:54 AM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
sort recordset in different order pablohoney Classic ASP Databases 0 December 29th, 2005 08:32 PM
Sort order Question Tere Crystal Reports 1 February 14th, 2005 03:18 PM
Access 2K Sort Order rgerald Access VBA 0 August 12th, 2004 10:37 AM





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