Current Location: Home> Latest Articles> How to Use PHP XPath Function with SimpleXML to Parse XML: Detailed Examples

How to Use PHP XPath Function with SimpleXML to Parse XML: Detailed Examples

gitbox 2025-08-25

2. Introduction to XPath

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.


3. Example of Parsing XML with SimpleXML and XPath

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">&lt;&lt;&lt;XML
&lt;books&gt;
    &lt;book id="1"&gt;
        &lt;title&gt;PHP Basics&lt;/title&gt;
        &lt;author&gt;Zhang San&lt;/author&gt;
    &lt;/book&gt;
    &lt;book id="2"&gt;
        &lt;title&gt;Practical XML Parsing&lt;/title&gt;
        &lt;author&gt;Li Si&lt;/author&gt;
    &lt;/book&gt;
    &lt;book id="3"&gt;
        &lt;title&gt;Deep Dive into PHP&lt;/title&gt;
        &lt;author&gt;Li Si&lt;/author&gt;
    &lt;/book&gt;
&lt;/books&gt;
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>

4. Explanation of XPath Expressions

  • //book: Selects all book nodes

  • [author='Li Si']: A filter condition to select book nodes where the author node value is “Li Si”


5. More Practical Examples

1. Getting a Node’s Attribute

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>

2. Complex Queries

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>

6. Conclusion

  • 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.