note 94917 modified in simplexmlelement.xpath by thiago
| From: | thiago@php.net | Date: | Wed, 02 Dec 2009 20:32:08 +0000 |
| Subject: | note 94917 modified in simplexmlelement.xpath by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163503@lists.php.net to get a copy of this message | ||
Looks like the relative path is relative to the SimpleXMLElement object that you call the Xpath()
method on. Basically chopping off the root element.
<?php
$string = <<<XML
<foo>
<bar>
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
</bar>
</foo>
XML;
$xml = new SimpleXMLElement($string);
/* Search for children using 2nds level relative <bar><a><b><c> */
$result = $xml->xpath('a/b/c');
if ($result)
foreach($result as $node) {
drupal_set_message(t('a/b/c: @node.', array('@node' => $node)));
}
else
drupal_set_message(t('a/b/c: failed.'));
/* it relative to the SimpleXMLElement Search for 2nds level relative
<bar><a><b><c> but where is foo */
$bar = $xml->bar;
$result = $bar->xpath('a/b/c');
foreach($result as $node) {
drupal_set_message(t('bar->a/b/c: @node.', array('@node' => $node)));
}
?>
results in :
#
# a/b/c: failed.
# bar->a/b/c: text.
# bar->a/b/c: stuff.
--was--
Looks like the relative path is relative to the SimpleXMLElement object that you call the Xpath()
method on. Basically chopping off the root element.
$string = <<<XML
<foo>
<bar>
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
</bar>
</foo>
XML;
$xml = new SimpleXMLElement($string);
/* Search for children using 2nds level relative <bar><a><b><c> */
$result = $xml->xpath('a/b/c');
if ($result)
foreach($result as $node) {
drupal_set_message(t('a/b/c: @node.', array('@node' => $node)));
}
else
drupal_set_message(t('a/b/c: failed.'));
/* it relative to the SimpleXMLElement Search for 2nds level relative
<bar><a><b><c> but where is foo */
$bar = $xml->bar;
$result = $bar->xpath('a/b/c');
foreach($result as $node) {
drupal_set_message(t('bar->a/b/c: @node.', array('@node' => $node)));
}
results in :
#
# a/b/c: failed.
# bar->a/b/c: text.
# bar->a/b/c: stuff.
http://php.net/manual/en/simplexmlelement.xpath.php