Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 18th, 2007, 02:05 PM
Registered User
 
Join Date: Mar 2006
Location: , , Canada.
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

Reply With Quote
  #2 (permalink)  
Old September 18th, 2007, 02:19 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old September 18th, 2007, 03:10 PM
Registered User
 
Join Date: Mar 2006
Location: , , Canada.
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it to work!
Thank you.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02: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 11:37 AM



All times are GMT -4. The time now is 01:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.