PHP | DOMNode getNodePath() Function
The DOMNode::getNodePath() function is an inbuilt function in PHP which is used to get an XPath location path for the node.
Syntax:
string DOMNode::getNodePath( void )
Parameters: This function doesn’t accept any parameters.
Return Value: This function returns a string containing the XPath, or NULL in case of an error.
Below examples illustrate the DOMNode::getNodePath() function in PHP:
Example 1:
<?php // Create a new DOMDocument instance $dom = new DOMDocument; // Load the XML $dom ->loadXML(' <root> <h1>GeeksforGeeks</h1> </root> '); // Print XPath of the element echo $dom ->getElementsByTagName( 'h1' ) ->item(0)->getNodePath(); ?> |
Output:
/root/h1
Example 2:
<?php // Create a new DOMDocument instance $dom = new DOMDocument; // Load the XML $dom ->loadXML(' <root> <h1>Geeks</h1> <div> <h1>For</h1> </div> <h1>Geeks</h1> </root> '); // Print XPath of the element for ( $i = 0; $i < 3; $i ++) { // Print where the line where the 'node' // element was defined in echo $i +1 . ') The h1 tag\'s Xpath is: ' . $dom ->getElementsByTagName( 'h1' ) ->item( $i )->getNodePath() . "<br>" ; } ?> |
Output:
1) The h1 tag's Xpath is: /root/h1[1] 2) The h1 tag's Xpath is: /root/div/h1 3) The h1 tag's Xpath is: /root/h1[2]
Reference: https://www.php.net/manual/en/domnode.getnodepath.php
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.