略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DOMNode::getNodePath

2024-11-15

DOMNode::getNodePath

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DOMNode::getNodePathGet an XPath for a node

说明

public DOMNode::getNodePath(): ?string

Gets an XPath location path for the node.

参数

此函数没有参数。

返回值

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

范例

示例 #1 DOMNode::getNodePath() example

<?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";
}
?>

以上例程会输出:

/fruits
/fruits/apples
/fruits/apples/apple[1]
/fruits/apples/apple[2]
/fruits/pears
/fruits/pears/pear

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/domnode.getnodepath.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3