Microsoft KB Archive/330602

= How to pass Node sets to Inline XSLT Script Functions by Using Visual C# .NET =

Article ID: 330602

Article Last Modified on 3/23/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q330602





For a Microsoft Visual Basic .NET version of this article, see 812198.

This article references the following Microsoft .NET Framework Class Library namespaces:
 * System.Xml.Xsl
 * System.Xml.XPath



IN THIS TASK
 * SUMMARY
 * Create the sample XML file
 * Apply XSLT transformation by using Microsoft .NET Framework classes
 * Apply XSLT transformation by using MSXML
 * REFERENCES



SUMMARY
This step-by-step article describes how to pass a node or nodes to an inline script function in the .NET Framework. You can use inline script blocks or an XSLT extension object to pass a node to a function. In the .NET Framework, nodes are passed as System.Xml.XPath.XPathNodeIterator. In MSXML, nodes are passed as an IXMLDOMNodeList object to inline XSLT script functions.

Microsoft recommends that you avoid using inline script blocks. Instead, you can use extension objects.back to the top

Create the sample XML file
Paste the following code in Notepad or another text editor, and then save the file as Q330602.xml: back to the top

Apply XSLT transformation by using Microsoft .NET Framework classes
  Paste the following code in Notepad or another text editor, and then save the file as DotNet.xslt: 

   

 public string ConvertDate(XPathNodeIterator node) {       DateTime dt = DateTime.Parse(node.Current.Value); return dt.ToString(&quot;f&quot;); } 

  In Microsoft Visual Studio .NET, create a new ASP.NET Web Application named DateTimeDotNet with Visual C# .NET.</li> Right-click the designer surface of WebForm1.aspx, and then click View Code to edit the WebForm1.aspx.cs class module.</li>  Paste the following in the using directives section of WebForm1.aspx.cs: using System.Xml.Xsl; using System.Xml.XPath; </li>  Paste the following code in the Page_Load event: XPathDocument doc = new XPathDocument(this.MapPath(&quot;Q330602.xml&quot;)); XslTransform transform = new XslTransform; transform.Load(this.MapPath(&quot;DotNet.xslt&quot;)); // Transform XML data. transform.Transform(doc, null, Response.OutputStream); </li> Save changes to WebForm1.aspx.cs.</li> Make sure that the Q330602.xml and DotNet.xslt files are in the same folder where the project is created.</li> On the Build menu, click Build Solution.</li>  Start Microsoft Internet Explorer and open the WebForm1.aspx file. To do this, specify the following URL, where  is the name of your Microsoft Internet Information Services (IIS) server: http://IISServerName/DateTimeDotNet/WebForm1.aspx </li></ol>

back to the top

Apply XSLT transformation by using MSXML
<ol>  Paste the following code in Notepad or another text editor, and then save the file as Msxml.xslt: <?xml version='1.0'?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot; xmlns:user=&quot;http://my_domain_name/my_namespace&quot;>

<msxsl:script language=&quot;VBScript&quot; implements-prefix=&quot;user&quot;> Function ConvertDate(nodeList) ConvertDate = FormatDateTime( Left(nodeList(0).text, 10),1) End Function </msxsl:script>

 <xsl:value-of select=&quot;user:ConvertDate(data)&quot;/> </xsl:template>

</xsl:stylesheet> </li> Create a new ASP.NET Web Application project named DateTimeMSXml by using Visual C# .NET.</li> On the Project menu, click Add Reference.</li> On the COM tab, select Microsoft XML, v3.0.</li> Click Select, and then click OK .</li> Right-click the designer surface of WebForm1.aspx, and then click View Code to edit the WebForm1.aspx.cs class module.</li>  Paste the following code in the Page_Load event: MSXML2.DOMDocument doc = new MSXML2.DOMDocumentClass; doc.async = false; doc.resolveExternals = false; // Load XML document. doc.load(this.MapPath(&quot;Q330602.xml&quot;));

MSXML2.DOMDocument xsl = new MSXML2.DOMDocumentClass; doc.async = false; doc.resolveExternals = false; // Load XSLT document. xsl.load(this.MapPath(&quot;msxml.xslt&quot;)); // Transform XML data. string temp = doc.transformNode(xsl); Response.Write(temp); </li> <li>Save the changes to WebForm1.aspx.cs.</li> <li>Make sure that the Q330602.xml and Msxml.xslt files are in the folder where the project is created.</li> <li>On the Build menu, click Build Solution.</li> <li> Start Internet Explorer and open the WebForm1.aspx file. To do this, specify the following URL, where  is the name of your IIS server: http://IISServerName/DateTimeMSXml/WebForm1.aspx </li></ol>

back to the top

<div class="references_section">