Wrox Programmer Forums
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 17th, 2008, 10:18 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default count

Hello,

Thanks for your help.

I'd like to count how many <entry> nodes are present, whose <language> nodes have the attribute lang='DE'. I made some tests with

<xsl:if test="header/language[@lang='DE']">
entries: <xsl:value-of select="count(header/language/entry" />
</xsl:if>

but I get a line for each node. I'd like to have just one line, like:

entries : 2

Here is the data:

<data>

<header>
<language lang='DE' />
<entry>entry</entry>
</header>

<header>
<language lang='DE' />
<entry>entry 2</entry>
</header>

</data>

 
Old February 17th, 2008, 11:02 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is not clear where the problem is.
Your code sample seems to lack the predicate [@lang='DE'] on the count expression e.g. you need
Code:
<xsl:value-of select="count(header/language[@lang = 'DE']/entry)"/>
 
Old February 17th, 2008, 11:12 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

Sorry for not being clear enough. I'm trying to count how many <entry> there are with the lang attribute of <language> set to 'DE'.

I already tried
<xsl:value-of select="count(header/language[@lang = 'DE']/entry)"/>

but I get 0 as a result, although there should be 2.

Thanks for your help

 
Old February 17th, 2008, 11:18 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

maybe there is a problem because

<language> and <entry> are on the same level ? I had a look at Xpath, but even with a ./ it's still the same

thanks

 
Old February 17th, 2008, 11:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
<xsl:value-of select="count(header/language[@lang = 'DE']/following-sibling::entry)"/>

--

Joe (Microsoft MVP - XML)
 
Old February 17th, 2008, 11:33 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Joe, you're the man! It worked.

Have a nice day

 
Old February 17th, 2008, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Should be
Code:
<xsl:value-of select="count(header[language/@lang = 'DE']/entry)"/>
 
Old February 17th, 2008, 03:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've been given an answer, but I hope you have also understood where you were going wrong. It looks to me as if you were thinking along the lines "process all the records, and if the record matches my criteria, then count it." That's record-at-a-time thinking, and you've got to change your mindset to think in terms of processing sets of records a a time (or elements or nodes or whatever).

The count() function tells you how many items there are in a supplied set of items; so you need an expression that selects the set of items of interest, and then apply the count() function to the result. In your case the set of interest is a subset of all the entry nodes, specifically that subset for which language/@lang='DE'. You could translate this literally as

count(//entry[../language/@lang='DE'])

but if there's a one-to-one correpondence between entry elements and @lang attributes then you get the same answer with

count(//@lang[.='DE'])

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
is there any in built function to count page count g.tamilselvan MySQL 1 February 15th, 2006 07:43 AM
Need Count Help ~Bean~ SQL Language 11 August 9th, 2005 11:22 PM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
count() crmpicco MySQL 1 January 28th, 2005 01:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.