Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this XML:
XML
<Root>
  <Employee>
    <Name>Dash</Name>
    <Age>23</Age>
  </Employee>
  <Employee>
    <Name>Gwen</Name>
    <Age>22</Age>
  </Employee>
</Root>

And I need to convert to below XML using XSLT:
<pre lang="xml"><Root>
  <Employee>
    <Name>Dash,Gwen</Name>
    <Age>23,22</Age>
  </Employee>
</Root
>

I am using the for-each loop to get the values of the sub-nodes of the <employee> node.The problem I am facing is I cannot figure out how to store the concatenated values in another temperoary variable in XSLT.I found on many sites that we cannot update the variables in the XSLT, so is there any alternative solution for this?
Posted

Not sure why you want to screw up a well formed XML document and create something that will be difficult to maintain, but who am I to judge.

Here is one solution.
XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Root">
      <Root>
        <Employee>
          <Name>
            <xsl:for-each select="Employee">
              <xsl:value-of select="Name"/>
              <xsl:if test="following-sibling::*">
                <xsl:text>,</xsl:text>
              </xsl:if>
            </xsl:for-each>
          </Name>
          <Age>
            <xsl:for-each select="Employee">
              <xsl:value-of select="Age"/>
              <xsl:if test="following-sibling::*">
                <xsl:text>,</xsl:text>
              </xsl:if>
            </xsl:for-each>
          </Age>
        </Employee>
      </Root>
    </xsl:template>

</xsl:stylesheet>
 
Share this answer
 
v2
Comments
_Asif_ 15-Sep-14 3:06am    
+5
George Jonsson 15-Sep-14 3:18am    
Thank's Syed.
Darshil H Pandya 16-Sep-14 5:26am    
You need to optimize your code sir!
George Jonsson 16-Sep-14 5:29am    
How do you mean?
It is not my code. I made it in order to help you.
It is your job to optimize.
Darshil H Pandya 16-Sep-14 5:37am    
Thank you very much for your efforts.
This is what I figured out:

I hope it will be helpful to others

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Root">
    <Root>
        <Employee>
            <Name>
                <xsl:apply-templates select="Employee/Name"/>
            </Name>
            <Age>
                <xsl:apply-templates select="Employee/Age"/>
            </Age>
        </Employee>
    </Root>
</xsl:template>

<xsl:template match="Name|Age">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900