[xsl] Counting Nodes Based on Conditional Statements

Subject: [xsl] Counting Nodes Based on Conditional Statements
From: Michael <msell@xxxxxxxxxxxxxxx>
Date: Sat, 30 Oct 2004 21:32:30 +1000
I must have a stunted ability to search throughout the archives, because once again I can't find anything specific to solve my problem that should be common (sigh).

Anyway, I have a stylesheet that displays book titles depending on some parameters passed to it. As a result, a dynamic number of books are shown. I need to count the number of the book titles that are shown, and display the result accordingly. If no book titles are shown at all, I need to be able to display message in their absence. Only containing a cursory knowledge of variables in XSLT, i'm not yet skilled enough to handle this problem on my own. Some help would be great.

An example of the xml document follows:

 <category name="Health &amp; Fitness">
     <book status="Purchased">
       <title>Strength Training Anatomy</title>
       <sub-title>Your Illustrated Guide to Muscles at Work</sub-title>
       <author>Fridiric Delavier</author>
       <isbn>0736041850</isbn>
       <publisher>Human Kinetics Publishers</publisher>
       <date>September, 2001</date>
       <price>13.27</price>
     </book>
   </topic>
   <topic name="Nutrition">
     <book status="Purchased">
       <category>Health &amp; Fitness</category>
       <title>Nutrition For Dummies</title>
       <author>Carol Ann Rinzler</author>
       <isbn>0764540823</isbn>
       <publisher>Wiley Publishing</publisher>
       <date>November, 2003</date>
       <price>15.39</price>
     </book>
     <book status="Purchased">
       <title>Encyclopedia of Sports &amp; Fitness Nutrition</title>
       <author>Liz Applegate</author>
       <isbn>0761513787</isbn>
       <publisher>Prima Publishing</publisher>
       <date>December, 2002</date>
       <price>16.07</price>
     </book>
     <book status="Purchased">
       <title>Nancy Clark's Sports Nutrition Guidebook</title>
       <author>Nancy Clark</author>
       <isbn>073604602X</isbn>
       <publisher>Human Kinetics Publishers</publisher>
       <date>August, 2003</date>
       <price>13.27</price>
     </book>
   </topic>
 </category>

The stylesheet code follows:

 <xsl:param name="category" />
 <xsl:param name="topic" />
 <xsl:param name="status" />

 <xsl:template match="/">
   <div class="line"></div>

<xsl:for-each select="catalog/category/topic/book">
<xsl:sort select="title" data-type="text" order="ascending" />
<xsl:if test="$status = 'All' or ($status = 'Purchased' and @status = 'Purchased')">
<xsl:if test="$category = 'All' or ($category = ../../@name and ($topic = ../@name or $topic = 'All'))">
<div><xsl:value-of select="title" /></div>
<div class="line"></div>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>


</xsl:stylesheet>

Current Thread