[xsl] Newbie question on XSL transformations: multiple sorts on element attributes

Subject: [xsl] Newbie question on XSL transformations: multiple sorts on element attributes
From: Rob Newman <rlnewman@xxxxxxxx>
Date: Tue, 6 Feb 2007 12:29:31 -0800
Hi there,

I am relatively new to XSL transformations, so I apologize in advance if this question seems ridiculously simple. I have an XML file in the following format (which I cannot control):

<?xml version="1.0" encoding="iso-8859-1"?>
<pfarr name="xyz">
	<pfarr name="dls">
		<pfarr name="abcde">
			<pfstring name="aa">0.010</pfstring>
			<pfstring name="br24">23266128.0</pfstring>
			<pfstring name="dlt">20</pfstring>
			...... (lots of other pfstring elements)
		</pfarr>
	</pfarr>
</pfarr>

XSLT:

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


<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:template match="sources">
	<xsl:element name="dataloggerlist">
		<xsl:for-each select="source">
			<xsl:apply-templates select="/pfarr/pfarr/pfarr" />
		</xsl:for-each>
	</xsl:element>
</xsl:template>

<xsl:template match="pfarr/pfarr/pfarr">
<xsl:element name="datalogger">
<xsl:attribute name="name"><xsl:value-of select="@name"/></ xsl:attribute>
<xsl:apply-templates select="pfstring"/>
</xsl:element>
</xsl:template>


<xsl:template match="pfstring">
<xsl:element name="param">
<xsl:attribute name="id"><xsl:value-of select="@name"/></ xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>


</xsl:stylesheet>

Resulting XML:

<?xml version="1.0" encoding="UTF-8"?>
<dataloggerlist>
	<datalogger name="TA_A04A">
		<param id="aa">0.010</param>
		<param id="br24">23266128.0</param>
		<param id="dlt">20</param>
		.......
	</datalogger>
</dataloggerlist>

This is great and what I want my output to look like. However, I want to sort the XML output on (1) the id = 'dlt' attribute param element, and (2) the datalogger element name attribute. Can I do this in the same XSL file, or do I need to create another XSL file to transform the XML I just created? I have tried several approaches at taking my XML output and applying another XSL transformation (and creating a new XML file), such as:

-- START SNIPPET --
<xsl:template match="dataloggerlist">
<xsl:element name="dataloggerlist">
<xsl:apply-templates select="datalogger">
<xsl:sort select="param/@id{dlt}" data-type="number" order="descending" />
<xsl:sort select="@name" order="ascending" />
</xsl:apply-templates>
</xsl:element>
</xsl:template>
-- END SNIPPET --
(Note: the datalogger template outputs the data same as before)


This sorts the output on datalogger name (50% correct), but not the param element id = 'dlt'. I am sure this is just a simple syntax issue, and I have searched online for examples, but none seem to show sorts on specific element ids. I have Jeni Tennison's 'Beginning XSLT' and have scoured the chapter on Sorting, but I can't seem to figure it out. I would prefer to do this whole process in one step, and avoid having two XSL files that create an interim XML file that just gets transformed again. Any help would be most appreciated.

Thanks in advance,
- Rob

Current Thread