SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleXML Xpath limitations

    The manual only shows a somewhat vauge example of using xpath with simpleXML. It only shows how to fetch elements, and doesn't include checking attrubutes,etc. How much of w3c's recommendation does it support?
    Is this possible using SimpleXML, or would I have to use the DOM?

    For example, A simple loop
    PHP Code:
    $xml simplexml_load_file('template.xml');
          foreach(
    $xml as $tpl) {
        echo 
    $tpl->asXML();
      } 
    Now assuming I had a rather large file, and I wanted to return an element whose 'name' attribute is equal too 'foo', I need to loop through every element.

    PHP Code:
    $xml simplexml_load_file('template.xml');
      foreach(
    $xml as $tpl) {
        if(
    $tpl['name'] == 'foo') {
        echo 
    $tpl->asXML();
        }
      } 
    It would be nice if xpath could be used in this case, and thus eleminating the need to itterate over every element. Maybe someone has a better explination and could elaborate a bit.

    Cheers -Justin

  2. #2
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't have a box with PHPversion5 though I do know that XPATH support would be more than effiecent to do what you want to do with it, going by your post

    If your stuck for tutorials on PHPversion5s DOM, have you looked over the links in the advance resources yet, sure you'll find something useful in there I'd imagine ?

  3. #3
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Widow Maker

    I have rubbed the letters off search buttons all over the internet, but most of the examples are pertaining to using xpath with the dom extensions, and not simple xml.

    I have found some examples, Php.net's own example and a couple on bitflux, but they only show how to use it with elements, and not attributes.

    Guess I'll have to remain patient while everybody takes ahold of this new player.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use it with elements, and not attributes
    Since your looking for SimpleXML specfic, a shame as if the problem was with the DOM, this would be easier to find a solution

    Don't really know that much about SimpleXml myself, though have you looked at Harry's PHPversion5 article here ?

    If I remember, there were several example in that, which may help you ?

  5. #5
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I give his article a run, very nice and indepth and it will surley be helpful in other areas!

    The refrences to simpleXML seemed to be outdated from some aspects though. His article states that simpleXML ignores attributes.

    The SimpleXML extension does have limitations. In particular, I found it ignores XML attributes and can only deal with XML documents of up to three levels of node in depth, including the root node.
    This isn't the case anymore, but it probally was the time of his article. I was also unaware that you could only deal with nodes < 3 deep and decided to test that as well.

    HTML Code:
    <root>
    <children>
      <child>
        <name>Bob</name>
        <cars>
          <car>
            <name>BMW</name>
            <tires>4</tires>
            <warranty>
              <type>
                <name>Super Warranty</name>
                <miles>100,00 Miles</miles>
              </type>
              <length>3 Years</length>
            </warranty>
          </car>
          <car>
          <name>Lexus</name>
            <tires>4</tires>
            <warranty>
             <type>
                <name>Super Warranty</name>
                <miles>100,00 Miles</miles>
              </type>
              <length>3 Years</length>
            </warranty>
           </car>
          </cars>
        </child>
     </children>
    </root>
    Outputs:
    HTML Code:
    SimpleXMLElement Object
    (
        [children] => SimpleXMLElement Object
            (
                [child] => SimpleXMLElement Object
                    (
                        [name] => Bob
                        [cars] => SimpleXMLElement Object
                            (
                                [car] => Array
                                    (
                                        [0] => SimpleXMLElement Object
                                            (
                                                [name] => BMW
                                                [tires] => 4
                                                [warranty] => SimpleXMLElement Object
                                                    (
                                                        [type] => SimpleXMLElement Object
                                                            (
                                                                [name] => Super Warranty
                                                                [miles] => 100,00 Miles
                                                            )
    
                                                        [length] => 3 Years
                                                    )
    
                                            )
    
                                        [1] => SimpleXMLElement Object
                                            (
                                                [name] => Lexus
                                                [tires] => 4
                                                [warranty] => SimpleXMLElement Object
                                                    (
                                                        [type] => SimpleXMLElement Object
                                                            (
                                                                [name] => Super Warranty
                                                                [miles] => 100,00 Miles
                                                            )
    
                                                        [length] => 3 Years
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    This also proves to be false using php 5.0.0.

    I may end up having to rewrite my class using the dom. Maybe i'm mistaking the intentions of simpleXML, but from the current information I've acquired, simplicity is the primary focus, and that is the case. But simplicity at the price of functionality isn't the trade I want to make.

    Maybe Harry will pop his head in here and shed some light on this .

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may end up having to rewrite my class using the dom.


    This would be the best approach, the one I'd have taken anyway since this was the very purpose of Doc Obj Model was design for

    I think you may well find more documentation on the DOM of PHPversion5 anyway ?

    Good Luck


Bookmarks

Posting Permissions

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