我在使 XPath 函数工作时遇到问题。我应该只提取 XML 文件中名称以字母“z”结尾的条目,但我无法让它工作。我是 xmllint 用 cat 命令操作文件。
ends-with()
只适用于 XPath 2.0,我很确定服务器只有 XPath 1.0,所以我尝试输入这个命令:
cat //country/city[substring(@name, string-length(@name) - string-length('z') +1)]/name
但是我一直报错,我不确定是我写的不对还是写的方式不对。
最佳答案
您的 XPath 缺少检查 substring()
的返回值是否等于预期的字符串结束值 "z"
的步骤。所以正确的 XPath 应该是:
//country[substring(@name, string-length(@name) - string-length('z') +1) = 'z']/@name
并且由于 -string-length('z') +1
等于 0
,可以省略该部分以获得更简单的 XPath,类似于发布的通过 @Kirill Polishchuk在other answer .
关于xml - 需要 XPath ends-with 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399825/