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



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

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

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 "References" section of this article.

back to the top

Create the Sample XML and XSLT Files

  1. Paste the following code in Notepad, or another text editor:
    <data>10/24/2002</data>
  2. Save the file as Q812198.xml.
  3. In Notepad, paste the following code:
    <xsl:transform version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="http://my_domain_name/my_namespace">
    
    <xsl:output method="xml" omit-xml-declaration="yes"/>
      
     <xsl:template match="/">
        <xsl:value-of select="user:ConvertDate(data)"/>
     </xsl:template>
    
     <msxsl:script implements-prefix="user" language="VB">
     Public Function ConvertDate(ByVal node As XPathNodeIterator) As String
    	Dim dt As DateTime = DateTime.Parse(node.Current.Value)
    	Return dt.ToString("f")
     End Function
     </msxsl:script>
    
    </xsl:transform>
  4. Save the file as DotNet.xslt
back to the top

Apply XSLT Transformation by Using Microsoft .NET Framework Classes

  1. Create a new Visual Basic .NET ASP.NET Web Application project, and name it DateTimeDotNet. By default, WebForm1.aspx is created.
  2. Right-click the designer surface of WebForm1.aspx, and then click View Code to edit the WebForm1.aspx.vb class module.
  3. Paste the following code in the using directives section of WebForm1.aspx.vb:
    Imports System.Xml.Xsl
    Imports System.Xml.XPath
  4. Paste the following code in the Page_Load event:
    Dim doc As XPathDocument = New XPathDocument(MapPath("Q812198.xml"))
    Dim transform As XslTransform = new XslTransform()
    transform.Load(MapPath("DotNet.xslt"))
    ' Transform XML data.
    transform.Transform(doc, Nothing, Response.OutputStream)
  5. Save changes to WebForm1.aspx.vb.
  6. Make sure that the Q812198.xml and DotNet.xslt files are in the same folder where the project is created.
  7. On Build menu, click Build Solution.
  8. Start Microsoft Internet Explorer and open the WebForm1.aspx file. To do this, specify the following URL, where IISServerName is the name of your Microsoft Internet Information Services (IIS) server:
    http://IISServerName/DateTimeXSLT/WebForm1.aspx
back to the top

Apply XSLT Transformation by Using MSXML

  1. 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="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:msxsl="urn:schemas-microsoft-com:xslt"
          xmlns:user="http://my_domain_name/my_namespace">
    
    <msxsl:script language="VBScript" implements-prefix="user">
    Function ConvertDate(nodeList)
    	ConvertDate = FormatDateTime( Left(nodeList(0).text, 10),1)
    End Function
    </msxsl:script>
    
    <xsl:template match="/">
       <xsl:value-of select="user:ConvertDate(data)"/>
    </xsl:template>
    
    </xsl:stylesheet>
  2. Create a new Visual Basic .NET ASP.NET Web Application project, and name it DateTimeMSXml.
  3. On the Project menu, click Add Reference.
  4. On the COM tab, click Microsoft XML, v3.0.
  5. Click Select, and then click OK.
  6. Right-click the designer surface of WebForm1.aspx, and then click View Code to edit the WebForm1.aspx.vb class module.
  7. 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("Q812198.xml"))
    
          Dim xsl As MSXML2.DOMDocument = New MSXML2.DOMDocumentClass()
          doc.async = False
          doc.resolveExternals = False
          ' Load XSLT document.
          xsl.load(Me.MapPath("msxml.xslt"))
          ' Transform XML data.
          Dim temp As String = doc.transformNode(xsl)
          Response.Write(temp)
  8. Save the changes to WebForm1.aspx.vb.
  9. Make sure that the Q812198.xml and Msxml.xslt files are in the folder where the project is created.
  10. On the Build menu, click Build Solution.
  11. Start Internet Explorer and open the WebForm1.aspx file. To do this, specify the following URL, where IISServerName is the name of your IIS server:
    http://IISServerName/DateTimeXSLT/WebForm1.aspx
back to the top

REFERENCES

For additional information about XSLT Transformation, click the following article numbers to view the articles in the Microsoft Knowledge Base:

321702 HOW TO: Use Extension Objects When You Execute XSL Transformations in Visual Basic .NET Applications

316775 PRB: Cannot Unload Assemblies That You Create and Load by Using Script in XSLT


back to the top


Modification Type:MajorLast Reviewed:1/23/2004
Keywords:kbXML kbHOWTOmaster kbDateTime kbWebForms KB812198 kbAudDeveloper