title: XSLT, Transform template result to a variable and dynamically construct the new XML
body: <p>I have an xml, expecting an processed xml via XSLT.
  Here, I could generate the xml by applying template and now I want to use this template output as an input(dynamically) for the another input within the same XSL.</p>
  
  <p>i.e. I want to pass the current output as input to the template output2.</p>
  
  <p>Output1 : first Template, resulting R1<br>
  Output2 : second Template, needs to generate the final output.</p>
  
  <p><strong>INPUT XML</strong></p>
  
  <pre><code>   &lt;rules&gt;
      &lt;emie&gt;
          &lt;domain exclude="true"&gt;sww-epw3.testing.com&lt;/domain&gt;
          &lt;domain&gt;sww-epw3.testing.com
  
              &lt;path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang"&gt;/Apps/dsprm&lt;/path&gt;
              &lt;path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang"&gt;/Apps/uaprm&lt;/path&gt;
              &lt;path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang"&gt;/Apps/ptprm&lt;/path&gt;
              &lt;path docMode="7" comment="APEXID:141923;MyRequestID:7512579;AppName:Production Revenue Accounting-PRA;StepOut:4 March 2016;StepIn:28 February 2017;Requester:PAUL.GEBBIE@testing.COM;BusinessUnit:UI;PortfolioManager:Per.Roseth@testing.com;Remarks:On Board Application"&gt;/apps/doto&lt;/path&gt;
              &lt;path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 December 2015;StepIn:;Requester:SAIKAT.MANNA@testing.COM;BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment"&gt;/apps/seprm&lt;/path&gt;
              &lt;path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 Dec 2015;StepIn:;Requester:SAIKAT.MANNA@testing.COM;BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment"&gt;/apps/gfprm&lt;/path&gt;
          &lt;/domain&gt;
      &lt;/emie&gt;
  &lt;/rules&gt;
  </code></pre>
  
  <p>**XSLT **</p>
  
  <pre><code>    &lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="xml" indent="yes"/&gt;
      &lt;xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/&gt;
      &lt;xsl:template match="/"&gt;
          &lt;xsl:copy-of select="$R2"/&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:variable name="R1"&gt;
          &lt;xsl:call-template name="output1" /&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:variable name="R2"&gt;
          &lt;xsl:call-template name="output2" /&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:template name="output2"&gt;
          &lt;xsl:element name="DOMAIN"&gt;
              &lt;xsl:element name="APEXID"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="MYREQUESTID"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="APPLICATION_NAME"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="URL"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="MODE"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="STEPOUT"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="STEPIN"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="BU"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="PORTFOLIO_MGR"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="REMARKS"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="RAG_STATUS"&gt;&lt;/xsl:element&gt;
              &lt;xsl:element name="APEX_DATA_PORTAL_URL"&gt;&lt;/xsl:element&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="output1"&gt;
          &lt;xsl:variable name="site"&gt;
              &lt;xsl:value-of select="rules/emie/domain/text()"/&gt;
          &lt;/xsl:variable&gt;
          &lt;emie&gt;
              &lt;xsl:for-each select="rules/emie/domain/path"&gt;
                  &lt;domain&gt;
                      &lt;xsl:element name="URL"&gt;
                          &lt;xsl:value-of select="concat($site,text())" /&gt;
                      &lt;/xsl:element&gt;
                      &lt;xsl:apply-templates select="@comment"/&gt;
                  &lt;/domain&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select="domain"&gt;
                  &lt;domain&gt;
                      &lt;xsl:element name="URL"&gt;
                          &lt;xsl:value-of select="concat($site,text())" /&gt;
                      &lt;/xsl:element&gt;
                      &lt;xsl:apply-templates select="@comment"/&gt;
                  &lt;/domain&gt;
              &lt;/xsl:for-each&gt;
          &lt;/emie&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="@comment" name="tokenize"&gt;
          &lt;xsl:param name="text" select="."/&gt;
          &lt;xsl:param name="separator" select="';'"/&gt;
          &lt;xsl:param name="separator2" select="':'"/&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="not(contains($text, $separator))"&gt;
                  &lt;xsl:choose&gt;
                      &lt;xsl:when test="(contains($text, $separator2))"&gt;
                          &lt;xsl:variable name="token" select="normalize-space(substring-before($text, $separator2))" /&gt;
                          &lt;xsl:element name="{$token}"&gt;
                              &lt;xsl:value-of select="normalize-space(substring-after($text, $separator2))"/&gt;
                          &lt;/xsl:element&gt;
                      &lt;/xsl:when&gt;
                      &lt;xsl:otherwise&gt;
                          &lt;xsl:element name="AppName"&gt;
                              &lt;xsl:value-of select="$text"/&gt;
                          &lt;/xsl:element&gt;
                      &lt;/xsl:otherwise&gt;
                  &lt;/xsl:choose&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:call-template name="tokenize"&gt;
                      &lt;xsl:with-param name="text" select="substring-before($text, $separator)"/&gt;
                  &lt;/xsl:call-template&gt;
                  &lt;xsl:call-template name="tokenize"&gt;
                      &lt;xsl:with-param name="text" select="substring-after($text, $separator)"/&gt;
                  &lt;/xsl:call-template&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>CURRENT OUTPUT</strong></p>
  
  <pre><code>&lt;emie&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/Apps/dsprm&lt;/URL&gt;
          &lt;APEXID&gt;102994&lt;/APEXID&gt;
          &lt;AppName&gt;Physical Records Management&lt;/AppName&gt;
          &lt;StepOut&gt;22 May 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;22 May 2016&lt;/StepIn&gt;
          &lt;Requester&gt;Per Roseth&lt;/Requester&gt;
          &lt;BusinessUnit&gt;Upstream&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;Junfeng Liang&lt;/PortfolioManager&gt;
      &lt;/domain&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/Apps/uaprm&lt;/URL&gt;
          &lt;APEXID&gt;102994&lt;/APEXID&gt;
          &lt;AppName&gt;Physical Records Management&lt;/AppName&gt;
          &lt;StepOut&gt;22 May 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;22 May 2016&lt;/StepIn&gt;
          &lt;Requester&gt;Per Roseth&lt;/Requester&gt;
          &lt;BusinessUnit&gt;Upstream&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;Junfeng Liang&lt;/PortfolioManager&gt;
      &lt;/domain&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/Apps/ptprm&lt;/URL&gt;
          &lt;APEXID&gt;102994&lt;/APEXID&gt;
          &lt;AppName&gt;Physical Records Management&lt;/AppName&gt;
          &lt;StepOut&gt;22 May 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;22 May 2016&lt;/StepIn&gt;
          &lt;Requester&gt;Per Roseth&lt;/Requester&gt;
          &lt;BusinessUnit&gt;Upstream&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;Junfeng Liang&lt;/PortfolioManager&gt;
      &lt;/domain&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/doto&lt;/URL&gt;
          &lt;APEXID&gt;141923&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7512579&lt;/MyRequestID&gt;
          &lt;AppName&gt;Production Revenue Accounting-PRA&lt;/AppName&gt;
          &lt;StepOut&gt;4 March 2016&lt;/StepOut&gt;
          &lt;StepIn&gt;28 February 2017&lt;/StepIn&gt;
          &lt;Requester&gt;PAUL.GEBBIE@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UI&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;Per.Roseth@testing.com&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;On Board Application&lt;/Remarks&gt;
      &lt;/domain&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/seprm&lt;/URL&gt;
          &lt;APEXID&gt;&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7219068&lt;/MyRequestID&gt;
          &lt;AppName&gt;&lt;/AppName&gt;
          &lt;StepOut&gt;9 December 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;&lt;/StepIn&gt;
          &lt;Requester&gt;SAIKAT.MANNA@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UA&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;Technical Assessment&lt;/Remarks&gt;
      &lt;/domain&gt;
      &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/gfprm&lt;/URL&gt;
          &lt;APEXID&gt;&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7219068&lt;/MyRequestID&gt;
          &lt;AppName&gt;&lt;/AppName&gt;
          &lt;StepOut&gt;9 Dec 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;&lt;/StepIn&gt;
          &lt;Requester&gt;SAIKAT.MANNA@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UA&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;Technical Assessment&lt;/Remarks&gt;
      &lt;/domain&gt;
  &lt;/emie&gt;
  </code></pre>
  
  <p><strong>EXPECTED OUTPUT- Need to include more new elements and process the data of input xml (output1) e.g date formating</strong></p>
  
  <pre><code>    &lt;emie&gt;&lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/gfprm&lt;/URL&gt;
          &lt;APEXID&gt;&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7219068&lt;/MyRequestID&gt;
          &lt;AppName&gt;&lt;/AppName&gt;
          &lt;StepOut&gt;9 Dec 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;&lt;/StepIn&gt;
          &lt;Requester&gt;SAIKAT.MANNA@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UA&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;Technical Assessment&lt;/Remarks&gt; &lt;APEX_DATA_PORTAL_URL&gt;somesite&lt;/APEX_DATA_PORTAL_URL&gt;
      &lt;/domain&gt;
    &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/gfprm&lt;/URL&gt;
          &lt;APEXID&gt;&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7219068&lt;/MyRequestID&gt;
          &lt;AppName&gt;&lt;/AppName&gt;
          &lt;StepOut&gt;9 Dec 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;&lt;/StepIn&gt;
          &lt;Requester&gt;SAIKAT.MANNA@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UA&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;Technical Assessment&lt;/Remarks&gt; &lt;APEX_DATA_PORTAL_URL&gt;somesite&lt;/APEX_DATA_PORTAL_URL&gt;
      &lt;/domain&gt;
    &lt;domain&gt;
          &lt;URL&gt;sww-epw3.testing.com/apps/gfprm&lt;/URL&gt;
          &lt;APEXID&gt;&lt;/APEXID&gt;
          &lt;MyRequestID&gt;7219068&lt;/MyRequestID&gt;
          &lt;AppName&gt;&lt;/AppName&gt;
          &lt;StepOut&gt;9 Dec 2015&lt;/StepOut&gt;
          &lt;StepIn&gt;&lt;/StepIn&gt;
          &lt;Requester&gt;SAIKAT.MANNA@testing.COM&lt;/Requester&gt;
          &lt;BusinessUnit&gt;UA&lt;/BusinessUnit&gt;
          &lt;PortfolioManager&gt;&lt;/PortfolioManager&gt;
          &lt;Remarks&gt;Technical Assessment&lt;/Remarks&gt; &lt;APEX_DATA_PORTAL_URL&gt;somesite&lt;/APEX_DATA_PORTAL_URL&gt;
      &lt;/domain&gt;&lt;/emie&gt;
  </code></pre>