Microsoft KB Archive/812198

= HOW TO: Pass Node Sets to Inline XSLT Script Functions by Using Visual Basic .NET =

Article ID: 812198

Article Last Modified on 1/23/2004

-

APPLIES TO


 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-





For a Microsoft Visual C# .NET version of this article, see 330602.



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



IN THIS TASK


 * SUMMARY
 * Create the Sample XML and XSLT Files
 * 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 Microsoft .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 the .NET Framework, you cannot unload assemblies that you create and load by using inline script in XSLT. Microsoft recommends that you do not use inline script blocks. You may use XSLT extension objects. For more information, see the &quot;References&quot; section of this article.

back to the top

Create the Sample XML and XSLT Files
  Paste the following code in Notepad, or another text editor:  Save the file as Q812198.xml .  In Notepad, paste the following code: 

   

<msxsl:script implements-prefix=&quot;user&quot; language=&quot;VB&quot;> Public Function ConvertDate(ByVal node As XPathNodeIterator) As String Dim dt As DateTime = DateTime.Parse(node.Current.Value) Return dt.ToString(&quot;f&quot;) End Function </msxsl:script>

</xsl:transform> </li> Save the file as DotNet.xslt </li></ol>

back to the top

Apply XSLT Transformation by Using Microsoft .NET Framework Classes
<ol> Create a new Visual Basic .NET ASP.NET Web Application project, and name it DateTimeDotNet. By default, WebForm1.aspx is created.</li> Right-click the designer surface of WebForm1.aspx, and then click View Code to edit the WebForm1.aspx.vb class module.</li>  Paste the following code in the using directives section of WebForm1.aspx.vb: Imports System.Xml.Xsl Imports System.Xml.XPath </li>  Paste the following code in the Page_Load event: Dim doc As XPathDocument = New XPathDocument(MapPath(&quot;Q812198.xml&quot;)) Dim transform As XslTransform = new XslTransform transform.Load(MapPath(&quot;DotNet.xslt&quot;)) ' Transform XML data. transform.Transform(doc, Nothing, Response.OutputStream) </li> Save changes to WebForm1.aspx.vb.</li> Make sure that the Q812198.xml and DotNet.xslt files are in the same folder where the project is created.</li> On 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/DateTimeXSLT/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 Visual Basic .NET ASP.NET Web Application project, and name it DateTimeMSXml .</li> On the Project menu, click Add Reference.</li> On the COM tab, click 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.vb class module.</li> <li> Paste the following code in the Page_Load event: Dim doc As MSXML2.DOMDocument = New MSXML2.DOMDocumentClass

doc.async = False doc.resolveExternals = False ' Load XML document. doc.load(Me.MapPath(&quot;Q812198.xml&quot;))

Dim xsl As MSXML2.DOMDocument = New MSXML2.DOMDocumentClass doc.async = False doc.resolveExternals = False ' Load XSLT document. xsl.load(Me.MapPath(&quot;msxml.xslt&quot;)) ' Transform XML data. Dim temp As String = doc.transformNode(xsl) Response.Write(temp) </li> <li>Save the changes to WebForm1.aspx.vb.</li> <li>Make sure that the Q812198.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/DateTimeXSLT/WebForm1.aspx </li></ol>

back to the top

<div class="references_section">