This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: To simulate SORT inside a xsl:choose condition
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 30 Apr 2002 08:47:01 +0100
- Subject: RE: [xsl] To simulate SORT inside a xsl:choose condition
- Reply-to: xsl-list at lists dot mulberrytech dot com
The most powerful solutions unfortunately require extensions.
There's xx:evaluate():
<xsl:for-each select=".....">
<xsl:sort select="xx:evaluate($sort-key)"/>
and there's stylesheet-defined functions (here in XSLT 2.0 syntax):
<xsl:for-each select=".....">
<xsl:sort select="xx:my-function($sort-key)"/>
...
<xsl:function name="xx:my-function">
<xsl:param name="sort-key"/>
...
Within XSLT 1.0 though there are a number of tricks that can be useful.
There's the exclusive union trick:
<xsl:sort select="key1[condition1] | key2[condition2] | key3[condition3]"/>
where only one of the conditions is actually true.
There's also the dynamic name trick:
<xsl:sort select="*[name()=$param]"/>
and of course if the worst comes to the worst you can always write
<xsl:choose>
<xsl:when test="condition1">
<xsl:for-each select="$x">
<xsl:sort select="sort-key-1"/>
<xsl:call-template name="the-work"/>
</xsl:for-each>
</xsl:when>
<xsl:when test="condition2">
<xsl:for-each select="$x">
<xsl:sort select="sort-key-2"/>
<xsl:call-template name="the-work"/>
</xsl:for-each>
</xsl:when>
...
XSLT 2.0 also introduces a sort() function that takes a named sort key as a
parameter, which can be determined at run-time:
<xsl:for-each select="sort($x, if (condition1) then 'sortkey1' else
'sortkey2')">
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of KUMAR
> NINGASHETTY
> Sent: 29 April 2002 23:15
> To: <
> Subject: [xsl] To simulate SORT inside a xsl:choose condition
>
>
> Hi all ,
>
> I know that the rule to sort should be immediately after
> <xsl:for-each >
> I am in a situation where i have to read the criteria to
> sort from some some node in the beginning of XML
> and based on which i need to make a decision to sortBY
> corresponding node somewhere down the line....
>
>
> But as you know the following way doesnt work ...
>
> <xsl:for-each select="somenode">
> <xsl:choose>
> <xsl;when test="$prevnode = 'criteriavalue' ">
> <xsl:sort order="ascending"
> select="node_to_be_sorted"/>
> </xsl:when>
> </xsl:choose>
>
> </xsl:for-each>
>
> Note: dont worry about syntax check on this ...
>
> Is there a way to sneak around and get this Sorting to work
> based on some criteria ...Or any other work arounds, suggestions?
>
> thanx in advance
> - kumar
>
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list