The DOMNode::getNodePath() function is an inbuilt function in PHP which is used to get an XPath location path for the node.
Syntax:
php
Output:
php
Output:
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();
?>
/root/h1Example 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>";
}
?>
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