Subject: Concatenating values in a loop at a stretch!!!!!!!
Posted By: rakesh Post Date: 7/23/2008 10:56:37 AM
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

Reply By: Martin Honnen Reply Date: 7/23/2008 10:59:50 AM
Well with XSLT 2.0 you can use

<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
Reply By: mhkay Reply Date: 7/23/2008 11:04:01 AM
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
Reply By: rakesh Reply Date: 7/23/2008 11:10:55 AM
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

Reply By: Martin Honnen Reply Date: 7/23/2008 11:15:42 AM
You need to run your stylesheet with an XSLT 2.0 processor like Saxon or Gestalt or Altova.
And you should use

<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
Reply By: rakesh Reply Date: 7/23/2008 11:21:07 AM
Thanks martin for the response.


Go to topic 44571

Return to index page 1