Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 April 4th, 2010, 01:34 PM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to count nodes in target output in xslt

Here's my input file

<header>
<detail/>
<detail/>
<detail/>
</header>

Here's my xslt file

<xsl:template match="/">
<book>
<xsl:for-each select="header/detail">
<chapter/>
</xsl:for-each>
<count><xsl:value-of select="count(//chapter)"/></count>
</book>
</xsl:template>

So the output file will look like this

<book>
<chapter/>
<chapter/>
<chapter/>
<count>3</count>
</book>

But I always get <count>0.0E0</count> instead of <count>3</count>

I want to count <chapter> instead of <detail> node because the number of <chapter> will be different. For simplicity's sake, I just do a 1-1 mapping in my example above.

Do you have any ideas?

Thanks
 
Old April 4th, 2010, 04:02 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well the easiest way is to work out how you determine how may chapter items there are going to be and count them. As your example doesn't illustrate your real input XML there is no way we can help you do this.

If you are using XSLT 2.0 then you could store the output in a temporary variable, and then count against that.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 4th, 2010, 07:27 PM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In my XSL file, I will do a lot of group-by against the input file inside a for-each loop therefore the number of <chapter> nodes will be different each time so there's no way for me to determine how many <chapter> will come out each time.

I'm using XSLT 2. Can you show me how to store output in a temporary variable please? Thanks
 
Old April 5th, 2010, 03:25 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

You can store the <chapter> in a variable and count like below:
Code:
<xsl:template match="/">
<book>
<xsl:variable name="detail">
<xsl:for-each select="header/detail">
<chapter/>
</xsl:for-each>
</xsl:variable>
<count><xsl:value-of select="count($detail//chapter)"/></count>
</book>
</xsl:template>
If you could post a relevant part of your input xml and xslt, then someone here can give you a good idea.
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Count Left Nodes Manoj Bisht SQL Language 9 April 9th, 2009 02:40 AM
Counting nodes on output bonekrusher XSLT 6 February 26th, 2007 08:19 PM
Using axis instead of count? Counting child nodes. dep XSLT 1 January 17th, 2007 11:27 AM
Count nodes based of if condition suri_1811 XSLT 1 December 7th, 2006 08:00 PM
count distinct nodes alexshiell XSLT 2 January 27th, 2005 11:19 AM





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