如何在 PHPQuery 中使用 xpath 查询?
我正在寻找如何使用 xpath phpQuery 的示例代码。 我阅读了维基页面,但没有找到任何东西。 提前致谢。
I'm looking for an example code how use xpath phpQuery.
I read the wiki-pages but not found any thing.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以 PHPQuery 实际上有一个名为
protected function getNodeXpath($oneNode = null, $namespace = null)
的函数。如果您只需查看 phpquery_onefile。如果您只需将此
protected
函数更改为public
,您现在就可以在代码中使用它。您只需要执行如下操作:pq('button')->getNodeXpath()
。但请记住,这会返回一个数组和许多函数(例如 php-webdriver) 将需要一个字符串 xpath。要解决此问题,只需将您返回的数组内爆即可。
So PHPQuery actually has a function called
protected function getNodeXpath($oneNode = null, $namespace = null)
. You can find it pretty easily if you just look in the phpquery_onefile.If you simply change this
protected
function topublic
, you can now use it in your code. You would just need to do something like this:pq('button')->getNodeXpath()
.Keep in mind though, this returns an array, and a lot of functions (such as those in php-webdriver) will require a string xpath. To fix this, simply implode the array you get back.
项目摘要指出
由于 XPath 不是 CSS3 选择器的一部分,并且文档中没有参考资料,所以我想说它尚未实现。
更新
通过挖掘它们的源代码,它看起来像是包装了
DOMDocument
并且您可以从phpQuery::$documents
数组中检索文档。获得DOMDocument
实例后,您可以创建DOMXPath
对象并对其执行查询。The project summary states
As XPath is not part of CSS3 selectors and there are no references in the documentation, I'd say it hasn't been implemented.
Update
From digging through their source, it looks like it wraps
DOMDocument
and you can retrieve the documents from thephpQuery::$documents
array. Once you have theDOMDocument
instance, you can perform create aDOMXPath
object and perform queries on it.