|
Subject: Re: [xsl] Find the node with maximum elements From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 04 Nov 2007 08:23:11 -0500 |
The following solution is likely more efficient than Ken's (because, it calculates 'max' only once):
You can optimize the speed by putting the max value into a variable and testing against that
<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>
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" version="2.0">
<xsl:template match="/">
<xsl:variable name="max" select="max(/Sample/*/count(Car))"/>
<xsl:value-of select="/Sample/*[count(Car)=$max]/name(.)"
separator="
"/>
</xsl:template></xsl:stylesheet> T:\ftemp>xslt2 avaneesh.xml avaneesh.xsl con Honda Mitsubishi T:\ftemp>type avaneesh2.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:value-of select="/Sample/*[count(Car)=max(/Sample/*/count(Car))]
/name(.)"
separator="
"/>
</xsl:template></xsl:stylesheet> T:\ftemp>xslt2 avaneesh.xml avaneesh2.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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] Find the node with maximu, Mukul Gandhi | Thread | Re: [xsl] Find the node with maximu, Mukul Gandhi |
| Re: [xsl] Find the node with maximu, Mukul Gandhi | Date | Re: [xsl] Find the node with maximu, Mukul Gandhi |
| Month |