I have to transform following two XMLs(Input XML # 1 and Input XML # 2) using XSLT. I would need your help to transform them using XSLT as I'm sturggling to find a solution.
Thanks in advance for your help.
Input XML # 1
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
<Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>
Condition to transform above XML is
If Last_Name node exists in the XML#1, that node should hold the value of Last_Formatted_Name after transformation and remove Last_Formatted_Name node from XML#1.
Output should look like as below. Please note Last_Name is holding the value of Last_Formatted_Name and Last_Formatted_Name is removed.
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
</Summary_Information>
</Employee>
Input XML # 2
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
<Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>
Condition to transform XML# 2 is: If Last_Name does'nt exist in input XML, a node Last_Name should be created right after First_Name node and should hold the value of Last_Formatted_Name
Output should look like below. Please note Last_Name node is created right after First_Name and Last_Formatted_Name node is removed.
<Employee>
<Summary>
<Employee_ID>12345</Employee_ID>
<Name>Mel Gibson</Name>
</Summary>
<Personal>
<First_Name>Mel</First_Name>
<Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
<Active>Yes</Active>
<Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
<Location>California</Location>
</Summary_Information>
</Employee>
Following XSLT is what i have written to achieve this, but doesn't produce the output that i wanted.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()" mode="#default">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Personal">
<xsl:if test="exists(Last_Name)">
<xsl:copy>
<xsl:apply-templates/>
<Last_Name>
<xsl:value-of select="//Last_Formatted_Name"/>
</Last_Name>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="Personal">
<xsl:if test="not(Last_Name)">
<xsl:copy>
<xsl:apply-templates/>
<Last_Name>
<xsl:value-of select="//Last_Formatted_Name"/>
</Last_Name>
</xsl:copy>
</xsl:if>
</xsl:template>
</Employee>node and hence not well-formed. Also your requirement is not clear. Do you want to use both the XMLs as input to the XSLT at the same time or you are looking for a generic XSLT for handling both the XMLs separately to transform the inputs in the desired output? - Aniket V