Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 21st, 2006, 12:21 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Node counting

Hello everyone -

Here's the basic format of what I'm trying to translate:

Code:
<PartyList>
<Count>3</Count>
<Party>
<UniqueID>1</UniqueID>
<Type>FRIEND</Type>
<First>Sam</First>
<Last>Johnson</Last>
</Party>
<Party>
<UniqueID>2</UniqueID>
<Type>FRIEND</Type>
<FirstName>Jean</FirstName>
<LastName>Miller</LastName>
</Party>
<Party>
<UniqueID>3</UniqueID>
<Type>EMPLOYER</Type>
<FirstName>Jack</FirstName>
<LastName>Summers</LastName>
</Party>
</PartyList>
I've been trying to create an xslt template that will count the instances where the <Type> is 'FRIEND'

This is one of the many, many things I tried:

Code:
<xsl:template match='*'>
<xsl:for-each select='.'>
<xsl:call-template name='friend_test'>
<xsl:with-param name='test' select='.'/>
</xsl:call-template>
</xsl:for-each> 
</xsl:template>
<xsl:template name='friend_test'>
<xsl:param name='test' select='.'/>
<xsl:value-of select='count([Type="FRIEND"])'/>
</xsl:template>
I know that I'm way off - this creates an XPATH error when I try to use it. Any guidance would be greatly appreciated.
Thanks in advance -
 
Old February 21st, 2006, 01:39 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:template match="PartyList">
  <xsl:value-of select="count(Party[Type='FRIEND'])"/>
</xsl:template>

There are a number of weaknesses in your code:

<xsl:for-each select='.'>
  -- this is not wrong, but it's silly, because it does nothing

<xsl:call-template name='friend_test'>
  <xsl:with-param name='test' select='.'/>
  -- the context node is an implicit parameter to a named template, there's no need to make it an explicit parameter. Also, when supplying a node to a template, it's often better to make it a match template rather than a named template.


<xsl:template name='friend_test'>
<xsl:param name='test' select='.'/>
   -- why declare a parameter that you don't use?

<xsl:value-of select='count([Type="FRIEND"])'/>
   -- square brackets are always used to qualify something, you need to say what you are qualifying.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2006, 02:25 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again, and thank you for your reply.

I apologize for how things look in my code sample. Basically, I've spliced that piece together from the growing list of approaches I've tried.

Thank you very much for your help, and I will give it try.




Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Missing Node (Node data) pashworth XSLT 3 January 29th, 2007 12:39 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Counting node-set children in template rufustfirefly XSLT 2 May 3rd, 2004 08:48 AM





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