Jump to content

Xpath to find parent based on value of child node


kwindo

Recommended Posts

I'm having a slight problem selecting an item based on the value of one of it's child nodes. Let's say I need to find the value from the "id" value in the item that has a value "Fables" in it's "name" node.Example xml:

<?xml version="1.0" encoding="UTF-8"?><results>	<game>		<id>01</id>		<name>Final Fantasy Fables</name>	</game>	<game>		<id>02</id>		<name>Fantasy Fables</name>	</game>	<game>		<id>03</id>		<name>Fables</name>	</game>	<game>		<id>04</id>		<name>Something Totally Different</name>	</game></results>

I have this PHP:

$parent = '//results/game/name[contains(., "Fables")]'; // Xpath to parent$resultsb = $xml->xpath($parent);foreach ($results as $titleElement) {			$game = simplexml_import_dom(dom_import_simplexml($titleElement)->parentNode);			echo $game -> name;		}

However this returns:Final Fantasy FablesFantasy FablesFablesSo its returning all games of which the "name" node contains "Fables". Is there a similar way to find the game of which the "name" node is identical to "Fables"? I tried stuff with "deep-equal" in the Xpath to parent but until now this gives me no results.Thanks!

Link to comment
Share on other sites

Try this:

'//results/game/name[. = "Fables"]'

It's a pretty common pattern. Just to be sure, I tested it using DOMDocument and DOMXpath, and it works fine. I don't know why it wouldn't work in your technique.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...