469,306 Members | 1,881 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Counter in XSLT

Hi All,

I'm having trouble counting the number of occurring nodes from my input XML.

I have this input:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2.   <level1>
  3.     <countMeIn1/>
  4.   <level1>
  5.   <level1>
  6.     <countMeIn2/>
  7.   <level1>
  8.   <level1>
  9.     <countMeIn3/>
  10.     <countMeIn4/>
  11.     <countMeIn5/>
  12.   <level1>
  13. </root>
  14.  
And my desired output is:

Expand|Select|Wrap|Line Numbers
  1. <root>
  2.   <countMeIn count="1"/>
  3.   <countMeIn count="2"/>
  4.   <countMeIn count="3"/>
  5.   <countMeIn count="4"/>
  6.   <countMeIn count="5"/>
  7. </root>
  8.  
I have tried the position() function but that only renders an incremented number for 3,4 and 5.

Any help would be greatly appreciated.
Oct 29 '09 #1

✓ answered by blacksab

This is solved:

This node to do the assignment to counter:
<xsl:variable name="counter">
<xsl:number level="any" count="countMeIn"/>
</xsl:variable>

And then this node to output the value as an attribute:
<xsl:attribute name="line">
<xsl:value-of select="$counter"/>
</xsl:attribute>

This solution was also given on another thread in this forum.

Thank you for your input.

4 6993
Dormilich
8,651 Expert Mod 8TB
@blacksab
could you explain, how you want to count the nodes, because I cant follow your calculation.
Oct 29 '09 #2
Hi Dormilich,

I explained my input badly. In my input XML I have several nodes named countMeIn. I want to do a sequential count of the included countMeIn nodes in the output XML
Expand|Select|Wrap|Line Numbers
  1.  <root>
  2.    <level1>
  3.      <countMeIn/> <!-- Want an attribute named count="1" for this node-->
  4.    <level1>
  5.    <level1>
  6.      <countMeIn/> <!-- Want an attribute named count="2" for this node-->
  7.    <level1>
  8.    <level1>
  9.      <countMeIn/> <!-- Want an attribute named count="3" for this node-->
  10.      <countMeIn/> <!-- Want an attribute named count="4" for this node-->
  11.      <countMeIn/> <!-- Want an attribute named count="5" for this node-->
  12.    <level1>
  13.  </root>
  14.  
Oct 29 '09 #3
Dormilich
8,651 Expert Mod 8TB
after some thinking I came up with 2 ideas
1. use the DOM
2. use XSLT twice, the first one is only for grabbing the <CountMeIn> nodes, the second one you can use to make the count attribute.
Oct 30 '09 #4
This is solved:

This node to do the assignment to counter:
<xsl:variable name="counter">
<xsl:number level="any" count="countMeIn"/>
</xsl:variable>

And then this node to output the value as an attribute:
<xsl:attribute name="line">
<xsl:value-of select="$counter"/>
</xsl:attribute>

This solution was also given on another thread in this forum.

Thank you for your input.
Oct 30 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by ted | last post: by
2 posts views Thread by Tom Corcoran | last post: by
3 posts views Thread by Jack Fox | last post: by
3 posts views Thread by Teksure | last post: by
3 posts views Thread by Ian Roddis | last post: by
1 post views Thread by Sergey Dubinets | last post: by
12 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.