idleonbench
idleonbench

Reputation: 97

xslt getting the value of node in a loop for a specified range

I have a xml drafted as follows:

  <node index="1">value1</node>
  <node index="2">value2</node>
  <node index="3">value3</node>
  <node index="4">value4</node>
  ...

I would like to specify a range, say index from 2~6, and output these values each within a tag.

Currently I am doing something like:

  <xsl:variable name="location" select="/node" />

  <xsl:for-each select="($location[position() &gt;= 2 and position() &lt;= 6])">
        <td><xsl:value-of select="$location[.]" /> </td>
  </xsl:for-each>

However, this doesn't seem to work..any suggestion? Thanks in advance

Upvotes: 0

Views: 1829

Answers (1)

Richard
Richard

Reputation: 109005

xsl:for-each sets the context node – the base for relative XPath expressions – on each iteration. Therefore if the for-each is matching the correct nodes (which looks OKish: there can only be a single root node in an XML document, so including a position check won't work unless it is 1: I assume the Q is simplified from the real code) then you should be able to use:

<xsl:for-each select="(/node[position() &gt;= 2 and position() &lt;= 6])">
  <td><xsl:value-of select="." /> </td>
</xsl:for-each>

Upvotes: 2

Related Questions