需要 XPath 结尾函数

BMi*_*825 5 xml xpath xmllint

我在使 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
Run Code Online (Sandbox Code Playgroud)

但我不断收到错误,我不确定我是否写得不正确或者这不是正确的方法。

har*_*r07 3

substring()您的 XPath 缺少检查返回值是否等于预期字符串结束值的步骤"z"。所以正确的 XPath 是:

//country[substring(@name, string-length(@name) - string-length('z') +1) = 'z']/@name
Run Code Online (Sandbox Code Playgroud)

由于-string-length('z') +1等于0,因此可以省略该部分以获得更简单的 XPath,类似于@Kirill Polishchuk另一个答案中发布的内容。