Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Apply an XSL Transformation to XML?

This sample illustrates how to apply an Extensible Stylesheet Language Transformations (XSLT) file to an XML document using the XslCompiledTransform class. This class implements the World Wide Web Consortium (W3C) W3C XSL Transformations (XSLT) version 1.0 specification. (For further reference, you might want to refer to W3C W3C Extensible Stylesheet Language (XSL) Version 1.0 specification.)

VB TransformXML.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xml\TransformXML_Retail\]

XSLT is an XML-based language designed to transform one XML document into another XML document or an XML document into any other structured document. XSLT uses the XML Path Language (XPath) to perform queries on an XML document in order to specify a particular part of the document. (For an example of how to use XPath to query an XML document, see How Do I...Query XML with an XPath expression?).

Often, the structure of an XML document does not match the structure needed to process the XML data. To transform the existing structure into one that can be processed, you use an XSLT processor and an XSLT style sheet (or XSLT file) that defines how to do the transformation. The XSLT file consists of templates that specify how each node of the source XML document should appear in the destination XML document. Thus, the XSLT processor holds three trees:

  1. One tree for the source XML.
  2. One tree for the destination structure.
  3. One tree for the XSLT file.

    The following diagram illustrates the transformation process: taking in an XML document, applying the XSLT file to the nodes of the XML document, and generating another document of the desired format.

    This sample consists of two functions. The first function simply reads an XML document and then transforms the data using an XSLT file. The second function reads an XML document, transforms the data, and then writes a new XML document from the transformed data.

    The sample applies these two functions to three sets of documents:

    1. The documents in the first set are the XML document, books.xml, and the XSLT file, books.xsl. The XSLT file consists of templates that get all the books in the bookstore element and then, for each book element, get the values for the ISBN attribute and the price element. There is also a template that tells the XSLT processor to start processing from the root ("/") node.
    2. The documents in the second set are the XML document, ProcessParametersA.xml, and the XSLT file, StyleSheetGenerator.xsl. In this case, the sample reads the XML document to find out which of two style sheets contained in the XSLT file to use. The sample then generates the specified style sheet.
    3. The documents in the third set are the XML document, ProcessParametersB.xml, and the XSLT file, StyleSheetGenerator.xsl. The sample processes these documents in the same way that it processes the second set of documents. However, in this case, the sample generates a different style sheet.

    The following code transforms data and writes the results to the screen. The Load method loads and compiles the style sheet. The Transform method performs the transformation.

    XslCompiledTransform myProcessor = new XslCompiledTransform();
    myProcessor.Transform(args[0], null, Console.Out);


    1. XSLT is a language for transforming XML documents into other XML documents using XPath as the query language.
    2. Use the Load method to compile an XSLT style sheet (or XSLT file).
    3. The Transform method performs the transformation.
    4. The XslCompiledTransform class can transform over any data that implements the IXPathNavigable interface.
    5. You can combine XSLT files to produce the required structure output format (for example, XML or HTML).
    6. XSLT processors do not manipulate documents. They manipulate document structure.

    Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
    Copyright � 2004 Microsoft Corporation. All rights reserved.