Click to See Complete Forum and Search --> : Counters and XML
Hester
06-09-2003, 05:17 AM
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.
Is there a way?
Khalid Ali
06-09-2003, 04:59 PM
is there any other way you would phrase your question?...I could not quite make iot that what is it you are wanting to achieve...
Hester
06-10-2003, 05:38 AM
Yes, sorry about that. Let me explain more:
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.
Charles
06-10-2003, 06:20 AM
I think that you want to be using the "position()" expression. See http://saxon.sourceforge.net/saxon6.5.2/expressions.html#NumericExpressions.
Hester
06-10-2003, 07:44 AM
Thanks. That appears to relate to XSLT which I am (ahem) not using.
Khalid Ali
06-10-2003, 09:44 AM
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.
var ctr = elements.length;
Hester
06-10-2003, 09:54 AM
I am using the PHP parser. The code looks like this:
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));
}
// close the parser
xml_parser_free($xml_parser);
Khalid Ali
06-10-2003, 12:24 PM
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.
http://forum.java.sun.com/forum.jsp?forum=34
Hester
06-11-2003, 08:18 AM
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.
Then just output the result in one go.
Khalid Ali
06-11-2003, 08:54 AM
if you think its not that imparative to use XML parsers or XSL then why not use JavaScript + DOM ....just curious...
if you do want to do that..email me the xml file and your question .
Hester
06-11-2003, 09:02 AM
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.
Khalid Ali
06-11-2003, 11:07 AM
In that case...XSL/XSLT will be the best suited option..ofcourse there will be a learning curve there....:D
Good luck.
Hester
06-12-2003, 03:24 AM
You bet. It doesn't help that our web host hasn't the Apache XSLT Sablotron module loaded.
Khalid Ali
06-12-2003, 06:22 AM
Is it not possible to find a DOM based parser for your php...as compare to SAX based ...
Hester
06-12-2003, 07:26 AM
I have this in mind: http://xml-sm.phplab.net/en/
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