使用xslt获取X位置的节点值

jas*_*_89 9 xslt xpath msxsl xslt-1.0

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

<items>
<item1>x</item1>
<item2>x</item2>
<item3>x</item3>
</items>
Run Code Online (Sandbox Code Playgroud)

这在编程意义上解释:

<xsl:value-of select="Items/Item[2]"/>
Run Code Online (Sandbox Code Playgroud)

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

只是为了扩展问题,在下面的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>
Run Code Online (Sandbox Code Playgroud)

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

Emi*_*ggi 17

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

<xsl:value-of select="(items//title)[2]"/>
Run Code Online (Sandbox Code Playgroud)

要么:

<xsl:value-of select="(items/*/title)[2]"/>
Run Code Online (Sandbox Code Playgroud)

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


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

<xsl:value-of select="items/*[2]"/>
Run Code Online (Sandbox Code Playgroud)

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

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

<xsl:value-of select="items/*[position()>2]"/>
Run Code Online (Sandbox Code Playgroud)

选择位置大于2的所有项目.其他position()不可缺少的情况是当位置值是字符串类型的变量时:

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