473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 7485
Dormilich
8,658 Expert Mod 8TB
@blacksab
could you explain, how you want to count the nodes, because I can’t 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,658 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

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

Similar topics

2
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
2
by: Tom Corcoran | last post by:
I am working to ease updating of a html page by transforming 2 xml files. I was going to use xslt for this and had bought 2 unopened books, wrox xslt and o'reilly's xslt cookbook. But am now...
1
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
3
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
12
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
3
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.