PHP SimpleXMLElement XPath () Function

PHP

Function SimpleXMLElement::xpath() - it is a built-in function in PHP that performs an XPath query on an XML document.Syntax:
SimpleXMLElement::xpath ($path) 
Parameters:This function takes one required parameter, $path . It is used to specify the XPath path of the XML document.Return Value:Returns a SimpleXMLElements array on success, or False on failure.Note .This feature is available in PHP 5.2.0 and later.Example :  
// Load XML document into $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;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Get xpaths $result = $xml -> xpath ( "username" );  
// Printout print_r ( $result );  
?>
Exit:
Array ([ 0] = > SimpleXMLElement Object ([0] = > Geeks123)) 
Example 2:  
// Load XML document into $user $user = < < < XML < user xmlns: user_id = " http://engineerforengineer.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;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Register xpath namespace $xml -> registerXPathNamespace ( 'u' , ' http: //engineerforengineer.org/user ' );  
// Get xpaths $result = $xml -> xpath ( '// u: id' );  
// Printout foreach ( $result as $id ) { echo $id . "< br >" ; }  
?>
Exit:
12345 
15980
Example 3:  
// Load XML document into $user $user = < < < XML < user xmlns: user_id = " http: // engineerforengineer.org/user " > < single_user id = " 1 " xmlns: name = " http://engineerforengineer.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://engineerforengineer.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://engineerforengineer.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;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Register xpath namespace $xml -> registerXPathNamespace ( 'u' , ' http: //engineerforengineer.org/user ' ); $xml -> registerXPathNamespace ( 'un' , ' http://engineerforengineer.org/user/name ' );  
// Get xpaths $result = $xml -> xpath ( '// u: id' ); $result_f_name = $xml -> xpath ( '// un: firstname' ); $result_l_name = $xml -> xpath ( '// un: lastname' );  
// Printout 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 >" ; }  
?>
Exit:
12345 
57833
98944
Rakesh, Manjeet, Ak, Kumar, Singh, Singh
Link: https://www.php.net/manual/en/simplexmlelement.xpath.php