This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Attribute-based XML Sorting using XSL
- From: Matt Jones <xsl-list at mattjones dot org>
- To: XSL-List at lists dot mulberrytech dot com
- Date: Mon, 17 Jun 2002 12:41:15 -0700
- Subject: [xsl] Attribute-based XML Sorting using XSL
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hello,
I have an XML file which rather generically describes the information to
display in an HTML table, like so:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="attribute-sort.xsl" ?>
<ROWSET>
<ROW>
<COLUMN name="Col1" value="4-1" />
<COLUMN name="Col2" value="4-2" />
<COLUMN name="Col3" value="4-3" />
</ROW>
<ROW>
<COLUMN name="Col1" value="1-1" />
<COLUMN name="Col2" value="1-2" />
<COLUMN name="Col3" value="1-3" />
</ROW>
<ROW>
<COLUMN name="Col1" value="3-1" />
<COLUMN name="Col2" value="3-2" />
<COLUMN name="Col3" value="3-3" />
</ROW>
<ROW>
<COLUMN name="Col1" value="2-1" />
<COLUMN name="Col2" value="2-2" />
<COLUMN name="Col3" value="2-3" />
</ROW>
</ROWSET>
And an XSL file which creates an HTML table, like so:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML><BODY>
<xsl:apply-templates select="ROWSET"/>
</BODY></HTML>
</xsl:template>
<xsl:template match="ROWSET">
<TABLE border="1" cellspacing="0" cellpadding="0">
<TR STYLE="font-size:x-small;">
<xsl:for-each select="ROW[position()=1]/COLUMN">
<xsl:value-of select="@name"/>
</xsl:for-each>
</TR>
<xsl:apply-templates select="ROW">
<xsl:sort select="COLUMN[@value]"/>
</xsl:apply-templates>
</TABLE>
</xsl:template>
<xsl:template match="ROW">
<!-- Output rows of the table -->
<TR STYLE="font-size:x-small;">
<xsl:for-each select="COLUMN">
<TD valign="top"><xsl:apply-templates
select="@value"/> </TD>
</xsl:for-each>
</TR>
</xsl:template>
</xsl:stylesheet>
My problem is that I'd like to be able to sort on a column name. That
is to say, I'd like to be able to sort the ROWs in the order of the
@value attribute for the COLUMN which has a matching @name attribute.
This is rather difficult to explain--think about a list control where
you can click any of the headers and sort the entire list based upon
that column.
To keep this as generic as possible, I don't really want to change the
XML, only the XSL.
Any help would be greatly appreciated.
Peace,
Matt
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list