XPath is a language used to locate nodes in XML documents. SimpleXML comes with the built-in xpath() method, which allows querying nodes using XPath expressions.
Suppose we need to retrieve the titles of all book nodes where the author is “Li Si” from the following XML.
<span><span><span class="hljs-variable">$xmlString</span></span><span> = <span class="hljs-string"><<<XML
<books>
<book id="1">
<title>PHP Basics</title>
<author>Zhang San</author>
</book>
<book id="2">
<title>Practical XML Parsing</title>
<author>Li Si</author>
</book>
<book id="3">
<title>Deep Dive into PHP</title>
<author>Li Si</author>
</book>
</books>
XML;
<p></span>$xml = simplexml_load_string($xmlString);</p>
<p>// XPath query for book nodes with author Li Si<br>
$result = $xml->xpath("//book[author='Li Si']");</p>
<p>foreach ($result as $book) {<br>
echo "Title: " . $book->title . "\n";<br>
}<br>
</span>
Output:
<span><span>Title: </span><span><span class="hljs-type">Practical XML Parsing</span></span><span>
Title: Deep Dive into PHP
</span></span>
//book: Selects all book nodes
[author='Li Si']: A filter condition to select book nodes where the author node value is “Li Si”
Retrieve the id attribute of all book nodes:
<span><span><span class="hljs-keyword">foreach</span></span><span> (</span><span><span class="hljs-variable">$xml</span></span><span>-></span><span><span class="hljs-title function_ invoke__">xpath</span></span><span>(</span><span><span class="hljs-string">"//book"</span></span><span>) </span><span><span class="hljs-keyword">as</span></span><span> </span><span><span class="hljs-variable">$book</span></span><span>) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Book ID: "</span></span><span> . </span><span><span class="hljs-variable">$book</span></span><span>[</span><span><span class="hljs-string">'id'</span></span><span>] . </span><span><span class="hljs-string">"\n"</span></span><span>;
}
</span></span>
Get books with id greater than 1:
<span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-variable">$xml</span></span><span>-></span><span><span class="hljs-title function_ invoke__">xpath</span></span><span>(</span><span><span class="hljs-string">"//book[@id > 1]"</span></span><span>);
</span><span><span class="hljs-keyword">foreach</span></span><span> (</span><span><span class="hljs-variable">$result</span></span><span> </span><span><span class="hljs-keyword">as</span></span><span> </span><span><span class="hljs-variable">$book</span></span><span>) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$book</span></span><span>->title . </span><span><span class="hljs-string">"\n"</span></span><span>;
}
</span></span>
Load XML using simplexml_load_string() or simplexml_load_file().
Use the xpath() method with XPath expressions for flexible node selection.
Access node attributes through the attribute array.
XPath expressions are powerful and support various filters and path selections.
Mastering the combination of SimpleXML and XPath enables PHP developers to efficiently handle different XML data, enhancing the flexibility and maintainability of their programs.