How to pass Node sets to Inline XSLT Script Functions by Using Visual C# .NET (330602)
The information in this article applies to:
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.1
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# .NET (2003)
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
SUMMARYThis 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 topCreate the sample XML filePaste the following code in Notepad or another text editor, and
then save the file as Q330602.xml: <data>10/24/2002</data> back to the
topApply 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:
<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="C#">
public string ConvertDate(XPathNodeIterator node)
{
DateTime dt = DateTime.Parse(node.Current.Value);
return dt.ToString("f");
}
</msxsl:script>
</xsl:transform> - In Microsoft Visual Studio .NET, create a new ASP.NET Web
Application named DateTimeDotNet with Visual C# .NET.
- Right-click the designer surface of
WebForm1.aspx, and then click View Code to
edit the WebForm1.aspx.cs class module.
- Paste the following in the using directives section of WebForm1.aspx.cs:
using System.Xml.Xsl;
using System.Xml.XPath; - Paste the following code in the Page_Load event:
XPathDocument doc = new XPathDocument(this.MapPath("Q330602.xml"));
XslTransform transform = new XslTransform();
transform.Load(this.MapPath("DotNet.xslt"));
// Transform XML data.
transform.Transform(doc, null, Response.OutputStream); - Save changes to WebForm1.aspx.cs.
- Make sure that the Q330602.xml and DotNet.xslt files are in
the same folder where the project is created.
- On the Build menu, click Build
Solution.
- 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/DateTimeDotNet/WebForm1.aspx back
to the topApply XSLT transformation by using MSXML- 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> - Create a new ASP.NET Web Application project named
DateTimeMSXml by using Visual C# .NET.
- On the Project menu, click Add
Reference.
- On the COM tab, select Microsoft
XML, v3.0.
- Click Select, and then click
OK .
- Right-click the designer surface of
WebForm1.aspx, and then click View Code to
edit the WebForm1.aspx.cs class module.
- 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("Q330602.xml"));
MSXML2.DOMDocument xsl = new MSXML2.DOMDocumentClass();
doc.async = false;
doc.resolveExternals = false;
// Load XSLT document.
xsl.load(this.MapPath("msxml.xslt"));
// Transform XML data.
string temp = doc.transformNode(xsl);
Response.Write(temp); - Save the changes to WebForm1.aspx.cs.
- Make sure that the Q330602.xml and Msxml.xslt files are in
the folder where the project is created.
- On the Build menu, click Build
Solution.
- 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/DateTimeMSXml/WebForm1.aspx back
to the topREFERENCES
For more information about XSLT Transformation, click the following article number to view the article in the Microsoft Knowledge Base:
323370
How to use Extension objects when you execute XSL transformations in Visual C# .NET applications
back to the top
Modification Type: | Major | Last Reviewed: | 3/23/2006 |
---|
Keywords: | kbHOWTOmaster kbDateTime kbWebForms KB330602 kbAudDeveloper |
---|
|