RE: [xsl] Find the node with maximum elements

Subject: RE: [xsl] Find the node with maximum elements
From: "Bjorndahl, Brad" <brad.bjorndahl@xxxxxxxxxxxxxxxx>
Date: Mon, 5 Nov 2007 15:02:09 -0500

I agree - a nice solution.

But the solution would be even nicer if we had more access to the key.

Once we have the key we can select all elements that have 0 cars, or 1 car, or
6 cars, but not a run-time function of the key use value such as what we want:
the largest value of 'use' in this document. So we do what is shown below -
scan through the elements to find the maximum value even though the
information is _already in the key_. (I doubt that I have the terminology

I would like something like:
<xsl:for-each select="key('cars', max())">
<xsl:for-each select="key('cars', sort(ascending))">

or if 'use' is alpha, something like:
<xsl:for-each select="key('carnames', not(contains('xyz'))">

Is this a good addition for XSLT 2.1? It seems to me this allows both
simplified and more efficient code.


-----Original Message-----
From: Mukul Gandhi [mailto:gandhi.mukul@xxxxxxxxx]
Sent: November 5, 2007 1:02 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Find the node with maximum elements

Nice solution, Scott.

On 11/5/07, Scott Trenda <Scott.Trenda@xxxxxxxx> wrote:
> This may be better represented with 2.0's grouping facilities, but
> here's the 1.0 solution I'd use to alleviate Mukul's concerns
> (building off Michael's response with <xsl:sort/>):
> <xsl:stylesheet version="1.0"
> xmlns:xsl="";>
>  <xsl:key name="cars" match="*[Car]" use="count(Car)"/>
>  <xsl:template match="Sample">
>    <xsl:variable name="max-cars">
>      <xsl:for-each select=".//*[Car]">
>        <xsl:sort select="count(Car)" data-type="number"/>
>        <xsl:if test="position() = last()">
>          <xsl:value-of select="count(Car)"/>
>        </xsl:if>
>      </xsl:for-each>
>    </xsl:variable>
>    <xsl:for-each select="key('cars', $max-cars)">
>      <!-- do whatever you'd do with the results here -->
>      <xsl:value-of select="name()"/>
>    </xsl:for-each>
>  </xsl:template>
> </xsl:stylesheet>
> ~ Scott

Mukul Gandhi

Current Thread