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 July 17th, 2006, 09:09 AM
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Counting Lookups elsewhere in the XML

I need to be able to count the number of items that appear in the xml somewhere else. I'm traversing each item want to check if its in the lookup as below

<root>
  <lookup>
     <id>
  <item>
     <id2>

<xsl:for-each select="//item">

  <xsl:value-of select="count(?????)"/>
</xsl:for-each>

How would i do this without defining a variable (i'm using a simple example). I'm stuck I can do pretty much anything else with the XSLT but this logic doesn't seem to be clicking

Thanks
Ash
 
Old July 17th, 2006, 10:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see why on earth you would want a variable to do simple counting. You just have to count the items selected by some path expression. But I can't write that path expression for you, since you haven't explained clearly what your source data structure is and what the condition is for items to be counted.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 17th, 2006, 10:38 AM
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok so say I want to do the following:

<xsl:value-of select="count(/root/lookup/id=<id2>)"/>

So essentially I want to go through each item and find how many entries there are in the lookup nodelist.

So if i was using a variable i'd do it like
<xsl:for-each select="//item">
  <xsl:param name="id2" select="id2"/>
  <xsl:value-of select="count(/root/lookup/id=$id2)"/>
</xsl:for-each>

however theres some cases when you can't define variables so the above wont work.

<root>
  <lookup>
     <id> (/root/lookup/id)
  <item>
     <id2> (/root/item/id2)
 
Old July 17th, 2006, 10:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please show me a specific example, don't just mumble that there are cases that don't work.

I've no idea what this notation is supposed to mean:

<root>
  <lookup>
     <id> (/root/lookup/id)
  <item>
     <id2> (/root/item/id2)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 17th, 2006, 11:04 AM
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

XML
===

<root>
  <lookup>
     <id>12345</id>
     <id>12345</id>
     <id>21345</id>
     <id>2345</id>
  </lookup>
  <item>
     <id2>12345</id2>
     <description>widget</description>
  </item>
  <item>
     <id2>31244</id2>
     <description>bolt</description>
  </item>
</root>


PSEUDO CODE
===========
if theres any matching ids in the lookup node to the items id then output the header.


<xsl:if test="count(/root/lookup/id = //item/id2) > 0">
   [header html]
</xsl:if>
 
Old July 17th, 2006, 11:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't need to count nodes for that: you can just write

<xsl:if test="/root/lookup/id = //item/id2">
   [header html]
</xsl:if>

You could of course compute the count if you needed it, for example

count(//item/id2[. = /root/lookup/id])



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
Lookups Gajun Access 5 September 20th, 2007 07:26 AM
Counting records Jonas Access 4 August 4th, 2006 09:07 AM
charachter counting bjackman Access 12 March 11th, 2005 04:51 AM
Multiple lookups in same table kev_79 Access 3 February 15th, 2004 05:38 PM
Counting ?!? hcweb Classic ASP Basics 2 December 8th, 2003 05:08 PM





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