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 January 9th, 2009, 11:08 AM
Registered User
 
Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sandeepbhutani304 Send a message via Yahoo to sandeepbhutani304
Default XSLT: Need to concatenate strings in loop and hold them for later use

Hi,
I am making some loops using xsl:for-each.
Now I want to concatenate strings of each iteration to the previous iteration results.
And want to keep it in some variable for later use.

e.g.:
for each iteration suppose i get a,b,c. Now I want to put them in a variable as a_b_c.
How can I achieve this?
 
Old January 9th, 2009, 11:16 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

xsl:for-each is not a loop.
If you want to concatenate the values selected in an xsl:for-each then simply do e.g.
Code:
<xsl:variable name="v">
<xsl:for-each select="foo">
  <xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
Of course you don't need a variable if you simply want to add the values to the result tree, in that case doing e.g.
Code:
<xsl:for-each select="foo">
  <xsl:value-of select="."/>
</xsl:for-each>
suffices.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 9th, 2009, 11:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

XSLT cannot loop, for-each processes each node found by the select attribute. It's also a functional language so what you want can't be done ina non-functional way such as repeated adding to a variable. The common way to carry out the task you require, especially using XSLT version 1.0, is to use recursion. Pass the nodes to a template that processes the first, and passes the rest on to itself. When there are no more nodes it returns.

If you're using version 2.0 you can probably use the xls:value-of element with the separator attribute set to '_' or the XPath string-join() function.
__________________
Joe
http://joe.fawcett.name/
 
Old January 9th, 2009, 11:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Martin's way is simpler, for version 1.0. I seem to have recursion on the brain at the moment
__________________
Joe
http://joe.fawcett.name/





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 06:04 PM
xslt when loop problem check XSLT 1 April 18th, 2008 08:47 AM
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 02:27 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 04:46 PM





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