[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

position() and last()

Michael Kay mhk at mhk.me.uk
Wed Sep 1 10:01:46 PDT 2004


xslt position count last
A "for" expression does not change the focus (that is, the context item,
position, or size). This is a common mistake for anyone accustomed to
xsl:for-each - I make it frequently myself.

In XQuery (but not XPath) you can assign an extra variable to represent the
current position, thus:

for $i at $pos in $items return
($i, if ($pos = count($items)) then () else ",")

Alternatively you can iterate over the positions:

(for $n in 1 to count($items)-1 return ($items[$n], ",")), $items[last()])

In many cases (but not this one) the insertion of separators between items
can be achieved using the string-join function:

string-join($items, ',')

position() and last() are available in XQuery but they are only really
useful inside predicates.

Michael Kay
 

> -----Original Message-----
> From: http://xquery.com/mailman/listinfo/talk 
> [mailto:http://xquery.com/mailman/listinfo/talk] On Behalf Of R. Mark Volkmann
> Sent: 01 September 2004 04:15
> To: http://xquery.com/mailman/listinfo/talk
> Subject:  position() and last()
> 
> I know how to use these functions in XSLT, but I haven't 
> figured out how to
> use them in XQuery.
> For example, suppose I want to put a comma between items, but 
> not after the
> last one.
> I could do something like this.
> 
> for $i in $items
> return (
>   $i,
>   if (position() = last()) then () else ","
> )
> 
> This doesn't work. What am I doing wrong?
> 
> 
> _______________________________________________
> http://xquery.com/mailman/listinfo/talk
> http://xquery.com/mailman/listinfo/talk
> 



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-2007 All Rights Reserved.