使用 XPATH 语法匹配文本
示例源:view-source:http://rss.packetstormsecurity.org/files/tags/exploit /
我只想返回父标题节点具有匹配文本的 xml 部分,在本例中,要匹配的文本是“site”。
//get feed with curl
$doc = new SimpleXmlElement($xml, LIBXML_NOCDATA);
//$result = $doc->xpath('//title'); //this works returns all the <title>'s
$result = $doc->xpath('//title[site]'); //doesn't work
$result = $doc->xpath('//title[text()="site"]'); //doesn't work
$result = $doc->xpath('//title[contains(site)]'); //doesn't work
$result = $doc->xpath('//title[contains(text(),'Site')]'); //doesn't work
foreach ($result as $title)
echo "$title<br />"
Exmple feed: view-source:http://rss.packetstormsecurity.org/files/tags/exploit/
I only want to return sections of xml where the parent title node has matching text in it, in this example the text to match is "site".
//get feed with curl
$doc = new SimpleXmlElement($xml, LIBXML_NOCDATA);
//$result = $doc->xpath('//title'); //this works returns all the <title>'s
$result = $doc->xpath('//title[site]'); //doesn't work
$result = $doc->xpath('//title[text()="site"]'); //doesn't work
$result = $doc->xpath('//title[contains(site)]'); //doesn't work
$result = $doc->xpath('//title[contains(text(),'Site')]'); //doesn't work
foreach ($result as $title)
echo "$title<br />"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要的调用是这样的:
请注意,这是区分大小写的。
请注意,
contains
XPath 函数采用两个参数:一个干草堆和一根针。在本例中,我们使用当前节点的文本值作为干草堆,这通过使用点 (.
) 来指示。The call you need is, I think, this:
Note that this is case-sensitive.
Note that the
contains
XPath function takes two arguments: a haystack and a needle. In this case, we are using the current node's text value as the haystack, which is indicated by the use of the dot (.
).