xsl:for-each iterate based on count of nodes

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-05-18
I'm new to xsl and need some help.  

1.  I'm trying to output the same text information based on the count of the node (./shipTo/item/qty)

2.  Here's my code.  Notice I'm using a variable "numValues" to store the count, so I can use it in my for each.  When I do so, I get the error "Cannot convert #NUMBER to a NodeList!"

<xsl:variable name="numValues">
    <xsl:value-of select="count(./shipTo/item/qty)+1"/>

  <xsl:for-each select="$numValues">
    <xsl:value-of select="./transSetId"/>,<xsl:value-of select="./releaseId"/>,<xsl:value-of select="./shipTo/item/qty"/>,<xsl:value-of select="./shipTo/item/qty/@um"/>,<xsl:value-of select="./shipTo/item/qty/@due"/>,<xsl:text>

3.  Here's a sample of what I expect as a result:

4.  How can I repeat the information for each incident of the ./shipTo/item/qty node?

Can you help?

Question by:nbingen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Expert Comment

ID: 8125537
You cannot run a for-each on a NUMBER this is not a regular "for" statment, the for-each can only run on node sets :

In your case, use :
<xsl:for-each select="./shipTo/item/qty">

Accepted Solution

stevenbaker earned 150 total points
ID: 8127115
If you want the count then use either script (if in msxml)
or, better, use the position() function:
<xsl:for-each select="./shipTo/item/qty">
<xsl:value-of select="./transSetId"/>,
<xsl:value-of select="./releaseId"/>,
<xsl:value-of select="."/>,
<xsl:value-of select@um"/>,
<xsl:value-of select@due"/>,
<xsl:value-of select="position()"/>

from the example (diff to know without XML) it seems like you dont actually need the counter, but the example above swill itterate through the shipTo/item/qty elements, output any required details, followed by their position within the XML tree fragment.

If this isnt what is required please post details and the an example of the XML doc you want to process.

Author Comment

ID: 8132892
Thanks!  position() did it.  Also used ancestor:: to take care of elements further up in the tree.

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question