brainless
brainless

Reputation: 5819

XSLT control structures!

how to find greatest 2 numbers out of 3 numbers. like in following example.

<root>
    <num>10</num>
    <num>12</num>
    <num>8</num>
</root>

for the above code xslt should display "10 12"

help me to do it.

Thanks in advance!!!

Upvotes: 1

Views: 392

Answers (1)

Oded
Oded

Reputation: 499212

This will work:

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

<xsl:template match="/">
  <xsl:apply-templates select="root/num">
    <xsl:sort select="." data-type="number" order="descending" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="num">
 <xsl:if test="position() != last()">
  <xsl:value-of select="." /><xsl:text> </xsl:text>
 </xsl:if>
</xsl:template>

</xsl:stylesheet>

It sorts the numbers in descending order, and the "num" template only outputs when it is not the last node.

Upvotes: 2

Related Questions