How do I select all parenting items based on a given node's attribute in php's xPath?

Posted by bakkelun on Stack Overflow See other posts from Stack Overflow or by bakkelun
Published on 2010-04-15T17:34:40Z Indexed on 2010/04/15 17:43 UTC
Read the original article Hit count: 157

Filed under:
|
|

I have an XML feed that looks something like this (excerpt):

<channel>
  <title>Channel Name</title>
  <link>Link to the channel</link>

    <item>
      <title>Heading 1</title>
      <link>http://www.somelink.com?id=100</link>
      <description><![CDATA[ Text here ]]></description>
      <publishDate>Fri, 03 Apr 2009 10:00:00</publishDate>
      <guid>http://www.somelink.com/read-story-100</guid>
      <category domain="http://www.somelink.com/?category=4">Category 1</category>
    </item>

    <item>
      <title>Heading 2</title>
      <link>http://www.somelink.com?id=110</link>
      <description><![CDATA[ Text here ]]></description>
      <publishDate>Fri, 03 Apr 2009 11:00:00</publishDate>
      <guid>http://www.somelink.com/read-story-110</guid>
      <category domain="http://www.somelink.com/?category=4">Category 1</category>
    </item>

  <channel>

That's the rough of it. I'm using this piece of PHP (excerpt):

$xml = simple_xml_load_file($xmlFile);
$xml->xpath($pattern);

Now I want to get all ITEM-nodes (with their children) based on that pesky "domain" attribute in the category node, but no matter what I try it does-not-work.

The closest I got was "//category[@domain= 'http://www.somelink.com/?category=4']"

The expression I tried gave me this result:

 [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [domain] => http://www.somelink.com/?category=4
                )

            [0] => Category 1

 [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [domain] => http://www.somelink.com/?category=4
                )

            [0] => Category 1

The expression should contain all childrens of the two items in the example, but as you can see only the info in the category node is present, I want all the item nodes.

Any help would be highly appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about xpath