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:
<issue>21</issue>
<filename>U21.pdf</filename>
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.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
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.
I am not sure which parser you are using,If youwere to use JavaScript then your life could have been allot easier since JS uses DOM based parsing that means it retains everything in memory.
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.
I am using the PHP parser. The code looks like this:
PHP Code:
function startElement($parser, $NAME, $attrs) {
global $startTags;
// if tag exists as key, print value
if ($startTags[$NAME]) print $startTags[$NAME];
}
function endElement($parser, $NAME) {
global $endTags;
if ($endTags[$NAME]) print $endTags[$NAME];
}
function characterData($parser, $data) {
print $data;
}
// initialize the parser
$xml_parser = xml_parser_create();
// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
// open xml file
$fp = fopen($xml_file, "r");
// read and parse data
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp));
}
Yes that is SAX based parsing...I'll see if I could spare anytime for this...in the meanwhile post a question at this forum...and post your code with your question.
I just thought. Why use the parser at all? I could just replace the tags with the required HTML directly. Then a counter could be used during the routine to insert the correct values required.
This would be feasible for simple XML documents with basic tags I'd have thought.
I want to use PHP as I know it better than Javascript. The DOM will only work with modern browsers surely? I want the output to be available to all browsers, as we must cater for Netscape 4 on this site.
To be honest, right now, it's not worth all this extra effort for just a few colours on a page. I will try and learn XSLT over time and apply it if I can. For now PHP does me proud.
//EDIT
I just found this. The guy is using a counter via an array! It also gives a clear definiton of how the PHP parser operates. http://www.zend.com/zend/art/parsing.php
Bookmarks