posted on Friday, May 21, 2004 7:45 PM
by
jdixon
Pass an XML node set to XSLT from .NET
I recently needed to pass some XML into my XSLT before performing a transform. Normally, this isn't very useful, because the XML gets converted into a string, not a node set. However, .NET provides a way around this problem. Because it took quite a while to discover the method, I thought I would note it here.
.NET Snippet
// create an XsltArgumentList to contain the node set
myArgumentList = new XsltArgumentList();
// load the nodes
NodeDoc = new XmlDocument();
NodeDoc.LoadXml(<Place XML here>);
// create a navigator, so that an iterator can be created
NodeNav = NodeDoc.CreateNavigator();
NodeItr = NodeNav.Select("/Parent/Child");
// populate argument list
myArgumentList.AddParam("NodeInfo", "", NodeItr);
// transform the XML directly to the file
myTransform.Transform(myXPathDoc, myArgumentList, OutputFile, new XmlUrlResolver());
XSLT Snippet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vc="http://www.me.com/Codes">
<xsl:param name="NodeInfo" select="/.."/>
<xsl:template match="/">
<xsl:for-each select="$NodeInfo">
...
</xsl:for-each>
</xsl:stylesheet>