我该如何修改这个xpath来查找是否存在一个包含我的关键词短语的alt的img标签?
$xPath->evaluate('count(/html/body//'.$tag.'[contains(@alt,"'.$keyword.'") and self::img])');
我该如何修改这个xpath来查找是否存在一个包含我的关键词短语的alt的img标签?
$xPath->evaluate('count(/html/body//'.$tag.'[contains(@alt,"'.$keyword.'") and self::img])');
用途:
boolean(//img[contains(@alt, 'yourKeywordHere')])
要查找 XML 文档中是否存在包含“yourKeywordHere”关键词的 alt
属性的 img
元素,可以使用 (true()
, false()
) 函数来实现。
用法:
boolean(//yourTag//img[contains(@alt, 'yourKeywordHere')])
查找文档中是否存在名为yourTag
的元素,它有一个后代img
,其alt
属性包含'yourKeywordHere'
。
img
元素且其alt
属性包含某些特定字符串的h1
元素。相反,@Scott-B想要知道的只是一个信息位——“是否存在这样的元素”。这就是我在我的答案中给他的。 - Dimitre Novatchev我不太清楚你具体需要哪些元素,但这里有一个例子,它会返回所有包含至少一张带有 your_keyword
作为 alt 属性的图片的 h1
元素:
//h1[.//img[contains(@alt, 'your_keyword')]]
lower-case
函数。//h1[.//img[contains(lower-case(@alt), lower-case('your_keyword'))]]
这里是一个例子:
//h1[.//img[contains(@alt, 'key ')]]
<html>
<h1> <!-- found -->
<img alt='here is my key' />
</h1>
<h1><!-- not found -->
<img alt='here is not' />
</h1>
<h1> <!-- found -->
<h2>
<img alt='the key is also here' />
</h2>
</h1>
<h1></h1> <!-- not found -->
</html>