xml 节点名称上的 XPath 通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4203119/
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-06 13:23:53  来源:igfitidea点击:

XPath wildcards on node name

xmlxsltxpath

提问by Ben Everard

I want to grab a node from my XML file, the node has a prefix such as "latest_", but this may change and I'm keen for my XSLT to be as fluid as possible. Here's the XPath I want to use:

我想从我的 XML 文件中获取一个节点,该节点有一个前缀,例如“latest_”,但这可能会改变,我希望我的 XSLT 尽可能流畅。这是我要使用的 XPath:

/data/stats/*_cost

This should match latest_cost, newest_cost, anything_cost, is there a way of doing this?

这应该匹配latest_cost, newest_cost, anything_cost,有没有办法做到这一点?

Cheers :-)

干杯:-)

回答by

This is the correct XPath 1.0 expression which selects an element with the last 5 character of name equal to "_cost" in any namespace.

这是正确的 XPath 1.0 表达式,它在任何命名空间中选择名称的最后 5 个字符等于“_cost”的元素。

/data/stats/*[substring(name(), string-length(name()) - 4) = '_cost']

回答by redsquare

You could also use contains

你也可以使用 contains

e.g

例如

/data/stats[contains(.,'_cost')] 

回答by Alex Nikolaenkov

With XPath 1.0 you can use /data/stats/*[substring-after(name(), '_cost') = '']pattern. That checks if the element's name ends with the _costsuffix.

在 XPath 1.0 中,您可以使用/data/stats/*[substring-after(name(), '_cost') = '']模式。这将检查元素的名称是否以_cost后缀结尾。

In XPath 2.0 there is fn:ends-with(str, str)and your corresponding expression will be *[ends-with(name(), '_cost')].

在 XPath 2.0 中fn:ends-with(str, str),您的相应表达式将是*[ends-with(name(), '_cost')].

回答by David W.

The above did not work for me. I had to "slightly" modify that as follows:

以上对我不起作用。我不得不“稍微”修改如下:

/data/stats/*[contains(name(),'_cost')]