Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xslt counter problem

Status
Not open for further replies.

rxg00u

Programmer
Apr 11, 2002
16
GB
hi i am relativly new to xml and was looking for some help,
Im transforming an xml document using a xslt ; i am now in need of a counter of some sort to remember the number of occurances of a particular tag. I am not sure if there is an easier way of doing counters but i was thinkin of using something like the following.
Set up a variable like:
Code:
<xsl:variable name=&quot;X&quot; select=&quot;1&quot;/>
and when i need to 'print' the value use something like:
Code:
<xsl:value-of select=&quot;$X&quot;/>
the problem i am having is incrementing the value.
Im not sure how you would increment the value, also is the best way of utilising a counter or is there a better method.
Thanks for all your time
rxg00u
 
Unfortunately, you can't update a variable like this in XSLT. Once a variable is set, you cannot update/reassign its value.

With XSLT, you have to think a little latterally. You can't update it, but the value of this count is predeteminable for that type of tag.

Therefore, instead of counting as you go, just count the whole lot at once when you need to display the total number of occurances like this:

<xsl:value-of select=&quot;count(//mynode)&quot;/>


The reason for this is that you are pretty much working on a &quot;static&quot; dataset. The XML shouldnt change for the lifetime of the XSLT transform, and therefore there is no need for dynamic variables such as in your example.

This is not to say you cant &quot;count&quot; through the lines, you can. If you really want to do it the other way, create a template that matches the node you are interested in. Then within this template use the position() function to return the position of the node within the given context.

eg:

<xsl:template match=&quot;mynode&quot;>

..

<xsl:value-of select=&quot;position()&quot;/>

..

</xsl:template>

The problem with this method is that position() will only work within one context at a time. The context of a node is the selected parent or xpath criteria within which you are working, and therefore you cannot acheive an overall count of the total number of mynodes in a document (for this, use count as above :)

hope this helps a bit,


Matt
 
im using your method where i count using
<xsl:value-of select=&quot;count(//mynode)&quot;/>
the problem now which i didnt think about is if i need to count at diff points in the document say at the begining middle and end i always get the total number of nodes .
I suppose the question now is is there a way where i can count nodes up to that particular point
Thanks for your time
 
u can supply the xpath expression like this:


<xsl:value-of select=&quot;count(beginning/mynode)&quot;/>
<xsl:value-of select=&quot;count(middle/mynode)&quot;/>
<xsl:value-of select=&quot;count(end/mynode)&quot;/>

or even
<xsl:value-of select=&quot;count(//part[@beginning=true]/mynode)&quot;/>

or something.

can u give me an example xml and i'll try to help a bit more :)
 
ok basically im transforming xml documents that are actually exam papers into equivalent pages.
now i can do the basic stuff like the exam ruric etc and the question but one of the final problems im having is question numbering.
Code:
<SECTION code=&quot;A&quot; supplemental=&quot;Answer ALL Questions&quot;>
<QUESTION marks=&quot;10&quot;>
Write a program that computes the product of the numbers from 1 to 5 inclusive.
Hint: The value you should obtain is 120.
</QUESTION>
<QUESTION marks=&quot;10&quot;>
Write a program that counts both the total number and number of vowel keys (a,e,i,o,u) pressed until
the user hits the ‘!’ key. When the ‘!’ key is pressed the program should display the total key-press count on the screen together with the percentage of vowel keys pressed.
</QUESTION>
i process the section tag and apply all templates.
My question template is someting like this:
Code:
<xsl:template match=&quot;QUESTION&quot;>
<xsl:text>Question </xsl:text><xsl:value-of select=&quot;count(//QUESTION)&quot;/>
<xsl:value-of select=&quot;self::node()&quot;/></body><br/>
</xsl:template>
With this code i get
Question 5....
Question 5....
(because i have a total of 5 questions)

basically i need to count how many questions i am on so i can then 'print' Question 1, Question 2 etc
Thanks for all this
 
use position() instead of count.


eg.
<xsl:text>Question </xsl:text><xsl:value-of select=&quot;position()&quot;/>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top