www.webdeveloper.com
Results 1 to 15 of 15

Thread: Counters and XML

  1. #1
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96

    Counters and XML

    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?

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    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...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    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.

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    I think that you want to be using the "position()" expression. See http://saxon.sourceforge.net/saxon6....ricExpressions.
    “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

  5. #5
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    Thanks. That appears to relate to XSLT which I am (ahem) not using.

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    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;
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  7. #7
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    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($fp4096)) 
            {
             
    xml_parse($xml_parser$datafeof($fp));
            }
            
        
    // close the parser
         
    xml_parser_free($xml_parser); 

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  9. #9
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    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.

  10. #10
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    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 .
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  11. #11
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    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.

  12. #12
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    In that case...XSL/XSLT will be the best suited option..ofcourse there will be a learning curve there....

    Good luck.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  13. #13
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    You bet. It doesn't help that our web host hasn't the Apache XSLT Sablotron module loaded.

  14. #14
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    Is it not possible to find a DOM based parser for your php...as compare to SAX based ...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  15. #15
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    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
    Last edited by Hester; 06-12-2003 at 07:51 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles