[xsl] XSL:SORT with Variable in 'select'

Subject: [xsl] XSL:SORT with Variable in 'select'
From: "Powell, Todd" <Todd.Powell@xxxxxxxxxx>
Date: Thu, 15 Mar 2001 10:12:59 -0500
I'm fairly new to XSLT and am not sure if the behavior I'm seeing (or lack
thereof) is expected/desired.  Basically, I have set an attribute
'SortOrder' in my XML's root node (<Root>) equal to the name of the
attribute on the <User> element that I want to sort the list of users by
(see XML below):

<Root SortOrder="@Name">
	<User LoginID="Powell" Name="Todd Powell"/>
	<User LoginID="Dahlman" Name="Becky Dahlman"/>
	<User LoginID="Wells" Name="Fritz Wells"/>
	<User LoginID="Fitch" Name="Karen Fitch"/>
	<User LoginID="Murphy" Name="Pat Murphy"/>
</Root>

In the case above, the resulting list of users should be sorted by the
values in the Name attribute of the <User> element.  I'm using the following
XSLT to attempt this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:template match="Root">
	<!--	get the value in the SortOrder attribute
		(on the Root node) into $SortOrder variable
	-->
	<xsl:variable name="SortOrder" select="@SortOrder"/>

	<table border="1">

		<!--	THIS CODE SORTS THE User NODES BY THE LoginID
			or Name ATTRIBUTE AND PROCESSES THEM
		-->

<!-- this code doesn't work, but it seems like it should! It just ignores
the sort -->
		<xsl:apply-templates select="User">
			<xsl:sort select="$SortOrder"/>
		</xsl:apply-templates>

<!-- have to do this instead to get sorting to work -->
<!--
		<xsl:choose>
			<xsl:when test="@SortOrder[.='@LoginID']">
				<xsl:apply-templates select="User">
					<xsl:sort select="@LoginID"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="User">
					<xsl:sort select="@Name"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
-->
	</table>
</xsl:template>

<xsl:template match="User">
	<tr>
		<td nowrap="yes"><xsl:value-of select="@LoginID"/></td>
		<td nowrap="yes"><xsl:value-of select="@Name"/></td>
	</tr>
</xsl:template>

</xsl:stylesheet>

I'm using Microsoft's MSXML3.DLL (the production release) and IE 5.5 SP1.
There seem to have been numerous questions about trying to use variables in
xsl:sort commands, but none I have read have talked exactly to the
circumstances I'm in.  I've seen the notion of using something like:
<xsl:sort select="*[name()=$s]"/>
to sort by a variable on the name of an immediate child element, but what do
you do when you want to sort on an attribute of the current node?  Can you
do:
<xsl:sort select="@[name()=$s]"/>

Is there another parser that can do this?  I'm trying to keep the values in
attributes, but I could move them to children elements if I have to.  Thanks
in advance for any help!

-- Todd Powell

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Todd S. Powell
SOZA - Senior Systems Analyst

Office of Nuclear and National Security Information
U.S. Department of Energy
19901 Germantown Road
Mailstop SO-22
Germantown, MD  20874

Phone: (301) 903-9043	Fax: (301) 903-5186
mailto:todd.powell@xxxxxxxxxx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread