Re: [xsl] Finding the maximun number of nodes

Subject: Re: [xsl] Finding the maximun number of nodes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 9 Jan 2001 10:03:09 +0000
Hi Dimitre,

> To be able to minimise the depth of recursion is something ***very
> important*** when confronted with the reality of some wellknown
> vendours' XSLT processors crashing during deep recursive processing.

Again a good point. The being the case, it looks like solutions that
mixes the non-exponential growth in required processing of recursive
solutions with the shallow recursive processing of non-recursive
solutions would be best.

With an arbitrary node set or a really really huge node set, I think
that restricts us to using the xsl:sort method, e.g.:

<xsl:template name="maxCols">
  <xsl:for-each select="//tr">
    <xsl:sort select="count(td)" order="descending" />
    <xsl:if test="position() = 1">
      <xsl:value-of select="." />
    </xsl:if>
  </xsl:for-each>
</xsl:template>

With a node set based on a axis relationship (like
following-sibling::), then you could also use:

<xsl:template name="maxCols">
  <xsl:apply-templates select="//tr[1]" mode="maxCols" />
</xsl:template>

<xsl:template match="tr" mode="maxCols">
  <xsl:variable name="nCols" select="count(td)" />
  <xsl:variable
    name="next"
    select="following-sibling::tr[count(td) &gt; $nCols][1]" />
  <xsl:choose>
    <xsl:when test="$next">
      <xsl:apply-templates select="$next" mode="maxCols" />
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="$nCols" /></xsl:otherwise>
  </xsl:choose>
</xsl:template>

This works on the same principal as the solution you gave most
recently, but is linear as long as the XSLT processor optimises XPaths
that end in [1] (ref the setting of the $next variable), and of course
depends on being able to navigate the source node tree to move from
one node to the next and thus won't work on arbitrary node sets.

I dunno - so much seems to depend on how the individual processor does
something, whether it's implemented to crash with deep recursion or
not, whether it sorts in a sensible way and so on, that the only
really safe guideline is "Suck it and See".

Cheers,

Jeni

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



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


Current Thread