Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 13th, 2006, 05:59 PM
Registered User
 
Join Date: Jan 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting xsl:variable maximum count

Considering the sample xml below, I want to initialise an xsl:variable with the maximum number of c-child nodes any of the b-nodes have (i.e. in the sample below, I would like the variable to be set to 3):

<a>
  <b>
    <c>1</c>
    <c>2</c>
  </b>
  <b>
    <c>7785</c>
  </b>
  <b>
    <c>100</c>
    <c>101</c>
    <c>102</c>
  </b>
</a>

Any help is greatly appreciated.

 
Old March 13th, 2006, 06:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

With XPath 2.0, it's easy:

(with <a> as the context node)

max(b/count(c))

With 1.0 as always it's more tortuous. The usual way to do max/min in 1.0 is to sort and take the first/last:

<xsl:variable name="max">
  <xsl:for-each select="b"/>
    <xsl:sort select="count(c)" data-type="number"/>
    <xsl:if test="position()=last()">
      <xsl:value-of select="count(c)"/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 14th, 2006, 03:39 AM
Registered User
 
Join Date: Jan 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael, now I'm really eager to go to XPath 2.0!





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
xsl:variable holding name of an xsl:param perissos XSLT 0 December 5th, 2006 07:09 AM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM
Maximum Length for XSL variable vlad027 XSLT 4 January 29th, 2004 12:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.