title: Merging two XPathDocuments using XmlCompiledTransform
body: <p>I can't possibly be the first person to do this, it seems like it would be such a common practice to merge two documents using XSLT.  However, I can't seem to find a single example on the ol' interweb.</p>
  
  <p>I have two XML documents that are being retrieved as strings of XML from SQL Server.  I want to use <code>XslCompiledTransform</code> to merge the two documents.  I know that <code>XslCompiledTransform</code> turns off the XSL <code>document()</code> function by default.  I have turned that on using <code>XsltSettings</code> when I create my <code>XslCompiledTransform</code> object. </p>
  
  <p>My understanding about how to "add" the second document to the transformation is to use an <code>XsltArgumentList</code> and use the <code>AddParam()</code> method and add an <code>XPathNavigator</code> object:</p>
  
  <pre><code>XsltArgumentList xsltArgs = new XsltArgumentList();
  xsltArgs.AddParam(
    (string)e.UserState + "s", "http://www.myuri.com/tabledata", 
    dataXmlDoc.CreateNavigator()
  );
  </code></pre>
  
  <p>However any attempts at accessing the document that is added results in either an error or nothing returned. &mdash; C#:</p>
  
  <pre><code>XslCompiledTransform fieldToXhtmlTransform = new XslCompiledTransform(true);
  try
  {
    UriBuilder xsltUri = new UriBuilder(
      Request.Url.Scheme, Request.Url.Host, 
      Request.Url.Port, this.ResolveUrl("Transforms/address1.xslt")
    );
  
    XmlSecureResolver resolver = new XmlSecureResolver(
      new XmlUrlResolver(), new PermissionSet(PermissionState.Unrestricted)
    );
    fieldToXhtmlTransform.Load(
      xsltUri.ToString(), new XsltSettings(true, false), resolver
    );
  }
  catch
  {
    //TODO: do something useful here. 
  }
  
  XPathDocument fieldSchemaXmlDoc = null;
  
  using (MemoryStream fieldMemoryStream = new MemoryStream(
    Encoding.UTF8.GetBytes(e.Result.TableMetaDataXml)
  ))
  {
    fieldSchemaXmlDoc = new XPathDocument(fieldMemoryStream);
  }
  
  XPathDocument dataXmlDoc = null;
  
  using (MemoryStream dataMemoryStream = new MemoryStream(
    Encoding.UTF8.GetBytes(e.Result.DataXml)
  ))
  {
    dataXmlDoc = new XPathDocument(dataMemoryStream);
  }
  
  StringBuilder output = new StringBuilder();
  
  XmlWriterSettings writerSettings = new XmlWriterSettings();
  writerSettings.OmitXmlDeclaration = true;
  writerSettings.Encoding = Encoding.UTF8;
  
  XsltArgumentList xsltArgs = new XsltArgumentList();
  xsltArgs.AddParam(
    (string)e.UserState + "s", "http://www.myuri.com/tabledata",
    dataXmlDoc.CreateNavigator()
  );    
  XmlWriter transformedDataWriter = XmlWriter.Create(output, writerSettings);
  fieldToXhtmlTransform.Transform(
    fieldSchemaXmlDoc, xsltArgs, transformedDataWriter
  );
  </code></pre>
  
  <p>XSLT - Only accesses the added document, not the document loaded with the transform.</p>
  
  <pre><code>&lt;xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:hlsschema="http://www.myuri.com/tableschema"
    xmlns:hlsdata="http://www.myuri.com/tabledata"
    exclude-result-prefixes="msxsl hlsschema hlsdata xsl"
  &gt;
    &lt;xsl:output method="html" indent="yes"/&gt;
  
    &lt;p&gt;
    &lt;xsl:template match="hlsdata:Address1s"&gt;
      &lt;xsl:for-each select="hlsdata:Address1"&gt;
        &lt;p&gt;
          &lt;xsl:value-of select="hlsdata:dr_id"/&gt;
        &lt;/p&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
    &lt;/p&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>XML</p>
  
  <pre><code>&lt;hlsdata:Address1s 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:hlsdata="http://www.myuri.com/tabledata"
  &gt;
    &lt;hlsdata:Address1&gt;
      &lt;hlsdata:dr_id&gt;12345678&lt;/hlsdata:dr_id&gt;
    &lt;/hlsdata:Address1&gt;
  &lt;/hlsdata:Address1s&gt;
  </code></pre>
  
  <p>I know I'm missing something obvious, but it is getting beyond frustrating.  I know the document gets added as a parameter, but I can't find an example of how to access a document loaded as a parameter.</p>
  
  <p>Any help would be greatly appreciated.  Keep in mind that the code above is a work in progress and is between two of hundreds of attempts to make it work so if something looks a bit odd, its probably because its between attempts.</p>