|
[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.ukWed Sep 1 10:01:46 PDT 2004
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! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|






