xml XPath 可以匹配元素名称的一部分吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46125/
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
Can XPath match on parts of an element's name?
提问by Chris Marasti-Georg
I want to do this:
我想做这个:
//*fu
//*福
which returns all nodes whose name ends in fu, such as <tarfu />and <snafu />, but not <fubar />
它返回名称以fu结尾的所有节点,例如<tarfu />and <snafu />,但不返回<fubar />
回答by Dimitre Novatchev
This answer is for XPath 1.0where there is no equivalent of the XPath 2.0 standard ends-with() function.
此答案适用于XPath 1.0,其中没有等效于XPath 2.0 标准的 Ends-with() 函数。
The following XPath 1.0 expression selects all elements in the xml document, whose names end with the string "fu":
以下 XPath 1.0 表达式选择 xml 文档中名称以字符串“fu”结尾的所有元素:
//*[substring(name(),string-length(name())-1) = 'fu']
回答by Chris Marasti-Georg
回答by Identity1
I struggled with Dimitre Novatchev's answer, it wouldn't return matches. I knew your XPath must have a section telling that "fu" has length 2.
我对 Dimitre Novatchev 的回答感到困惑,它不会返回匹配项。我知道您的 XPath 必须有一个部分告诉“fu”的长度为 2。
It's advised to have a string-length('fu') to determine what to substring.
建议使用 string-length('fu') 来确定子串的内容。
For those who aren't able to get results with his answer and they require solution with xpath 1.0:
对于那些无法通过他的回答获得结果并且需要使用 xpath 1.0 解决方案的人:
//*[substring(name(), string-length(name()) - string-length('fu') +1) = 'fu']
//*[substring(name(), string-length(name()) - string-length('fu') +1) = 'fu']
Finds matches of elements ending with "fu"
查找以“fu”结尾的元素的匹配项
or
或者
//*[substring(name(), string-length(name()) - string-length('Position') +1) = 'Position']
//*[substring(name(), string-length(name()) - string-length('Position') +1) = 'Position']
Finds matches to elements ending with "Position"
查找以“Position”结尾的元素的匹配项

