tags:

views:

29

answers:

1

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&lt;/link&gt;
      <description><![CDATA[ Text here ]]></description>
      <publishDate>Fri, 03 Apr 2009 10:00:00</publishDate>
      <guid>http://www.somelink.com/read-story-100&lt;/guid&gt;
      <category domain="http://www.somelink.com/?category=4"&gt;Category 1</category>
    </item>

    <item>
      <title>Heading 2</title>
      <link>http://www.somelink.com?id=110&lt;/link&gt;
      <description><![CDATA[ Text here ]]></description>
      <publishDate>Fri, 03 Apr 2009 11:00:00</publishDate>
      <guid>http://www.somelink.com/read-story-110&lt;/guid&gt;
      <category domain="http://www.somelink.com/?category=4"&gt;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.

+1  A: 

(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']]
AakashM
Thanks for the reply! The expression ran without errors, but the return set is empty. : /
bakkelun
@bakkelun — Most likely, you'll need to add additional element names to the beginning of the path to match your XML structure. (e.g. If your `channel` is wrapped in a `feed`, the xpath should be changed to `/feed/channel/item[...]`.) For testing, you could also temporarily change it to `//channel/item[...]`.
Ben Blank
@bakkelun, oops I had `child` in the expression where I meant `category`. Edited; try it now?
AakashM
Same as before, the expression ran, but the return set is empty :((
bakkelun
After 4 hours of getting nowhere, you Sir AakashM led me the right way. Here's the working one (See updated original post). I'll give you a granted solution to this one!
bakkelun