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 July 20th, 2005, 09:03 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Dynamic xsl:sort Method

I'm almost done with this application, which merges an XML doc and XSLT stylesheet through an ASP.NET page to form an XHTML resulting page. I've successfully been able to do almost everything except for the addition of a dynamic xsl:sort method (similar to an ASP.NET sortable DataGrid)

I've added empty xsl:parameters to the XSLT stylesheet, passed those 3 variables into the querystring attached to the ASP.NET page's URL, and I added a script within the ASP.NET page to transform those paramaters to whatever's contained in the querystring. But the parameter transformation is not working, and each of the parameters is storing the complete URL & querystring instead of each specified parameter.

So I'm attaching the code that I'm using in hopes that someone can see what step I'm missing. Any & all constructive criticism is welcome. Thanks.

XML doc (shortened example):
Code:

<formsdocs>
    <form>
        <title>Form 1</title>
        <url>http://DOMAIN/DIRECTORY/docs/pdf/FILENAME.pdf</url>
        <type>internal</type>
        <format>pdf</format>
        <size>2 KB</size>
        <dept>IT</dept>
        <div />
    </form>
    <form>
        <title>Form 2</title>
        <url>http://DOMAIN/DIRECTORY/SUBDIRECTORY/FILENAME.html</url>
        <type>internal</type>
        <format>html</format>
        <size>5 KB</size>
        <dept>AS</dept>
        <div />
    </form>
    <form>
        <title>Form 3</title>
        <url>http://www.OTHERWEBSITE.com</url>
        <type>external</type>
        <format>html</format>
        <size />
        <dept />
        <div />
    </form>
</formsdocs>

XSLT stylesheet:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:param name="sortBy" select="''" />
    <xsl:param name="sortType" select="''" />
    <xsl:param name="sortOrder" select="''" />

    <xsl:variable name="pageURL">http://DOMAIN/DIRECTORY/FILENAME.aspx</xsl:variable>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>Forms & Documents</title>
            </head>
            <body>
                <h1>Forms & Documents</h1>
                <br />
                <br />
                <table class="navyblueborder" width="95%" align="center">
                    <tr bgcolor="#333366" class="whiteheader">
                        <th align="left">
                            <xsl:text>Form/Document </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=title&sortType=text&sortOrder=ascending">[ASC</a>
                            <xsl:text> | </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=title&sortType=text&sortOrder=descending">DESC]</a>
                        </th>
                        <th align="left">
                            <xsl:text>Department </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=dept&sortType=text&sortOrder=ascending">[ASC</a>
                            <xsl:text> | </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=dept&sortType=text&sortOrder=descending">DESC]</a>
                        </th>
                        <th align="center">
                            <xsl:text>Type </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=type&sortType=text&sortOrder=ascending">[ASC</a>
                            <xsl:text> | </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=type&sortType=text&sortOrder=descending">DESC]</a>
                        </th>
                        <th align="center">
                            <xsl:text>Format </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=format&sortType=text&sortOrder=ascending">[ASC</a>
                            <xsl:text> | </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=format&sortType=text&sortOrder=descending">DESC]</a>
                        </th>
                        <th align="center">
                            <xsl:text>File Size </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=size&sortType=text&sortOrder=ascending">[ASC</a>
                            <xsl:text> | </xsl:text>
                            <a class="white" href="{$pageURL}?sortBy=size&sortType=text&sortOrder=descending">DESC]</a>
                        </th>

                        <th align="center">
                            <xsl:text>sortOrder</xsl:text>
                        </th>
                    </tr>

                    <xsl:for-each select="formsdocs/form">

                        <xsl:choose>
                            <xsl:when test="$sortOrder = 'ascending'">
                                <xsl:apply-templates select="$sortBy">
                                    <xsl:sort select="$sortBy" data-type="text" order="ascending" />
                                </xsl:apply-templates>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates select="$sortBy">
                                    <xsl:sort select="$sortBy" data-type="text" order="descending" />
                                </xsl:apply-templates>
                            </xsl:otherwise>
                        </xsl:choose>

                        <tr>
                            <td align="left"><a href="{url}" target="{target}"><xsl:value-of select="title" /></a></td>
                            <td align="left"><xsl:value-of select="dept" />
                            <xsl:choose>
                                <xsl:when test="div != ''">
                                (<xsl:value-of select="div" /> Division)
                                </xsl:when>
                            </xsl:choose>
                            </td>
                            <td align="center"><xsl:value-of select="type" /></td>
                            <td align="center"><xsl:value-of select="format" /></td>
                            <td align="center"><xsl:value-of select="size" />
                            <xsl:choose>
                                <xsl:when test="size = ''">
                                 ---
                                </xsl:when>
                                <xsl:otherwise>
                                 KB
                                </xsl:otherwise>
                            </xsl:choose>
                            </td>

                            <td align="left"><a href="*[name() = $sortOrder]" target="{target}"><xsl:value-of select="title" /></a></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
ASP.NET/VB.NET doc:
Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<%@ import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
'Load XML
Dim xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load("http://DOMAIN/DIRECTORY/FILENAME.xml")

'Load XSL
Dim xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("http://DOMAIN/DIRECTORY/FILENAME.xsl")

'Pull in querystring variables
Dim sortBy_qs = Request.QueryString("sortBy")
Dim sortType_qs = Request.QueryString("sortType")
Dim sortOrder_qs = Request.QueryString("sortOrder")

Dim sortBy
sortBy = xsl.selectSingleNode("//xsl:param[@name='sortBy']")
sortBy.SetAttribute("select", sortBy_qs)

Dim sortType
sortType = xsl.selectSingleNode("//xsl:param[@name='sortType']")
sortType.SetAttribute("select", sortType_qs)

Dim sortOrder
sortOrder = xsl.selectSingleNode("//xsl:param[@name='sortOrder']")
sortOrder.SetAttribute("select", sortOrder_qs)

'Transform file
Response.Write(xml.transformNode(xsl))
%>
KWilliams
 
Old July 20th, 2005, 03:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When you write

<xsl:sort select="$sortBy"/>

then if the value of $sortBy is the string "title", every item will have a sort key of "title", so of course all the sort keys will be equal.

You can't expect the system to know that this string is to be treated as an XPath expression rather than as a literal sort key.
You want select="*[name() = $sortBy]".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 20th, 2005, 03:19 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Michael,

Thanks for the quick response, as it's greatly appreciated. I had actually already tried your suggestion prior to my post, but it didn't change anything. Although the querystring changes properly on whether "ASC" or "DESC" links are clicked, the parameters never get their individual values from the ASP.NET page.

I understand what you're saying about properly declaring the XPath expression, and I've appropriately changed the sort method from <xsl:sort select="$sortBy"... to <xsl:sort select="$sortBy".... But I still am rather stumped as to why this setup isn't working. If you have any other suggestions, please let me know. Thanks.

KWilliams





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
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
Sort Method question rgalehouse Javascript 1 February 10th, 2004 07:03 PM





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