string 在 XPATH 中获取属性的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4322013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:53:55  来源:igfitidea点击:

Getting a substring of an attribute in XPATH

stringxpathsubstring

提问by Kit Sunde

Given

给定的

<foo>
  <bar baz="Hello, World!">
</foo>

How do I all but the last 4 characters of @baz? One of my attempts was:

除了@baz 的最后 4 个字符,我如何处理?我的尝试之一是:

/foo/bar/@baz[substring( ., 0, -4 )]

采纳答案by Dimitre Novatchev

Use:

使用

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-3) 

Do note the 3in the expression.

请注意3表达式中的 。

The following is wrong:

以下是错误的

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-4) 

because this returns the last 5 characters of the string value of the bazattribute.

因为这将返回baz属性字符串值的最后 5 个字符。

回答by ecciethetechie

try this: substring-before(/foo/bar/@baz,"rld!")

尝试这个: substring-before(/foo/bar/@baz,"rld!")

回答by Jan Thom?

That's actually not so bad, but IIRC substring doesn't like negative indices. I tried

这实际上还不错,但是 IIRC 子字符串不喜欢负索引。我试过

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-4)

Which gave me the expected result.

这给了我预期的结果。