使用 xslt 在 X 位置获取节点值 [英] Using xslt get node value at X position

查看:28
本文介绍了使用 xslt 在 X 位置获取节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用 foreach 的情况下使用 xslt,X 位置的节点值

How can I get using xslt, node value at X position, without using foreach

<items>
<item1>x</item1>
<item2>x</item2>
<item3>x</item3>
</items>

这是在编程意义上解释的:

This is explained in programming sense:

<xsl:value-of select="Items/Item[2]"/>

===================================================

==================================================

在下面的xml中稍微扩展一下问题:

Just to little expand question, in the following xml:

<items>
    <about>xyz</about>
    <item1>
       <title>t1</title>
       <body>b1</body>
    </item1>
    <item2>
       <title>t2</title>
       <body>b2</body>
    </item2>
    <item3>
       <title>3</title>
       <body>3</body>
   </item3>
</items>

如何选择第二个项目的标题.

推荐答案

回答扩展问题.如果您选择所需元素的节点集,则可以使用位置值:

Answer to expanded question. You can use the positional value if you select a node-set of the wanted elements:

<xsl:value-of select="(items//title)[2]"/>

或:

<xsl:value-of select="(items/*/title)[2]"/>

注意在按位置选择之前返回所需节点集所需的括号的用法.

Note the usage of the parenthesis required to return wanted node-set before selecting by position.

您可以使用您所说的在编程意义上".但是,由于子元素的名称未知,您需要 *:

You can use what you called "in programming sense". However you need * due to the unknown name of the children elements:

<xsl:value-of select="items/*[2]"/>

请注意,XSLT 中的节点集不是从零开始的.在上面的方式中,您选择的是第二个项目,而不是第三个.

Note that nodes-sets in XSLT are not zero-based. In the way above you are selecting the second item, not the third one.

当你想将当前位置与一个数字进行比较时,你真的需要 position() :

You really need position() when you want compare the current position with a number as in:

<xsl:value-of select="items/*[position()>2]"/>

选择位置大于 2 的所有 item.其他 position() 是必不可少的情况是当位置值是 string:

to select all item with position grater than 2. Other case where position() is indespensible is when position value is a variable of type string:

<xsl:variable name="pos" select="'2'"/>
<xsl:value-of select="items/*[position()=$pos]"/>

这篇关于使用 xslt 在 X 位置获取节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆