Re: [xsl] Counting Nodes Based on Conditional Statements

Subject: Re: [xsl] Counting Nodes Based on Conditional Statements
From: Geert Josten <Geert.Josten@xxxxxxxxxxx>
Date: Sat, 30 Oct 2004 17:36:35 +0200
Hi Michael,

 <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>

You could _first_ determine the list catching it in a variable and _then_ get the count and loop over them.


<xsl:variable
    name="books"
    select="catalog/category/topic/book
                [$status = 'All'
                  or ($status = 'Purchased' and @status = 'Purchased')]
                [$category = 'All'
                  or ($category = ../../@name and ($topic = ../@name or $topic = 'All'))]" />

(sorry for the silly indentation)

<xsl:choose>
  <xsl:when test="count($books) = 0">
    <!-- sorry no books -->
  </xsl:when>
  <xsl:otherwise>
    <!-- count($books) shown! -->
    <xsl:for-each select="$books">
      <!-- presentation of books -->
    </xsl:for-each>
  </xsl:otherwise>
</xsl:choose>

Grtz,
Geert

Current Thread