XSL - conditional sort

A

Andy Chambers

Hi,

I'd like to sort a nodelist using a sort key or just leave the
nodelist in document order if any of the nodes lack the specified sort
key. Is it possible to do this using just the select attribute on an
xsl:sort?

For example given the following input....

Example A:
<nodes>
<node rank="3">c</node>
<node rank="">a</node>
<node rank="2">b</node>
</nodes>

Example B:
<nodes>
<node rank="3">c</node>
<node rank="1">a</node>
<node rank="2">b</node>
</nodes>

For the input in Example A, I'd want to process the nodes in document
order. For the input in Example B, I'd want to process the nodes
according to their @rank attribute. What's the best way to acheive
this? Can it (or should it) be done without resorting to a xsl:choose
that checks whether all the @rank attributes have been set before
sorting?

Many Thanks,
Andy
 
M

Martin Honnen

Andy said:
I'd like to sort a nodelist using a sort key or just leave the
nodelist in document order if any of the nodes lack the specified sort
key. Is it possible to do this using just the select attribute on an
xsl:sort?

I think you need an xsl:choose e.g.
<xsl:choose>
<xsl:when test="/nodes/node/@rank[not(normalize-space())]">
<xsl:apply-templates select="/nodes/node"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates select="/nodes/node">
<xsl:sort select="@rank" data-type="number"/>
</xsl:apply-templates>
</xsl:eek:therwise>
</xsl:choose>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top