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

Re: Hello - how do I use the count function properly?

Subject: Re: Hello - how do I use the count function properly?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 17 Sep 2010 22:12:26 +0100
Re:  Hello - how do I use the count function properly?
And on every 4th subcategory I just want to set the margin-
right: 0px.

Here is the XSL and my attempt.

Please guide me.

  <xsl:template match="category">
   <xsl:for-each select="child::category">
    <xsl:choose>
     <xsl:when test="count(/child::category)=4 or
count(/child::category)=8">

There are several significant problems with this code.


First, xsl:for-each sets the context node, and any path expression immediately within the for-each selects using that context node as its start point. If you wrote "child::category" or "./child::category", you would be testing whether the category node you are currently processing has 4 (or 8) children called category, whereas you actually want to test whether it has 3 (or 7) preceding siblings called category.

Secondly, by writing /child:category, you are not selecting children of the context node, but children of the root (document) node at the top of the tree. (A document node in a well-formed XML document has exactly one element child, so testing whether it has 4 or 8 children will always return false.)

You could write this as

<xsl:for-each select="child::category">
<xsl:choose>
<xsl:when test="count(preceding-sibling::category) = 3 or count(preceding-sibling::category) = 7"


but I would tend to write it as

<xsl:for-each select="child::category">
<xsl:choose>
<xsl:when test="position() mod 4 = 0" ...

The position() function returns 1, 2, 3, ,,, in successive executions of the xsl:for-each body.

Michael Kay
Saxonica

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.