Microsoft KB Archive/815655

= How to apply an XSL transformation to XML for streaming by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815655

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-





For a Microsoft Visual C# .NET version of this article, see 307494.



For a Microsoft Visual Basic .NET version of this article, see 300934.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::Xml
 * System::Xml::Xsl
 * System::Xml::XPath
 * System::IO

IN THIS TASK

 * Summary
 * Requirements
 * How to Apply XSL Transformations
 * Complete Code Sample
 * References



SUMMARY
This step-by-step article describes how to apply the Extensible Stylesheet Language (XSL) Transformation (XSLT) language to an 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 to another XML document or to transform an XML document to any other structured document.

back to the top



Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you may require:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Visual Studio .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

back to the top

How to Apply XSL Transformations
This example uses two files that are named Books.xml and Books.xsl. You can create your own Books.xml file and Books.xsl file, or you can use the sample files that are included with the .NET Software Development Kit QuickStarts.

Important You must copy the Books.xml file and the Books.xsl file to the folder that you create for this project. You can locate these files in the following folder:

...\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs

For Visual C++ .NET (2003), you can locate these files in the following folder:

...\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\QuickStart\howto\samples\xml\transformxml\cs

 Create a new Managed C++ application in Visual C++ .NET 2002, create a new Console application in Visual C++ .NET 2003, or create a new Console application in Visual C++ 2005.  Add the following code to add a reference to the System.xml namespace:  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 function: Xml::Xsl::XslTransform* pMyXslTransform = new Xml::Xsl::XslTransform; Xml::XPath::XPathDocument* pMyXPathDocument = new Xml::XPath::XPathDocument(S&quot;books.xml&quot;);   Populate an XPathDocument object with the sample file, Books.xml.
 * 1) using  

The XPathDocument class provides a fast and performance-oriented cache to process XML documents by using XSLT. The XPathDocument class is similar to an XML Document Object Model (DOM), but the XPathDocument class is highly optimized for XSLT processing and for the XPath data model: Xml::XPath::XPathDocument* pMyXPathDocument = new Xml::XPath::XPathDocument(&quot;books.xml&quot;);   Construct a new XslTransform object.

The XslTransform class is an XSLT processor that implements the XSL Transformations (XSLT) version 1.0 recommendation: Xml::Xsl::XslTransform *pMyXslTransform = new Xml::Xsl::XslTransform; </li>  Use the Load method to load the XslTransform object with the style sheet.

This style sheet transforms the details of the Books.xsl document to a simple International Standard Book Number (ISBN) list of books: pMyXslTransform->Load(S&quot;Books.xsl&quot;); </li>  Create an XmlTextWriter class with the new, transformed XML file name. Call the Transform method to initiate the transformation. Xml::XmlTextWriter* pWriter = new Xml::XmlTextWriter(S&quot;ISBNBooks.xml&quot;,System::Text::Encoding::UTF8);

// In Visual C++ .NET 2002, write the following line as    // pMyXslTransform->Transform(pMyXPathDocument,NULL,pWriter):

pMyXslTransform->Transform(pMyXPathDocument,NULL,pWriter,0); pWriter->Flush ; pWriter->Close ; </li>  Alternatively, you may send the transformed XML document to an XmlReader class, to a Stream class, or to a TextWriter class. The following code sample sends the XML transformation to an instance of the StringWriter class (a derivative of TextWriter) that in turn writes the transformation to the console window. System::IO::StringWriter* pStWriter = new System::IO::StringWriter; // In Visual C++ .NET 2002, write the following line as   // pMyXslTransform->Transform(pMyXPathDocument,NULL,pStWriter).

pMyXslTransform->Transform(pMyXPathDocument,NULL,pStWriter,0); Console::WriteLine (pStWriter->ToString ); Console::ReadLine; Note The complete code listing uses the previous code instead of the code in step 7. </li>  Build and then run your project. The results of the transformation appear in the console window: <?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?> <book ISBN=&quot;1-861003-11-0&quot;> 8.99 <book ISBN=&quot;0-201-63361-2&quot;> 11.99 <book ISBN=&quot;1-861001-57-6&quot;> 9.99 </li></ol>

back to the top

Complete Code Sample

 * 1) include &quot;stdafx.h&quot;
 * 2) using <mscorlib.dll>
 * 3) include <tchar.h>

using namespace System;
 * 1) using <System.XML.Dll>

// This is the entry point for this application. int _tmain(void) {   try{ Xml::XPath::XPathDocument* pMyXPathDocument = new Xml::XPath::XPathDocument(S&quot;books.xml&quot;); Xml::Xsl::XslTransform *pMyXslTransform = new Xml::Xsl::XslTransform; pMyXslTransform->Load(S&quot;Books.xsl&quot;);

System::IO::StringWriter* pStWriter = new System::IO::StringWriter; // In Visual C++ .NET 2002, write the following line as        // pMyXslTransform->Transform(pMyXPathDocument,NULL,pStWriter). pMyXslTransform->Transform(pMyXPathDocument,NULL,pStWriter,0);

Console::WriteLine (pStWriter->ToString ); Console::ReadLine;

}   catch (Exception *e) {       Console::WriteLine (e->Message); }   return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample. To do this, follow these steps:
 * 1) Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article.

back to the top

<div class="references_section">