Source of: /manual/en/simplexmlelement.xpath.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.simplexmlelement.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'simplexmlelement.xpath.php',
1 => 'SimpleXMLElement::xpath',
),
'up' =>
array (
0 => 'class.simplexmlelement.php',
1 => 'The SimpleXMLElement class',
),
'prev' =>
array (
0 => 'simplexmlelement.registerXPathNamespace.php',
1 => 'SimpleXMLElement::registerXPathNamespace',
),
'next' =>
array (
0 => 'ref.simplexml.php',
1 => 'SimpleXML Functions',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="simplexmlelement.xpath" class="refentry">
<div class="refnamediv">
<h1 class="refname">SimpleXMLElement::xpath</h1>
<p class="verinfo">(PHP 5 >= 5.2.0)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::xpath</span> — <span class="dc-title">Runs XPath query on XML data</span></p>
</div>
<a name="simplexmlelement.xpath.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="classsynopsis">
<div class="ooclass"><a href="class.simplexmlelement.php" class="classname">SimpleXMLElement</a></div>
<div class="methodsynopsis dc-description">
<span class="type">array</span> <span class="methodname"><b>xpath</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$path</tt></span>
)</div>
</div>
<p class="para">
The <i>xpath</i> method searches the SimpleXML node for
children matching the <acronym title="XML Path Language">XPath</acronym> <i><tt class="parameter">path</tt></i>
.
</p>
</div>
<a name="simplexmlelement.xpath.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">path</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
An XPath path
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="simplexmlelement.xpath.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns an <a href="language.types.array.php" class="type array">array</a> of SimpleXMLElement objects or <b><tt class="constant">FALSE</tt></b> in
case of an error.
</p>
</div>
<a name="simplexmlelement.xpath.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Xpath</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$string </span><span style="color: #007700">= <<<XML<br /></span><span style="color: #DD0000"><a><br /> <b><br /> <c>text</c><br /> <c>stuff</c><br /> </b><br /> <d><br /> <c>code</c><br /> </d><br /></a><br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Search for <a><b><c> */<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'/a/b/c'</span><span style="color: #007700">);<br /><br />while(list( , </span><span style="color: #0000BB">$node</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'/a/b/c: '</span><span style="color: #007700">,</span><span style="color: #0000BB">$node</span><span style="color: #007700">,</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* Relative paths also work... */<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'b/c'</span><span style="color: #007700">);<br /><br />while(list( , </span><span style="color: #0000BB">$node</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'b/c: '</span><span style="color: #007700">,</span><span style="color: #0000BB">$node</span><span style="color: #007700">,</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff
</pre></div>
</div>
<div class="example-contents simpara"><p>
Notice that the two results are equal.
</p></div>
</div><p>
</p>
</div>
</div><?php manual_footer(); ?>