DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 300929 - Last Review: April 26, 2006 - Revision: 5.2

This article was previously published under Q300929

On This Page

SUMMARY

This article illustrates how to apply an Extensible Stylesheet Language (XSL) Transformation (XSLT) to an Extensible Markup Language (XML) document using the XslTransform class to create a new XML document. XSL is an XML-based language that is designed to transform one XML document into another XML document or an XML document into any other structured document.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET
  • Microsoft .NET Software Development Kit (SDK) Quickstarts
This article assumes that you are familiar with the following topics:
  • XML terminology
  • Creating and reading an XML file
  • XML Path Language (XPath) syntax
  • XSL

Steps to build the sample

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. You must copy the Books.xml and Books.xsl files to the \Bin folder that is located underneath the folder in which you create this project. In Visual Studio .NET 2003, these files can be found in the following folder:
..\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\QuickStart\Howto\Samples\Xml\Transformxml\Vb
In Visual Studio .NET 2002, these files can be found in the following folder:
..\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Vb
  1. Create a new Console Application in Visual Basic .NET.
  2. Make sure that the project contains a reference to the System.Xml namespace, and add a reference if it does not.
  3. Use the Imports statement on the Xml 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:
    Imports System.Xml
    Imports System.Xml.Xsl
    					
  4. Declare the appropriate variables. Declare an XslTransform object to transform XML documents:
    Dim myXslTransform As XslTransform
    					
  5. Construct a new XslTransform object. The XslTransform class is an XSLT Processor that implements the XSLT version 1.0 recommendation:
    myXslTransform = New XslTransform()
  6. Use the Load method to load the XslTransform object with the style sheet. This style sheet transforms the details of the Books.xsl file into a simple International Standard Book Number (ISBN) list of books.
    myXslTransform.Load("books.xsl")
    					
  7. Call the Transform method to initiate the transformation, passing in the source XML document and the transformed XML document name:
    myXslTransform.Transform("books.xml", "ISBNBookList.xml")
    					
  8. Build and run your project. You can find the resultant ISBNBookList.xml file in the \Bin folder under your project file's folder.

Complete code sample

Imports System.Xml
Imports System.Xml.Xsl
Module Module1

    Sub Main()
        Dim myXslTransform As XslTransform
        myXslTransform = New XslTransform()
        myXslTransform.Load("books.xsl")
        myXslTransform.Transform("books.xml", "ISBNBookList.xml")
    End Sub

End Module
				

REFERENCES

For more information about the XslTransform class, see the following Microsoft .NET Framework Class Library documentation:
http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltransform(VS.71).aspx (http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltransform(VS.71).aspx)
For more information about the XslTransform class with the XslTransform object, see the following Microsoft .NET Framework Developer's Guide documentation:
http://msdn.microsoft.com/en-us/library/aa735781.aspx (http://msdn.microsoft.com/en-us/library/aa735781.aspx)
For a practical comparison of XSLT and ASP.NET, see the following MSDN Online Voices Extreme XML column:
http://msdn.microsoft.com/en-us/library/ms950754.aspx (http://msdn.microsoft.com/en-us/library/ms950754.aspx)
For more information about XML in .NET, see the "XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation" article from MSDN Magazine at the following Microsoft Web site:
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx (http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx)
For more information, refer to the following book:
R. Allen Wyke, Sultan Rehman, Brad Leupen. XML Programming (Core Reference). Microsoft Press, 2001
For more information, refer to the following Microsoft Training & Certification course:
Microsoft Corporation 2500 Introduction to XML and the Microsoft .NET Platform (http://www.microsoft.com/learning/syllabi/en-us/2500Afinal.mspx)

APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB300929
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support