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

Counting elements with count() and position()

Subject: Counting elements with count() and position()
From: "Rudi Starcevic" <rudi@xxxxxxxxxxxx>
Date: Thu, 10 Jul 2003 22:39:33 +1000
xsl count elements
Hi,

I'm using trying to use the function's position() and count()
to help format some HTML output.

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

This what I started with:

<tr><td>url:</td><td>www.footyworld.com</td></tr>
<tr><td>url:</td><td>www.footyworld.com.au</td></tr>

I've used position() to take the next formating step:

<tr><td>url:</td><td>www.footyworld.com</td></tr>
<tr><td></td><td>www.footyworld.com.au</td></tr>

Now I what to use count() to add a plural 's' to my formatting so it's like:

<tr><td>urls:</td><td>www.footyworld.com</td></tr>
<tr><td></td><td>www.footyworld.com.au</td></tr>

This last formating step throws an error.

I'm setting an xsl variable, $addresscount, straight after an 
<xsl:for-each> tag and trying to use the value in an <xsl:template> tag.
My thinking is the $addresscount var. is reset each <website> node.

Can you help correct me with this?

Below is a trimmed down version of the xml and xsl files.
As I've trimmed it down in this email I have checked for typo's and
I trust their are none.
I've been trying different positions with these tags without joy so I though
I'd ask.

Many thanks
Cheers
Rudi.


XML file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xlink="http://www.w3.org/XML/XLink/0.9">

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="webapps2.xsl"?>
<webapps xmlns:xlink="http://www.w3.org/XML/XLink/0.9">
<website name="Footyworld">

<address
 xlink:type="simple"
 xlink:href="http://www.footyworld.com">www.footyworld.com</address>

<address
 xlink:type="simple"
 xlink:href="http://www.footyworld.com.au">www.footyworld.com.au</address>

</website>	
</webapps>

XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xlink="http://www.w3.org/XML/XLink/0.9">

<xsl:template match="/">
<html>
<head><title>Web app. list</title></head>
<body>

<xsl:for-each select="webapps/website">
	
<xsl:variable name="addresscount" select="count(address)"/>
				
<table border="1">
<xsl:apply-templates select="address"/>		
</table>
</xsl:for-each>
	
</body>
</html>
</xsl:template>

<xsl:template match="address">
<tr>
<xsl:choose>

<xsl:when test="position() = 1">

<td class="title">
 <xsl:choose>
 <xsl:when test="$addresscount > 1">urls:</xsl:when>
 <xsl:otherwise>url:</xsl:otherwise>
 </xsl:choose>
</td>			

</xsl:when>

<xsl:otherwise>
<td></td>
</xsl:otherwise>

</xsl:choose>

<td><a href="{@xlink:href}"><xsl:value-of select="."/></a></td>
</tr>
</xsl:template>

</xsl:stylesheet>


 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.