PHP | DOMNode getNodePath() Function
Last Updated :
27 Feb, 2020
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
$dom = new DOMDocument;
$dom ->loadXML('
<root>
<h1>GeeksforGeeks</h1>
</root>
');
echo $dom ->getElementsByTagName( 'h1' )
->item(0)->getNodePath();
?>
|
Output:
/root/h1
Example 2:
<?php
$dom = new DOMDocument;
$dom ->loadXML('
<root>
<h1>Geeks</h1>
<div>
<h1>For</h1>
</div>
<h1>Geeks</h1>
</root>
');
for ( $i = 0; $i < 3; $i ++) {
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...