Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2008, 10:56 AM
Authorized User
 
Join Date: Jul 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Concatenating values in a loop at a stretch!!!!!!!

Hi All,

I have got a requirement. I got an xml file like this

<Players>
<player>
 <Name>Rakesh</Name>
</Player>
<player>
 <Name>Ashish</Name>
</Player>
</Players>

I have to show output like below
 Players: Rakesh,Ashish

Is there any aggregate function which will do this? Is there any other way of doing apart from looping through and concatenating the values with a script? Please help.

Rakesh

 
Old July 23rd, 2008, 10:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well with XSLT 2.0 you can use
Code:
<xsl:value-of select="/Players/Player/Name" separator=","/>
With XSLT 1.0 you need to use xsl:for-each or xsl:apply-templates to output each name and the comma for each Name but the last one.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 23rd, 2008, 11:04 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In 2.0, use the string-join() function, or simply

<xsl:value-of select="//Name" separator=","/>

In 1.0 use

<xsl:for-each select="//Name">
  <xsl:value-of select="."/>
  <xsl:if test="position()!=last()">,</xsl:if>
</

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old July 23rd, 2008, 11:10 AM
Authorized User
 
Join Date: Jul 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that. One silly question. how can we use 2.0 version if our xslt is of 1.0. is there any way to upgrade?

Rakesh

 
Old July 23rd, 2008, 11:15 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You need to run your stylesheet with an XSLT 2.0 processor like Saxon or Gestalt or Altova.
And you should use
Code:
<xsl:stylesheet version="2.0"
to make full use of XSLT 2.0 features like the suggested xsl:value-of use.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 23rd, 2008, 11:21 AM
Authorized User
 
Join Date: Jul 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks martin for the response.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
How to loop for Select Values rohit_ghosh Excel VBA 3 May 8th, 2007 03:29 PM
Need help in retrieving values from a loop ! back2grave XSLT 0 June 27th, 2006 05:29 PM
Concatenating column values arnabghosh Access 1 September 21st, 2005 06:45 AM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.