Open In App

PHP | SimpleXMLElement XPath() Function

Last Updated : 03 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Read XML Basics

The SimpleXMLElement::xpath() function is an inbuilt function in PHP which runs XPath query on the XML document.

Syntax:

SimpleXMLElement::xpath( $path )

Parameters: This function accepts single parameter $path which is required. It is used to specify the XPath path of XML document.

Return Value: It returns an array of SimpleXMLElements on success or False on failure.

Note: This function is available on PHP 5.2.0 and newer version.

Example:




<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <id>12345</id>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noida India
    </detail>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Retrieving xpaths
$result = $xml->xpath("username");
  
// Printing output
print_r($result);
   
?>


Output:

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => Geeks123
        )

)

Example 2:




<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://geeksforgeeks.org/user">
<single_user id="1">
    <user_id:id>12345</user_id:id>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noida India
    </detail>
</single_user>
  
<single_user id="2">
    <user_id:id>15980</user_id:id>
    <username>Geeks54321</username>
    <name>Geeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noida India
    </detail>
</single_user>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Registering xpath namespace
$xml->registerXPathNamespace('u', 'http://geeksforgeeks.org/user');
  
// Retrieving xpaths
$result = $xml->xpath('//u:id');
  
// Printing output
foreach ($result as $id) {
    echo $id . "<br>";
}
  
?>


Output:

12345
15980

Example 3:




<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://geeksforgeeks.org/user">
    <single_user id="1" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>12345</user_id:id>
        <username>rakesh123</username>
        <name:firstname>Rakesh</name:firstname>
        <name:lastname>Kumar</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Noida India</detail>
    </single_user>
      
    <single_user id="2" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>57833</user_id:id>
        <username>man123</username>
        <name:firstname>Manjeet</name:firstname>
        <name:lastname>Singh</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Kolkata, India</detail>
    </single_user>
      
    <single_user id="3" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>98944</user_id:id>
        <username>ak98</username>
        <name:firstname>Ak</name:firstname>
        <name:lastname>Singh</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Noida India</detail>
    </single_user>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Registering xpath namespace
$xml->registerXPathNamespace('u', 'http://geeksforgeeks.org/user');
$xml->registerXPathNamespace('un', 'http://geeksforgeeks.org/user/name');
  
// Retrieving xpaths
$result = $xml->xpath('//u:id');
$result_f_name = $xml->xpath('//un:firstname');
$result_l_name = $xml->xpath('//un:lastname');
  
// Printing output
foreach ($result as $id) {
    echo $id . "<br>";
}
     
foreach ($result_f_name as $f_name) {
    echo $f_name . "<br>";
}
  
foreach ($result_l_name as $l_name) {
    echo $l_name . "<br>";
}
  
?>


Output:

12345
57833
98944
Rakesh
Manjeet
Ak
Kumar
Singh
Singh

Reference: https://www.php.net/manual/en/simplexmlelement.xpath.php



Similar Reads

PHP | SimpleXMLElement children() Function
Pre-requisite: Read XML BasicsThe SimpleXMLElement::children() function is an inbuilt function in PHP which returns children of a given node in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and described below: $namespace: It is op
3 min read
PHP | SimpleXMLElement __toString() Function
The XMLReader::__toString() function is an inbuilt function in PHP which is used to get the text content that is directly in current element. This function does not return text content that is inside this element's children. Syntax: void XMLReader::__toString( void ) Parameters: This function doesn’t accept any parameters.Return Value: This functio
1 min read
PHP | SimpleXMLElement::getName() Function
Pre-requisite: Read XML basicsThe SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.Syntax: string SimpleXMLElement::getName( void ) Parameter: This function does not accept any parameter.Return Value: It returns a string which represents the name of XML element of a SimpleXMLElement object
2 min read
PHP | SimpleXMLElement saveXML() Function
The SimpleXMLElement::saveXML() function is an inbuilt function in PHP which return well-formed XML string from a SimpleXML object. It is an alias of SimpleXMLElement::asXML() function. Syntax: mixed SimpleXMLElement::saveXML( string $filename ) Parameter: This function accepts single parameter $filename which is optional. It specified this functio
2 min read
PHP | SimpleXMLElement count() Function
Pre-requisite: Read XML Basics The SimpleXMLElement::count() function is an inbuilt function in PHP which counts number of child element in a SimpleXML object. Syntax: int SimpleXMLElement::count() Parameter: This function does not accept any parameters. Return Value: This function returns number of children of an element. Note: This function is av
2 min read
PHP | SimpleXMLElement::__construct() Function
Pre-requisite:XML The __construct() function is an inbuilt function in PHP that is used to create a new SimpleXMLElement object for XML. Syntax: SimpleXMLElement::__construct( $data, $options, $data_is_url, $namespace, $is_prefix ) Parameters: This function accepts five parameters as mentioned above and described below: $data: It is a required para
3 min read
PHP | SimpleXMLElement asXML() Function
Pre-requisite: Read XML The SimpleXMLElement::asXML() function is an inbuilt function in PHP which returns well-formed XML string from a SimpleXML object. Syntax: mixed SimpleXMLElement::asXML( $filename ) Parameters: This function accepts single parameter $filename which is optional. It specified this function save data to the file instead of retu
2 min read
PHP | SimpleXMLElement addChild() Function
Pre-requisite: Read XML Basics The SimpleXMLElement::addChild() function is an inbuilt function in PHP which is used to add a child in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::addChild($name, $value, $namespace); Parameter: This function accepts three parameters as mentioned above and described below: $name: It is required par
1 min read
PHP | SimpleXMLElement addAttribute() Function
Pre-requisite: Read XML Basics The SimpleXMLElement::addAttribute() function is an inbuilt function in PHP which add an attribute in a SimpleXML object. Syntax: void SimpleXMLElement::addAttribute($name, $value, $namespace) Parameter: This function accepts three parameters as mentioned above and described below: $name: It is required parameter. It
2 min read
PHP | SimpleXMLElement attributes() Function
Pre-requisite: Read XML BasicsThe SimpleXMLElement::attributes() function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::attributes( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and d
2 min read