2

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.

I have two XML documents that are being retrieved as strings of XML from SQL Server. I want to use XslCompiledTransform to merge the two documents. I know that XslCompiledTransform turns off the XSL document() function by default. I have turned that on using XsltSettings when I create my XslCompiledTransform object.

My understanding about how to "add" the second document to the transformation is to use an XsltArgumentList and use the AddParam() method and add an XPathNavigator object:

XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParam(
  (string)e.UserState + "s", "http://www.myuri.com/tabledata", 
  dataXmlDoc.CreateNavigator()
);

However any attempts at accessing the document that is added results in either an error or nothing returned. — C#:

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
);

XSLT - Only accesses the added document, not the document loaded with the transform.

<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"
>
  <xsl:output method="html" indent="yes"/>

  <p>
  <xsl:template match="hlsdata:Address1s">
    <xsl:for-each select="hlsdata:Address1">
      <p>
        <xsl:value-of select="hlsdata:dr_id"/>
      </p>
    </xsl:for-each>
  </xsl:template>
  </p>

</xsl:stylesheet>

XML

<hlsdata:Address1s 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:hlsdata="http://www.myuri.com/tabledata"
>
  <hlsdata:Address1>
    <hlsdata:dr_id>12345678</hlsdata:dr_id>
  </hlsdata:Address1>
</hlsdata:Address1s>

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.

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.

2
  • I don't think that your XSLT would work at all in its current form. The <xsl:stylesheet> element does not allow <p> children Commented Mar 3, 2010 at 10:39
  • Tomalak, thanks for the reply. Like I mentioned the above code extracts were between many attempts to get this to work successfully. The <p> element was in there because at the point that I copied the xslt I was attempting to correct a "multiple root nodes" problem. Using Martin's examples below I was able to get my code to work. It took a little tweaking, but it is working. Thanks again for your reply. Commented Mar 4, 2010 at 20:56

1 Answer 1

4

You need to define a parameter in your stylesheet and then use that parameter. Here is a simple example, the stylesheet looks as follows:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:docs="http://example.com/2010/docs"
  exclude-result-prefixes="docs"
>
  <xsl:param name="docs:doc1" select="/.."/>

  <xsl:template match="/">
    <xsl:apply-templates select="$docs:doc1/node()"/>
  </xsl:template>

  <xsl:template match="root">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>

  <xsl:template match="foo">
    <li>
      <xsl:apply-templates/>
    </li>
  </xsl:template>
</xsl:stylesheet>

The C# code looks as follows:

    string xml = "<root><foo>1</foo><foo>2</foo></root>";
    XPathDocument doc = new XPathDocument(new StringReader(xml));

    XslCompiledTransform proc = new XslCompiledTransform();
    proc.Load(@"..\..\XSLTFile1.xslt");

    XsltArgumentList xsltArgs = new XsltArgumentList();
    xsltArgs.AddParam("doc1", "http://example.com/2010/docs", doc.CreateNavigator());

    proc.Transform(XmlReader.Create(new StringReader("<dummy/>")), xsltArgs, Console.Out);

This is a console application which for simplicity writes to Console.Out but you can of course use other outputs the Transform method allows.

That example then writes <ul><li>1</li><li>2</li></ul> so the input parameter has been processed.

So that should show you how to pass in a parameter that XslCompiledTransform sees as a node-set you can process with XSLT.

As for writing a stylesheet that merges two documents, please post two input samples and the corresponding result sample you want to create if you have problems writing that XSLT.

Sign up to request clarification or add additional context in comments.

2 Comments

Martin, Thank you for your assistance. Your example worked perfectly. I'm having some difficulty getting my code to work as well as yours, but I'm sure its just a matter of figuring out how to apply your techniques to my code.
Martin, I was able to massage your example to work with my xml, I needed to add the namespace to the elements, but I did get it to work. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.