How do I select all parenting items based on a given node's attribute in php's xPath?
Thanks to AakashM, he led me in the right direction. For the question below, this is the xPath expression that led me the right way:
"//channel/item[category[@domain='http://www.somelink.com/?category=4']]"
Original post:
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.
(Try and avoid using //
when you know for sure the structure of your xml)
So you want all item
nodes which have a category
child node which in turn has a domain
attribute with a particular value:
/channel/item[category[@domain='http://www.somelink.com/?category=4']]
精彩评论