Re: General counting question: finding max

Subject: Re: General counting question: finding max
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 15 Aug 2000 19:51:25 +0100
Gert,

>I would like to find the length of the longest chain of <x>'s in the
>following document:
>
><a><x/<x/></a>
><a><x/></a>
><a><x/><x/><x/><x/></a>

One way that you could do this is to do a xsl:for-each on each of the 'a'
elements, sort them in order of the number of 'x' element children then
have (in descending order - biggest first), and then take the first one of
that list - that's the 'a' that you're interested in.  So:

<xsl:for-each select="a">
  <xsl:sort select="count(x)" order="descending" />
  <xsl:if test="position() = 1">
    <!-- this is the 'a' you were after -->
    Max number of 'x's = <xsl:value-of select="count(x)" />
  </xsl:if>
</xsl:for-each>

If this will work in your situation, I *think* it's the best method.  If it
won't, there are other ways of doing it, just about (I can think of two -
selecting the 'a' that does not have a sibling with more 'x' children, and
using keys - but I haven't tried them out).  Let me know if you want to see
them as well.

I hope this solution works for you,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread