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

Re: Counting elements with count() and position()

Subject: Re: Counting elements with count() and position()
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 10 Jul 2003 14:05:31 +0100
xsl variable count
Hi Rudi,

> The position() function is working well for me however I think
> I'm positioning the count() function incorectly.

You're setting the $addresscount variable within one template and then
trying to use it in another template. When a variable is declared
locally (in a template), it's only in scope for the following siblings
of the variable declaration and their descendants; it isn't in-scope
in other templates.

You need to do one of the following:

1. Use the last() function in your address template to give you the
count of the number of nodes "currently being processed". Since all
the nodes are <address> elements, this will give you the count that
you need:

<xsl:template match="address">
  <tr>
    <xsl:choose>
      <xsl:when test="position() = 1">
        <td class="title">
          <xsl:choose>
            <xsl:when test="last() > 1">urls:</xsl:when>
            <xsl:otherwise>url:</xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:when>
      ...
    </xsl:choose>
    ...
  </tr>
</xsl:template>

2. Make the variable global, so that it's in-scope everywhere. At the
top level of the stylesheet, add:

<xsl:variable name="addresscount"
              select="count(/webapps/website/address)" />

3. Calculate the value for the variable within the address template:

<xsl:template match="address">
  <xsl:variable name="addresscount" select="count(../address)" />
  ...
</xsl:template>

4. Pass the value of $addresscount as a parameter into the address
template. The address template needs to declare the parameter:

<xsl:template match="address">
  <xsl:param name="addresscount" />
  ...
</xsl:template>

and you need to pass it when you apply templates to the <address>
elements:

  <xsl:apply-templates select="address">
    <xsl:with-param name="addresscount" select="$addresscount" />
  </xsl:apply-templates>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 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.