[xsl] trying for: a template that calls apply-templates on its children only until some condition is reached

Subject: [xsl] trying for: a template that calls apply-templates on its children only until some condition is reached
From: Nick Mealy <nick@xxxxxxxxxx>
Date: Wed, 15 Jun 2005 14:16:31 -0700
Hi all,

im trying to do something a little unusual, but which is surprisingly quite hard.

for a given template, i only want it to keep rendering it's children until it's counted a certain number of occurrences of a particlar element (in this case br elements), and then stop applying templates altogether.
As to why I need this, basically there are some rare cases where our system returns xml that's just enormous, so enormous that rendering takes way too long. And since the data coming back is to large to be useful anyway, i want to just render the first few lines and then stop completely.


Maybe this isnt a job for XSLT, but i'm not quite ready to admit that..

I first tried a massively inefficient way:

<xsl:template match="myTroublesomeNode">
  <xsl:for-each select="node()">
      <xsl:if test="count(preceding-sibling::br)&lt;20">
          <xsl:apply-templates select="."/>
      </xsl:if>
  </xsl:for-each>
</xsl:template>

but calling this count(preceding-sibling::br) on every node is a clear case of the cure being worse than the disease.


So... since then I've been trying recursion, but I clearly dont know what im doing cause the parser complains about infinite recursion in the following snippet...


(I have a sense that the way im using my $nextNode param and the preceding/following axes is wrong, but i cant improve on it. )

<xsl:template match="myTroublesomeNode">
  <xsl:call-template name="brCounter">
      <xsl:with-param name="nextNode" select="node()[1]"/>
  </xsl:call-template>
</xsl:template>

<!-- danger, i contain infinite recursion somewhere, so take care -->
<xsl:template name="brCounter">
<xsl:param name="nextNode" />
<xsl:if test="count($nextNode/preceding-sibling::br)&lt;10">
<xsl:apply-templates select="$nextNode"/>
<xsl:call-template name="brCounter">
<xsl:with-param name="nextNode" select="$nextNode/following-sibling::node()[1]" />
</xsl:call-template>
</xsl:if>
<xsl:if test="count($nextNode/preceding-sibling::br)&gt;=10">
I've had all i can stands. i cant stands no more.
</xsl:if> </xsl:template>


Any help or advice would be greatly appreciated... n

Current Thread