php

Use of xpath() in PHP

XML document is used to store a small amount of data, and sometimes it is required to read the particular content of XML document based on the path value using PHP script. xpath() function is used to parse the content of an XML document. This function can be used by using simplexml_load_file() function or by creating the object of SimpleXMLElement class. The xpath() function can be used to read the particular XML node values shown in this tutorial.

Syntax:

The syntax of the xpath() function is given below.

array xpath(string $path)

This function has one argument that takes a path value, and if the path exists in any node of the XML document, then the value of the node will be returned as an array. Different uses of this function have explained in the next part of this tutorial.

Create XML document:

Create an XML file named products.xml with the following content on the location where the PHP file will be created to parse this file.

<?xml version="1.0"?>

<PRODUCTS>

<PRODUCT category="Monitor">
<ID>MN-56345</ID>
<BRAND>DELL</BRAND>
<NAME>15 inches Dell Monitor</NAME>
<PRICE>700</PRICE>
</PRODUCT>

<PRODUCT category="HDD">
<ID>HD-34278</ID>
<BRAND>SAMSUNG</BRAND>
<NAME>1 TB Samsung HDD</NAME>
<PRICE>520</PRICE>
</PRODUCT>

<PRODUCT category="Mouse">
<ID>MS-67457</ID>
<BRAND>LOGITECH</BRAND>
<NAME>Logitech Wireless Mouse</NAME>
<PRICE>100</PRICE>
</PRODUCT>
<PRODUCT category="Monitor">
<ID>MN-76453</ID>
<BRAND>HP</BRAND>
<NAME>14 inches HP Monitor</NAME>
<PRICE>750</PRICE>
</PRODUCT>

</PRODUCTS>

Use of simplexml_load_file() function:

The xpath() function with the object created by the simplexml_load_file() function has shown in this part of this tutorial.

Example-1: Read the particular XML node values

The following example shows how to read the content of the particular node values from the XML document by defining the xpath() function path. The object variable, $xml is created to read the specific node values of the products.xml file. ‘/PRODUCTS/PRODUCT/BRAND’ is used in the xpath() to read all BRAND node values. ‘/PRODUCTS/PRODUCT’ is used in the xpath() to read all child node values of PRODUCT nodes. foreach loop is used to print the values of NAME and PRICE nodes.

<?php

//Create object to read the XML file
$xml = simplexml_load_file('products.xml');
//Search all BRAND node values
$brand = $xml->xpath('/PRODUCTS/PRODUCT/BRAND');
//Print the array values
echo "<h3>The list of brand names are:</h3>";
foreach($brand as $name) {
echo "$name<br />";
}

//Search all PRODUCT node values
$products = $xml->xpath('/PRODUCTS/PRODUCT');
echo "<h3>The list of product name and price:</h3>";
echo "<table>";
echo "<tr><th align='left'>Name</th><th>Price</th></tr>";
//Print the array values
foreach($products as $product) {
echo "<tr><td>$product->NAME</td><td> $$product->PRICE</td></tr>";
}
echo "</table>";
?>

Output:

The following output will appear after running the script from the server.

Example-2: Read the particular XML node values based on condition

The following example shows how to read the particular node values based on the condition using xpath() function. The path value, ‘/PRODUCTS/PRODUCT[PRICE > 600]’, will search the values of all child nodes of PRODUCT node where the value of PRICE node is more than 600. foreach loop is used to print the values of NAME and PRICE nodes.

<?php

//Create object to read the XML file
$xml = simplexml_load_file('products.xml');
//Search the products where the price value is more than 600
$products = $xml->xpath('/PRODUCTS/PRODUCT[PRICE > 600]');
echo "<h3>The list of product name and price where the price is more than 600:</h3>";
echo "<table>";
echo "<tr><th align='left'>Name</th><th>Price</th></tr>";
//Print the array values
foreach($products as $product) {
echo "<tr><td>$product->NAME</td><td> $$product->PRICE</td></tr>";
}
echo "</table>";
?>

Output:

The following output will appear after running the script from the server.

Example-3: Read the particular XML node values based on attribute

The following example shows how to read the particular node values based on the attribute values of the XML document’s specific node by using the xpath() function. The path value, ‘/PRODUCTS/PRODUCT[@category=”Monitor”]’ will search the values of all child nodes of the PRODUCT node, where the category attribute’s value is Monitor. foreach loop is used to print the values of BRAND, NAME, and PRICE nodes.

<?php

//Create object to read the XML file
$xml = simplexml_load_file('products.xml');
//Search the products where the çategory attribute value is 'Monitor'
$products = $xml->xpath('/PRODUCTS/PRODUCT[@category="Monitor"]');
echo "<h3>The list of product brand,name and price based on category(Monitor):</h3>";
echo "<table>";
echo "<tr><th align='left'>Brand</th><th align='left'>Name</th><th>Price</th></tr>";
//Print the array values
foreach($products as $product) {
echo "<tr><td>$product->BRAND</td><td>$product->NAME</td><td> $$product->PRICE</td></tr>";
}
echo "</table>";
?>

Output:

The following output will appear after running the script from the server.

Use of SimpleXMLElement class:

The uses of xpath() function by creating the SimpleXMLElement class object have shown in this part of this tutorial.

Example-4: Use of xpath() by defining XML content in a variable

The following example shows how to read the XML document’s node values declared in a variable instead of the file. XML content is stored in the $xml_data variable. “/customers/customer” is used as the argument value of xpath() function to read all values of the child nodes of the customer node. Next, a for each loop is used to print the values of the name node. “/customers/customer[@department=’HR’]” is used as the argument value of another xpath() function to read all values of the child nodes of customer node where the value of department attribute is HR. Next, a for each loop is used to print the values of the email node.

<?php

$xml_data = <<<XML
<customers>
<customer department="HR">
<name>Md. Mahbub</name>
<email>[email protected]</email>
</customer>
<customer department="Sales">
<name>Farhana Zaman</name>
<email>[email protected]</email>
</customer>

</customers>
XML
;

//Define object to read the XML data
$xml = new SimpleXMLElement($xml_data);
//Define path to read all customers data
$customers = $xml->xpath("/customers/customer");

//Print the name of the customers
echo "<h3>The list of customer names:</h3>";
foreach($customers as $customer) {
    echo "$customer->name<br />";
}

//Define path to read all customer data of HR department
$customers = $xml->xpath("/customers/customer[@department='HR']");

//Print the email of the customers
echo "<h3>The customer's email of HR department:</h3>";
foreach($customers as $customer) {
    echo "$customer->email<br />";
}
?>

Output:

The following output will appear after running the script from the server.

Conclusion:

Two different ways of using the xpath() function to read the XML document’s node values based on the specific path or the path with the condition or the path with attribute value have been explained in this tutorial by using multiple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.