PHP获取DOMNode的XPath [英] PHP Getting XPath of a DOMNode

查看:58
本文介绍了PHP获取DOMNode的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一个XML文档.而且我需要知道我刚刚创建的Node的XPath.我没有看到像 DOMNode :: calculateXPath(void):string 这样的函数而且我不愿意自己写一个.是否有任何已知的 lite 第三方解决方案?

I am creating an XML document on the fly. and I need to know the XPath of the Node I've just created. I don't see any such function like DOMNode::calculateXPath(void):string and I am not willing to write one by my own. is there any known lite 3rd party Solution ??

推荐答案

给定节点没有明确的XPath,因为XPath是搜索查询.像在google中一样,您可以使用不同的搜索词找到特定的页面.此外,在修改DOM树时,返回当前节点的XPath查询可以(可能会)更改,因此您可以获取的路径仅在完成下一个树修改之前才有效.

There's no definite XPath for given node, because XPath is a a search query. Like in google, you can find specific page using different search terms. What's more, XPath query returning current node can (probably will) change when you modify DOM tree, so the path you can get is valid only until next tree modification is done.

您可以为每个节点赋予唯一的ID或类名(当您想使用其他ID时,类会更安全-可以有多个类,只有一个ID).然后,您可以使用该类别/编号进行搜索.

You may give each of your nodes unique ID or class name (class is safer when you want to use other IDs - there can be multiple classes and only one ID). Then you can search using this class/id.

标签范围和类unique_1_的示例:

Example for tag span and class unique_1_:

//span[contains(@class, 'unique_1_')]

这篇关于PHP获取DOMNode的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆