xsl-list
[Top] [All Lists]

RE: [xsl] Find the node with maximum elements

2007-11-05 13:03:09
Hi,

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 correct.)

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.

Brad.


-----Original Message-----
From: Mukul Gandhi [mailto:gandhi(_dot_)mukul(_at_)gmail(_dot_)com]
Sent: November 5, 2007 1:02 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Find the node with maximum elements

Nice solution, Scott.

On 11/5/07, Scott Trenda <Scott(_dot_)Trenda(_at_)oati(_dot_)net> 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="http://www.w3.org/1999/XSL/Transform";>

 <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


--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>