Is it possible to use a counter with XML so that each time a tag is output, the counter increases? I tried this and gave up. The counter was only incremented once, even though the tag in question was output many times.
I'm using PHP to generate XHTML before and after the content of each tag. This is done with an array. Then I output the lot, adding the data between the XML tags as well.
I'm not skilled enough to use XSLT yet, so forgive me.
I am using XML to store a list of documents. Then I am using PHP to parse the list and convert it to HTML drop-down menus. To do this, I add the correct tags around each XML entry. Example, my XML looks like this:
Then I make it into this: <option value="files/U21.pdf">21</option>
This is very basic XML but it works. Now what I would like to do is use a counter so that each time the <issue> tag is found, the counter increases. Why? Because then I can read from an array to give me a different coloured background for each option on the menu.
I am sure there are many other uses this idea will give me, but I cannot get it to work.
What I tried was increasing the counter every time the XML tag equals "<issue>" but that gave me the same counter every time. It just gave me "1". The counter never advanced. Is that because the XML is only really parsed once?
I also tried it if the data equals a number in the range I use. (Eg: <issue>23</issue> - data = "23".) But that was the same.
What I found odd also is that I am getting the counter displayed in other places on the page too, even though there is no <issue> tag there.
Please forgive me for my basic knowledge of XML and parsing. It's still a bit of a mystery to me, but I am slowly learning.
Originally posted by Hester .......What I tried was increasing the counter every time the XML tag equals "<issue>" but that gave me the same counter every time. It just gave me "1". The counter never advanced. ...
Most XML parsing is based on events..that means when an event( or a tag is encountered) it performs an action,but it does not keep record for that action.
Usually what this means that xml parsing is a stateless process.You will have to devise a way to simmulate the counter process.
Let me know what parser is being used.
And if its a SAX based parser( which I am afraid it is) thenyou will have to write a recursive function that will go through the xml doc and create a counter effect.
Else if its DOM then you only eed to do this
var elments = document.getElementsByTagName("issue");
this will return a NodeList object which has a length property you can use to find out howmany of issue tags are in the doc.