xml 使用 xpath 获取节点的第 N 个子节点

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

Get Nth child of a node using xpath

xmlxpath

提问by IordanTanev

My sample input XML is:

我的示例输入 XML 是:

<root>
 <a>
   <b>item</b>
   <b>item1</b>
   <b>item2</b>
   <b>item3</b>
   <b>item4</b>
 </a>
</root>

I am suppose to select a node bwhose position is the value of a variable.

我假设选择一个b位置是变量值的节点。

How can I use the value of a variable to test the position of a node?

如何使用变量的值来测试节点的位置?

回答by remi bourgarel

you can use this:

你可以使用这个:

/root/a/b[position()=$variable]

position() is 1 based

position() 是基于 1

http://saxon.sourceforge.net/saxon6.5.3/expressions.html

http://saxon.sourceforge.net/saxon6.5.3/expressions.html

回答by Ronald Wildenberg

The following should work:

以下应该工作:

/root/a/b[2]

And if it doesn't, try:

如果没有,请尝试:

/root/a/b[position()=2]