Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

xsl:for-each iterate based on count of nodes

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?

1 Solution
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">
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.
nbingenAuthor Commented:
Thanks!  position() did it.  Also used ancestor:: to take care of elements further up in the tree.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now