By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

XSLT: Combine Contents of Elements with the Same Name

P: 2
Hi there, I'm a little new to XSLT and I've run across this problem.
I need to go through a document and combine the contents of similarly named elements together. I hope you can help me, thanks.

Example input:


Required output:

<red>Car,Motorcycle, Truck</red>
<blue>Minivan, Sportscar</blue>
<green>Truck, Car</green>
Oct 10 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 2,057
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:fo="">
  3.     <xsl:template match="/*">
  4.         <xsl:copy>
  5.             <xsl:for-each select="/group/*">
  6.                 <xsl:variable name="name" select="local-name()"/>
  7.                 <xsl:if test="not(preceding-sibling::*[local-name() = $name])">
  8.                     <xsl:copy>
  9.                         <xsl:value-of select="."/>
  10.                         <xsl:for-each select="following-sibling::*[local-name() = $name]">
  11.                             <xsl:text>, </xsl:text>
  12.                             <xsl:value-of select="."/>
  13.                         </xsl:for-each>
  14.                     </xsl:copy>
  15.                 </xsl:if>
  16.             </xsl:for-each>
  17.         </xsl:copy>
  18.     </xsl:template>
  19. </xsl:stylesheet>
Oct 10 '07 #2

P: 2
I didn't even know half of that was possible, especially in 1.0.

Thanks a lot

(I am only an egg.)
Oct 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.