问题描述
在PHP中,我想获取包含给定文本的所有 DOMElement
。
In PHP, i want to get all DOMElement
containing a given text.
我得到 DOMText
当 $ xpath-> query( // text()[contains(。,'My String')])
,但是我必须执行哪个查询才能获取 DOMElement
?
I get DOMText
when $xpath->query("//text()[contains(., 'My String')]")
, but which query must i perform for getting DOMElement
instead?
推荐答案
使用:
(//*[text()
[contains(., 'My String')]
]
)[1]
此选择XML文档中具有文本节点子项的第一个元素,该子节点包含字符串我的字符串
。
This selects the first element in the XML document that has a text node child that contains the string "My String"
.
如果可以保证只有一个这样的元素存在,则上述表达式可以简化为:
If it is guaranteed that only one such element exist, the above expression can be simplified to:
//*[text()
[contains(., 'My String')]
]
如果要确保您要查找的元素只有一个文本节点子代,则可以将该表达式简化为:
If the elements you are looking for are guaranteed to have just a single text-node child, this expression can be simplified to:
(//*[contains(., 'My String')])[1]
分别:
//*[contains(., 'My String')]
这篇关于获取具有特定文本的DOMElement PHP / XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!