title: Need help - ADD/Replace/Remove nodes in XML based on conditions using XSLT
body: <p>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.</p>
  
  <p>Thanks in advance for your help.</p>
  
  <p>Input XML # 1</p>
  
  <pre><code>&lt;Employee&gt;
  &lt;Summary&gt;
      &lt;Employee_ID&gt;12345&lt;/Employee_ID&gt;
      &lt;Name&gt;Mel Gibson&lt;/Name&gt;
  &lt;/Summary&gt;
  &lt;Personal&gt;
      &lt;First_Name&gt;Mel&lt;/First_Name&gt;
      &lt;Last_Name&gt;Gibson&lt;/Last_Name&gt;
  &lt;/Personal&gt;
  &lt;Status&gt;
      &lt;Active&gt;Yes&lt;/Active&gt;
      &lt;Base_Location&gt;Boston&lt;/Base_Location&gt;
  &lt;/Status&gt;
  &lt;Summary_Information&gt;
      &lt;Location&gt;California&lt;/Location&gt;
      &lt;Last_Formatted_Name&gt;Samuel Gibson&lt;/Last_Formatted_Name&gt;
  &lt;/Summary_Information&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>Condition to transform above XML is </p>
  
  <p>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. </p>
  
  <p>Output should look like as below. Please note Last_Name is holding the value of Last_Formatted_Name and Last_Formatted_Name is removed. </p>
  
  <pre><code>&lt;Employee&gt;
  &lt;Summary&gt;
      &lt;Employee_ID&gt;12345&lt;/Employee_ID&gt;
      &lt;Name&gt;Mel Gibson&lt;/Name&gt;
  &lt;/Summary&gt;
  &lt;Personal&gt;
      &lt;First_Name&gt;Mel&lt;/First_Name&gt;
      &lt;Last_Name&gt;Samuel Gibson&lt;/Last_Name&gt;
  &lt;/Personal&gt;
  &lt;Status&gt;
      &lt;Active&gt;Yes&lt;/Active&gt;
      &lt;Base_Location&gt;Boston&lt;/Base_Location&gt;
  &lt;/Status&gt;
  &lt;Summary_Information&gt;
      &lt;Location&gt;California&lt;/Location&gt;
  &lt;/Summary_Information&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>Input XML # 2</p>
  
  <pre><code>&lt;Employee&gt;
  &lt;Summary&gt;
      &lt;Employee_ID&gt;12345&lt;/Employee_ID&gt;
      &lt;Name&gt;Mel Gibson&lt;/Name&gt;
  &lt;/Summary&gt;
  &lt;Personal&gt;
      &lt;First_Name&gt;Mel&lt;/First_Name&gt;
  &lt;/Personal&gt;
  &lt;Status&gt;
      &lt;Active&gt;Yes&lt;/Active&gt;
      &lt;Base_Location&gt;Boston&lt;/Base_Location&gt;
  &lt;/Status&gt;
  &lt;Summary_Information&gt;
      &lt;Location&gt;California&lt;/Location&gt;
      &lt;Last_Formatted_Name&gt;Samuel Gibson&lt;/Last_Formatted_Name&gt;
  &lt;/Summary_Information&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>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</p>
  
  <p>Output should look like below. Please note Last_Name node is created right after First_Name and Last_Formatted_Name node is removed.</p>
  
  <pre><code>&lt;Employee&gt;
  &lt;Summary&gt;
      &lt;Employee_ID&gt;12345&lt;/Employee_ID&gt;
      &lt;Name&gt;Mel Gibson&lt;/Name&gt;
  &lt;/Summary&gt;
  &lt;Personal&gt;
      &lt;First_Name&gt;Mel&lt;/First_Name&gt;
      &lt;Last_Name&gt;Samuel Gibson&lt;/Last_Name&gt;
  &lt;/Personal&gt;
  &lt;Status&gt;
      &lt;Active&gt;Yes&lt;/Active&gt;
      &lt;Base_Location&gt;Boston&lt;/Base_Location&gt;
  &lt;/Status&gt;
  &lt;Summary_Information&gt;
      &lt;Location&gt;California&lt;/Location&gt;
  &lt;/Summary_Information&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>Following XSLT is what i have written to achieve this, but doesn't produce the output that i wanted. </p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  &lt;xsl:output method="xml" indent="yes"/&gt;
  &lt;xsl:template match="@*|node()" mode="#default"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Personal"&gt;
  
      &lt;xsl:if test="exists(Last_Name)"&gt;
      &lt;xsl:copy&gt;
              &lt;xsl:apply-templates/&gt;
          &lt;Last_Name&gt;
              &lt;xsl:value-of select="//Last_Formatted_Name"/&gt;
          &lt;/Last_Name&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Personal"&gt;
  &lt;xsl:if test="not(Last_Name)"&gt;
      &lt;xsl:copy&gt;
              &lt;xsl:apply-templates/&gt;
          &lt;Last_Name&gt;
              &lt;xsl:value-of select="//Last_Formatted_Name"/&gt;
          &lt;/Last_Name&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>