title: How to add additional element at the end of the record in XSLT
body: <p>I need to add an additional element at the end of the record. The siblings of the added element are only optional, which can be present or not. As long as the <code>&lt;Property&gt;</code> element will be added at the end. There are 2 scenarios that can be done: 1st scenario, if <code>&lt;Property&gt;</code> element is already present in the testfile, it should retain as is but I need to add another occurrence of <code>&lt;User&gt;</code> element under the <code>&lt;Property&gt;</code> element. 2nd scenario, if the  element is not present I need to add this <code>&lt;Property&gt;</code> at the end or it should be placed after all the elements of <code>&lt;Data&gt;</code>. The other elements are only optional.</p>
  
  <p>I need to add this additional part:</p>
  
  <pre><code>&lt;Property&gt;
      &lt;User&gt;
         &lt;Value listID="AAA"&gt;Sample testing&lt;/Value&gt;
      &lt;/User&gt;
  &lt;/Property&gt;
  </code></pre>
  
  <p><strong>1st scenario example:</strong> <em><code>&lt;Property&gt;</code> is present in the testfile</em></p>
  
  <p><strong>INPUT</strong></p>
  
  <pre><code>&lt;Record&gt;
     &lt;Data&gt;
        &lt;Date&gt;2017-04-25&lt;/Date&gt;
        &lt;Name&gt;John Kledd&lt;/Name&gt;
        &lt;Address&gt;
           &lt;BuildingNumber&gt;4603&lt;/BuildingNumber&gt;
        &lt;/Address&gt;
        &lt;Property&gt;
           &lt;User&gt;
              &lt;Value listID="123"&gt;Example Only&lt;/Value&gt;
           &lt;/User&gt;
        &lt;/Property&gt;
     &lt;/Data&gt;
  &lt;/Record&gt;
  </code></pre>
  
  <p><strong>EXPECTED OUTPUT</strong></p>
  
  <pre><code>&lt;Record&gt;
     &lt;Data&gt;
        &lt;Date&gt;2017-04-25&lt;/Date&gt;
        &lt;Name&gt;John Kledd&lt;/Name&gt;
        &lt;Address&gt;
           &lt;BuildingNumber&gt;4603&lt;/BuildingNumber&gt;
        &lt;/Address&gt;
        &lt;Property&gt;
           &lt;User&gt;
              &lt;Value listID="123"&gt;Example Only&lt;/Value&gt;
           &lt;/User&gt;
           &lt;User&gt;
              &lt;Value listID="AAA"&gt;Sample testing&lt;/Value&gt;
           &lt;/User&gt;
        &lt;/Property&gt;
     &lt;/Data&gt;
  &lt;/Record&gt;
  </code></pre>
  
  <p><strong>2nd scenario example:</strong> <em><code>&lt;Property&gt;</code> is not present in the testfile</em> </p>
  
  <p><strong>INPUT</strong></p>
  
  <pre><code>&lt;Record
      &lt;Data&gt;
        &lt;Date&gt;2017-04-25&lt;/Date&gt;
        &lt;Name&gt;John Kledd&lt;/Name&gt;
        &lt;Address&gt;
           &lt;BuildingNumber&gt;4603&lt;/BuildingNumber&gt;
        &lt;/Address&gt;
     &lt;/Data&gt;
  &lt;/Record&gt;
  </code></pre>
  
  <p><strong>EXPECTED OUTPUT</strong></p>
  
  <pre><code>&lt;Record&gt;
     &lt;Data&gt;
        &lt;Date&gt;2017-04-25&lt;/Date&gt;
        &lt;Name&gt;John Kledd&lt;/Name&gt;
        &lt;Address&gt;
           &lt;BuildingNumber&gt;4603&lt;/BuildingNumber&gt;
        &lt;/Address&gt;
        &lt;Property&gt;
           &lt;User&gt;
              &lt;Value listID="AAA"&gt;Sample testing&lt;/Value&gt;
           &lt;/User&gt;
        &lt;/Property&gt;
     &lt;/Data&gt;
  &lt;/Record&gt;
  </code></pre>
  
  <p>Here is my XSLT I used for both scenarios:</p>
  
  <p><strong>XSLT</strong></p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
  &lt;!-- identity transform --&gt;
  &lt;xsl:template match="@*|node()"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  &lt;!-- This part deletes the Property element --&gt;
  &lt;xsl:template match="Data/Property"/&gt;
  &lt;xsl:template match="Data/Address"&gt;
      &lt;xsl:copy-of select="."/&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="not(following-sibling::Property)"&gt;
              &lt;xsl:element name="Property"&gt;
                  &lt;xsl:element name="User"&gt;
                      &lt;xsl:element name="Value"&gt;
                          &lt;xsl:attribute name="name"&gt;AAA&lt;/xsl:attribute&gt;
                          &lt;xsl:value-of select="'Sample Testing'"/&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:element name="Property"&gt;
                  &lt;xsl:element name="User"&gt;
                      &lt;xsl:element name="Value"&gt;
                          &lt;xsl:attribute name="name"&gt;&lt;xsl:value-of select="../UserArea/Property/NameValue/@name"/&gt;&lt;/xsl:attribute&gt;
                          &lt;xsl:value-of select="../Property/User/Value"/&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:element&gt;
                  &lt;xsl:element name="User"&gt;
                      &lt;xsl:element name="Value"&gt;
                          &lt;xsl:attribute name="name"&gt;AAA&lt;/xsl:attribute&gt;
                          &lt;xsl:value-of select="'Sample Testing'"/&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>My XSLT is working for both scenarios, however, it didn't work if I removed the <code>&lt;Address&gt;</code> element. I am using XSLT v2.0, apology if it is too long. Thank you for your help.</p>
  
  <p>Regards,</p>