[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Conditional counting

Subject: Re: Conditional counting
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Fri, 12 Oct 2001 10:45:19 +0200
counting rows in xsl
"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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.