This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
How to define a xsl:sort's order attribute using a variable
- To: "'xsl-list at lists dot mulberrytech dot com '" <xsl-list at lists dot mulberrytech dot com>
- Subject: [xsl] How to define a xsl:sort's order attribute using a variable
- From: Mike McGraw <mmcgraw at 724 dot com>
- Date: Wed, 27 Jun 2001 13:31:09 -0400
- Reply-To: xsl-list at lists dot mulberrytech dot com
I want to sort a list of <person> elements, and define the "select" and
"order" attributes of the xsl:sort element using variables, rather than
hardcoded values.
xml source:
<personlist sortBy="name" sortOrder="descending">
<person>
<name>Bob</name>
<age>40</age>
</person>
<person>
<name>Mary</name>
<age>53</age>
</person>
<person>
<name>Arthur</name>
<age>22</age>
</person>
</personlist>
Here's an example of what I want to do. Iterate through the list of <person>
elements, and sort by the element whose names matches personlist/@sortBy, in
the order defined by personlist/@sortOrder:
<xsl:template match="personlist">
<xsl:variable name="sortBy">
<xsl:attribute name="value">
<xsl:value-of select="@sortBy">
</xsl:attribute>
</xsl:variable>
<xsl:variable name="sortOrder">
<xsl:attribute name="value">
<xsl:value-of select="@sortOrder">
</xsl:attribute>
</xsl:variable>
<xsl:for-each select="person/*[name()=$sortBy]">
<xsl:sort select="." order="$sortOrder"/>
<p>
<xsl:value-of select="../name"/> 
<xsl:value-of select="../age"/>
</p>
</xsl:for-each>
</xsl:template>
.. This does not work. I cannot define the value of the order attribute
using a variable. Is there any way I can do this?
Mike
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list