SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleXML Xpath - Get attribute value from an unknown host node.

    Given this xml:

    Code XML:
    <root>
      <event id="test1">
          <randomTag url="">
     
          </randomTag>
      </event>
      <event id="test2">
          <anotherTag url="">
     
          </anotherTag>
      </event>
      <event id="test2">
          <someTag url="">
     
          </someTag>
      </event>
    </root>

    Sample Xpath to use with PHP SimpleXML (doesn't work)
    //event[@id="test1"]/@url

    The first part of the xpath expression works fine. It locates the correct event node based on the supplied id tag. The part I'm having trouble with is then finding any attribute in the event tag that has an attribute of url. I've done some googling and can't seem to find the right syntax to get the attribute text from an unknown host node.

    In short this is what the xpath expression should do:
    In a given event tag return all url attributes found in any of events decedents.

    Any suggestions on how to accomplish this functionality?

    Thanks.
    Follow Me On Twitter: BryceRay

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    You were nearly there. The broken XPath asks for the url attribute belonging to the event itself. To tell XPath to look through all descendant nodes for their url attributes, you could use something like the following.

    Code:
    //event[@id="test1"]/descendant::*/@url
    Salathe
    Software Developer and PHP Manual Author.


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
  •