p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   XSLT: Need to concatenate strings in loop and hold them for later use (http://p2p.wrox.com/showthread.php?t=72164)

sandeepbhutani304 January 9th, 2009 11:08 AM

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?

Martin Honnen January 9th, 2009 11:16 AM

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.

joefawcett January 9th, 2009 11:28 AM

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.

joefawcett January 9th, 2009 11:31 AM

Martin's way is simpler, for version 1.0. I seem to have recursion on the brain at the moment [:)]


All times are GMT -4. The time now is 10:14 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.