[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Find the node with maximum elements

Subject: Re: Find the node with maximum elements
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 4 Nov 2007 18:15:45 +0530
Re:  Find the node with maximum elements
Hi Mike,
  Thanks for your observations.

The following solution is likely more efficient than Ken's (because,
it calculates 'max' only once):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

 <xsl:output method="text" />

 <xsl:template match="/Sample">
   <xsl:variable name="max-count" select="max(*/count(Car))" />
   <xsl:for-each select="*">
     <xsl:if test="count(Car) = $max-count">
       <xsl:value-of select="local-name()" /><xsl:text> </xsl:text>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>


On 11/4/07, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> Yes, I read the requirement "return the node", not the example "return Honda
> and Mitsubishi".
>
> The solution *[count(*) = max(current()/*/count(*))] is easy to write, but
> it's very dependent on optimization. Saxon will move the condition
> max(current()/*/count(*)) out of the loop if it's written this way, but not
> if it's written *[count(*) = max(../*/count(*))]. Even if the max() is
> calculated outside the loop, you're visiting each node twice and calculating
> the "key" (count(*) twice for each node. Hence the slight preference for the
> sorting approach.
>
> The most efficient solution is probably a recursive function, but that's not
> the easiest to write. It really calls out for a higher-order function along
> the lines of saxon:highest().
>
> Michael Kay
> http://www.saxonica.com/


-- 
Regards,
Mukul Gandhi

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.