我试图找到一个嵌套表的所有行,其中包含一个id为'_imgProductImage'的图像.
我正在使用以下查询:
"//tr[/td/a/img[ends-with(@id,'_imgProductImage')]"
Run Code Online (Sandbox Code Playgroud)
我收到错误:xmlXPathCompOpEval:函数结束 - 找不到
我的谷歌搜索我相信说这应该是一个有效的查询/功能.如果不是"结束",我正在寻找的实际功能是什么?
ax.*_*ax. 12
从如何以编程方式在XPathExpression实例中使用XPath函数?
可以很容易地构造一个XPath 1.0表达式,其评估产生与函数相同的结果
ends-with():
$str2 = substring($str1, string-length($str1)- string-length($str2) +1)产生相同的布尔结果(
true()或false()):
ends-with($str1, $str2)
所以对于你的例子,以下xpath应该工作:
//tr[/td/a/img['_imgProductImage' = substring(@id, string-length(@id) - 15)]
你可能想要添加一个注释,这是一个xpath 1.0重构ends-with().
它似乎ends-with()是一个XPath 2.0函数.
DOMXPath仅支持XPath 1.0
评论后编辑:在你的情况下,我想你必须:
id属性(请参阅getAttribute方法)与您想要的匹配,则循环使用PHP,针对每一个进行测试.要测试属性是否正常,您可以在迭代图像的循环中使用类似的东西:
$id = $currentNode->getAttribute('id');
if (preg_match('/_imgProductImage$/', $id)) {
// the current node is OK ;-)
}
Run Code Online (Sandbox Code Playgroud)
请注意,在我的正则表达式模式中,我使用a $来表示字符串的结尾.
ends-withXPath 1.0中没有任何功能,但您可以伪造它:
"//tr[/td/a/img[substring(@id, string-length(@id) - 15) = '_imgProductImage']]"
Run Code Online (Sandbox Code Playgroud)