Re: [xsl] Conditional counting

Subject: Re: [xsl] Conditional counting
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Fri, 12 Oct 2001 10:45:19 +0200
"Hunsberger, Peter" <Peter.Hunsberger@xxxxxxxxxx> wrote
> I've got an XSLt running under LotusXSLt (Xalan) that conditionally calls
> two different extensions:
>     <table border="1">
>         <xsl:if test="$gunk = 'x''">
>             <xsl:apply-templates select="myExt.method1(*)" mode="x" />
>         </xsl:if>
>         <xsl:if test="$gunk != 'x'">
> 		 <xsl:apply-templates select="myExt.method2(*)" mode="x"/>
> 
>         </xsl:if>
>     </table>
>     <xsl:value-of select="count(tr)"/> rows generated.

Your <xsl:value-of select="count(tr)"/> will count the <tr> child elements
of the context node in the XML source document. As there are likely none,
it'll return zero. If you want to count the generated* <td> elements,
you'll have to take a completely different approach.
There are several possibilities, depending on your exact problem.
If your extension methods produce a node-set with node for each table row,
as yoursecond template suggests, you can try

 <xsl:if test="$gunk = 'x''">
   <xsl:variable name="result" select="myExt.method1(*)"/>
   <table border="1">
     <xsl:apply-templates select="$result" mode="x" />
   </table>
   <xsl:value-of select="count($result)"/> rows generated.
 </xsl:if>
 <xsl:if test="$gunk != 'x'">
   <table border="1">
     <xsl:variable name="result" select="myExt.method2(*)"/>
     <xsl:apply-templates select="$result" mode="x" />
   </table>
   <xsl:value-of select="count($result)"/> rows generated.
 </xsl:if>

Note that you'll have to distribute a lot of stuff into the
conditional statements, because of the way variables are scoped.

Duplicating that much code may look clumsy, so here is another
approach, which unfortunately imposes a portability penalty
because the xx:node-set() function processor specific. This
function converts the RTF stored in the variable $result into
a node set so that you can count the tr elements.
Consult the documentation of your processor about the details.
 
 <xsl:variable name="result" select="myExt.method1(*)"/>
   <xsl:if test="$gunk = 'x''">
     <xsl:apply-templates select="myExt.method1(*)" mode="x"/>
   </xsl:if>
   <xsl:if test="$gunk != 'x'">
     <xsl:apply-templates select="myExt.method2(*)" mode="x"/>
   </xsl:if>
 </xsl:variable>
 <table border="1">
   <xsl:copy-of select="$result"/>
 </table>
 <xsl:value-of select="count(xx:node-set($result)//tr)"/> rows generated.

Be warned this will also count rows of nested tables, you may have to
fiddle with the expression a bit if you experience difficulties
(xx:node-set($result)/tr might work, but i'm not sure).

You may probably want to wrap the "NN rows generated" in a <p> element,
or something like that.

HTH
J.Pietschmann

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


Current Thread