Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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 -
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:31 PM.


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