0

I have found this site which says that I can pick a specific node of this code

<xsl:variable name="in-xml" as="item()*"> <in-xml>
     <a>1</a>
     <c>2</c>
     <a>3</a>
     <a>4</a>
     <a>5</a>    </in-xml> </xsl:variable>

It says that if you wrote this:

$in-xml/*[position() > 2]

you would get this:

<a>3</a>
<a>4</a>
<a>5</a>

but it seems not to be working for me. Here is the site: http://www.xsltfunctions.com/xsl/fn_position.html

Can anyone give the code to get that???

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
Gazaz
  • 167
  • 1
  • 3
  • 9
  • The `xsl:variable "as"` attribute is only supported in xslt 2.0. Which xsl processor are you using. There may be other alternatives? – StuartLC Sep 25 '12 at 15:38
  • Ehi, this site is not a code dispenser. The collaborative guys do not 'give' code. Think abount this. – Alberto De Caro Sep 26 '12 at 14:55

2 Answers2

2

In XSLT 1.0, XPath expressions can only be performed on input data, not on data created in the course of evaluating the stylesheet. So one way to make your example work would be to move the in-xml data to an external document and load it using the document() function. There are also some tricky / clever techniques that involve writing the in-xml element as a child of xsl:stylesheet and using document('')/xsl:stylesheet/in-xml to declare the variable; these techniques work with good XSLT 1.0 implementations but fail in some browser implementations.

In XSLT 2.0, the constraint on the use of XPath was relaxed and something along the lines you sketch should work.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
2

EXSLT defines node-set() which can be used here. Here is a MsXsl implementation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="in-xml" > <!--as="item()*-->
            <in-xml>
                <a>1</a>
                <c>2</c>
                <a>3</a>
                <a>4</a>
                <a>5</a>
            </in-xml>
        </xsl:variable>

        <xsl:value-of select="msxsl:node-set($in-xml)/in-xml/a[position() > 2][1]"/>
    </xsl:template>
</xsl:stylesheet>
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285