Microsoft KB Archive/815653

= How to apply an XSL transformation from one XML document to another by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815653

Article Last Modified on 11/14/2007

-

APPLIES TO


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

-





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



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

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

'''

IN THIS TASK'''
 * SUMMARY
 * Requirements
 * Steps to Build the Sample
 * Complete Code Sample
 * REFERENCES



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

back to the top



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

back to the top

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 you can use the sample files that are included with the .NET Software Development Kit (SDK) QuickStarts. Copy the Books.xml and Books.xsl files to the folder where you create this project. You can find these files in the following folder:

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

 In Visual Studio .NET, create a new Managed C++ Application project.

Note In Visual Studio 2005, create a new CLR Console Application.  Add the following code to add a reference to the System.Xml namespace:  Specify the using statement on the Xml and the Xsl namespaces so that you do not have to qualify declarations in those namespaces later in your code. Use the using statement before any other declarations. using namespace System::Xml; using namespace System::Xml::Xsl;   Declare the appropriate variables, and then declare an XslTransform object to transform XML documents. XslTransform* myXslTransform;   Construct a new XslTransform object. The XslTransform class is an XSLT processor that implements the 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 file into a simple ISBN list of books. myXslTransform->Load(S&quot;books.xsl&quot;); </li>  Call the Transform method to initiate the transformation, passing in the source XML document and the transformed XML document name. myXslTransform->Transform(S&quot;books.xml&quot;, S&quot;ISBNBookList.xml&quot;); </li> Build and run the project. You may see the ISBNBookList.xml file in your project file folder.</li></ol>
 * 1) using <System.XMl.Dll> </li>

back to the top

Complete Code Sample
// This is the main project file for VC++ application project // generated using an Application Wizard.


 * 1) include &quot;stdafx.h&quot;


 * 1) using <mscorlib.dll>
 * 2) using <System.XMl.Dll>
 * 3) include <tchar.h>

using namespace System; using namespace System::Xml; using namespace System::Xml::Xsl;

// This is the entry point for this application int _tmain(void) {   XslTransform* myXslTransform; myXslTransform = new XslTransform; myXslTransform->Load(S&quot;books.xsl&quot;); myXslTransform->Transform(S&quot;books.xml&quot;, S&quot;ISBNBookList.xml&quot;);

return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for 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 in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, 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.

NOTE: While compiling above code in Visual C++ .NET 2003, you may receive C4996 compiler warning. This is due to 'System::Xml::Xsl::XslTransform::Transform' is declared deprecated.

Use following code so that you may not get C4996 compiler warning.

Replace: myXslTransform->Transform(S&quot;books.xml&quot;, S&quot;ISBNBookList.xml&quot;);

With myXslTransform->Transform(S&quot;books.xml&quot;, S&quot;ISBNBookList.xml&quot;, 0); back to the top

<div class="references_section">