title: Merging two list with xslt
body: <p>Basically I need to combine the output from two different xslt transforms.</p>
  
  <p>I'm using "container.xslt" to import and combine the two inner xslts: "transform1.xslt" and "transform2.xslt"  </p>
  
  <p>The two transform files are generated to by an xslt tooling program so I would like to avoid making changes to them.</p>
  
  <p>This is my input xml:</p>
  
  <pre><code>&lt;Enrollments&gt;
      &lt;Enrollment&gt;
          &lt;MEDICAL id="11111111"&gt;
          &lt;/MEDICAL&gt;
      &lt;/Enrollment&gt;
  &lt;/Enrollments&gt;
  </code></pre>
  
  <p>This is the output I'm currently getting:</p>
  
  <pre><code>&lt;fields&gt;
      &lt;fields&gt;
          &lt;field name="ID1"&gt;
              &lt;value&gt;11111111&lt;/value&gt;
          &lt;/field&gt;
      &lt;/fields&gt;
      &lt;fields&gt;
          &lt;field name="ID2"&gt;
              &lt;value&gt;11111111&lt;/value&gt;
          &lt;/field&gt;
      &lt;/fields&gt;
  &lt;/fields&gt;
  </code></pre>
  
  <p>Here is the output that I'm looking for:</p>
  
  <pre><code>&lt;fields&gt;
      &lt;field name="ID1"&gt;
          &lt;value&gt;11111111&lt;/value&gt;
      &lt;/field&gt;
      &lt;field name="ID2"&gt;
          &lt;value&gt;11111111&lt;/value&gt;
      &lt;/field&gt;
  &lt;/fields&gt;
  </code></pre>
  
  <p>container.xslt</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn"&gt;
  
      &lt;xsl:include href="./transform1.xslt"/&gt;
      &lt;xsl:include href="./transform2.xslt"/&gt;
  
      &lt;xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="yes"/&gt;
  
      &lt;xsl:variable name="list1"&gt;
          &lt;xsl:call-template name="one" /&gt;
      &lt;/xsl:variable&gt;
  
      &lt;xsl:param name="list2"&gt;
          &lt;xsl:call-template name="two"/&gt;
      &lt;/xsl:param&gt;
  
      &lt;xsl:template name="mergedList" match="/"&gt;
          &lt;fields&gt;
              &lt;xsl:copy-of select="$list1" /&gt;
              &lt;xsl:copy-of select="$list2" /&gt;
          &lt;/fields&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>transform1.xslt</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn"&gt;
          &lt;xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="yes"/&gt;
          &lt;xsl:template match="/" name="one"&gt;
              &lt;fields&gt;
                  &lt;field&gt;
                      &lt;xsl:attribute name="name" select="'ID1'"/&gt;
                      &lt;xsl:for-each select="Enrollments/Enrollment/MEDICAL"&gt;
                          &lt;value&gt;
                              &lt;xsl:sequence select="fn:string(@id)"/&gt;
                          &lt;/value&gt;
                      &lt;/xsl:for-each&gt;
                  &lt;/field&gt;
              &lt;/fields&gt;
          &lt;/xsl:template&gt;
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>transform2.xslt</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn"&gt;
      &lt;xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="yes"/&gt;
      &lt;xsl:template match="/" name="two"&gt;
          &lt;fields&gt;
              &lt;field&gt;
                  &lt;xsl:attribute name="name" select="'ID2'"/&gt;
                  &lt;xsl:for-each select="Enrollments/Enrollment/MEDICAL"&gt;
                      &lt;value&gt;
                          &lt;xsl:sequence select="fn:string(@id)"/&gt;
                      &lt;/value&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/field&gt;
          &lt;/fields&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>It might belong in another question entirely, but I'll add it here in case anyone has a suggestion.  I had to add these two lines to my transform files to allow me to use named templates in container.xslt.  It would be ideal to avoid changing the imported xlst</p>
  
  <pre><code>&lt;xsl:attribute name="name" select="'ID1'"/&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;xsl:attribute name="name" select="'ID2'"/&gt;
  </code></pre>
  
  <p>Thanks in advance for any suggestions! I'm fairly new to xslt and haven't hand written much.</p>