DOMNode::getNodePath

(PHP 5 >= 5.2.0, PHP 7)
Get an XPath for a node
public string DOMNode::getNodePath ( void )

Gets an XPath location path for the node.

Returns:

Returns a string containing the XPath, or NULL in case of an error.

Examples:
DOMNode::getNodePath() example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// Create a new DOMDocument instance
$dom new DOMDocument;
 
// Load the XML
$dom->loadXML('
<fruits>
 <apples>
  <apple>braeburn</apple>
  <apple>granny smith</apple>
 </apples>
 <pears>
  <pear>conference</pear>
 </pears>
</fruits>
');
 
// Print XPath for each element
foreach ($dom->getElementsByTagName('*'as $node) {
    echo $node->getNodePath() . "\n";
}
?>

The above example will output:

/fruits
/fruits/apples
/fruits/apples/apple[1]
/fruits/apples/apple[2]
/fruits/pears
/fruits/pears/pear
See also:

DOMXPath -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.