[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Unexpected ('a', 'b')[fn:position()] result

Subject: Re: Unexpected ('a', 'b')[fn:position()] result
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 07 Mar 2007 01:12:41 +0100
Re:  Unexpected ('a'
Rachel D. Basse wrote:
<xsl:template match="/">
 <xsl:sequence select="('a', 'b')[fn:position()]"/>
</xsl:template>

returns ('a', 'b').

Why is this? Everything tells me the last expression should evaluate to 'a'. Did I miss something?

Hi Rachel,


It's a classic, and every now and then, I do it wrongly still (check the archives, I have once or twice asked about the same question). The function position() (you don't need to put "fn:" in front of it) returns the position of the context node. The context changes within the XPath expression to each node it is evaluating.

Thus, the expression

('a', 'b')[position()]

a) will test the first item in the sequence 'a' for the predicate [position()] which evaluates to the predicate [1], which is short for [position() = 1], which will return true because the current position is 1.

b) will test the second item in the sequence 'b' for the predicate [position()] which evaluates to the predicate [2], which is short for [position() = 2], which will return true because the current position is 2 (we are at the second item, 'b', remember).

As it comes, the following:

some-node[position()]

will always evaluate to true, and

(some-sequence)[position()]

will always return the whole sequence, because each separate item will always have the position in the sequence that equals the result of the predicate [position()].

What you want is the following:

<xsl:variable name="pos" select="position()" />
<xsl:sequence select="('a', 'b')[$pos]" />

HTH,

Cheers,
-- Abel Braaksma
  http://www.nuntia.nl

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.