[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: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 03 Nov 2007 15:19:45 -0400
Re:  Find the node with maximum elements
At 2007-11-03 12:05 -0700, Avaneesh Ramprasad wrote:
I have a requirement to write a xsl transformation to find the node which has the maximum number of elements
...
The xsl should return Honda and Mitsubishi

Not sure where you are having the problem, but XSLT 2 provides the max() function that makes this very easy.


I hope the solution below helps. You can optimize the speed by putting the max value into a variable and testing against that, but I just wrote the XPath as I said the requirement aloud, so this is what I ended up with.

. . . . . . Ken

T:\ftemp>type avaneesh.xml
<Sample>
    <Toyota>
          <Car>Camry</Car>
          <Car>Corrola</Car>
     </Toyota>
     <Honda>
            <Car>Accord></Car>
          <Car>Civic</Car>
          <Car>Pilot</Car>
     </Honda>
     <Mitsubishi>
        <Car>Lancer</Car>
        <Car>Lancer</Car>
        <Car>Lancer</Car>
     </Mitsubishi>
    <Hyundai>
        <Car>Sonata</Car>
        <Car>Accent</Car>
</Hyundai>
</Sample>

T:\ftemp>type avaneesh.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                exclude-result-prefixes="xsd"
                version="2.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="/Sample/*[count(Car)=max(/Sample/*/count(Car))]">
    <xsl:value-of select="name(.)"/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>xslt2 avaneesh.xml avaneesh.xsl con
Honda
Mitsubishi

T:\ftemp>

--
Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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

Buy Stylus Studio Now

Cast Your Vote

We need your help – Vote for DataDirect XML Products!

  • Best SOA or XML site

Winners and finalists announced at SOA World Conference in November.

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.