This article describes how to apply the Extensible Stylesheets Language (XSL) Transformations (XSLT) language to an Extensible Markup Language (XML) document by using the XslTransform class to create a new XML document. XSL is an XML-based language that is designed to transform either an XML document into another XML document or an XML document into any other structured document.
This example uses two files named Books.xml and Books.xsl. You can create your own Books.xml and Books.xsl files or use the sample files that are included with the .NET Software Development Kit (SDK) QuickStarts.
Create a new Console Application in Visual Basic .NET.
Make sure that the project contains a reference to the System.Xml namespace, and add a reference if it does not.
Use the Imports statement on the Xml, XPath, and Xsl namespaces so that you are not required to qualify declarations in those namespaces later in your code. You must use the Imports statement prior to any other declarations:
Declare the appropriate variables. Declare an XPathDocument object to hold the XML document and an XslTransform object to transform XML documents. Add the declaration code in the Main procedure of Module1:
Dim myXslTransform As XslTransformDim myXPathDocument As XPathDocument
Populate an XPathDocument object with the sample file, Books.xml. The XPathDocument class provides a fast and performance-oriented cache to process XML documents using XSLT. The XPathDocument class is similar to an XML Document Object Model (DOM) but is highly optimized for XSLT processing and the XPath data model.
'Open the XML. myXPathDocument = New XPathDocument("books.xml")
Construct a new XslTransform object. The XslTransform class is an XSLT processor that implements the XSL Transformations (XSLT) version 1.0 recommendation:
myXslTransform = New XslTransform()
Use the Load method to load the XslTransform object with the style sheet. This style sheet transforms the details of the Books.xsl document into a simple International Standard Book Number (ISBN) list of books:
Create an XmlTextWriter class with the new, transformed XML file name. Call the Transform method to initiate the transformation.
Dim writer as XmlTextWriterwriter = New XmlTextWriter("ISBNBooks.xml",System.Text.Encoding.UTF8)myXslTransform.Transform(myXPathDocument,Nothing, writer)writer.Flush()writer.Close()
Alternately, you can send the transformed XML document into an XmlReader, Stream, or TextWriter class. The following code sample sends the XML transformation to an instance of the StringWriter (a derivative of TextWriter), which in turn writes the transformation to the console window.
Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter()myXslTransform.Transform(myXPathDocument, Nothing, stWrite)Console.WriteLine(stWrite.ToString)Console.ReadLine() 'Pause
NOTE: The complete code listing uses the preceding code instead of the code in step 8.
Build and run your project.The results of the transformation are displayed in the console window as follows:
Imports System.XmlImports System.Xml.XPathImports System.Xml.XslModule Module1 Sub Main() Dim myXslTransform As XslTransform Dim myXPathDocument As XPathDocument myXPathDocument = New XPathDocument("books.xml") myXslTransform = New XslTransform() myXslTransform.Load("books.xsl") Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter() myXslTransform.Transform(myXPathDocument, Nothing, stWrite) Console.WriteLine(stWrite.ToString) Console.ReadLine() End SubEnd Module