How to apply an XSL transformation from one XML document to another by using Visual C++ .NET or Visual C++ 2005 (815653)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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:
IN THIS TASKSUMMARYThis 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
topRequirements
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
topSteps to Build the SampleThis 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:
#using <System.XMl.Dll> - 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"books.xsl");
- Call the Transform method to initiate the transformation, passing in the source XML
document and the transformed XML document name.
myXslTransform->Transform(S"books.xml", S"ISBNBookList.xml");
- Build and run the project. You may see the ISBNBookList.xml
file in your project file folder.
back to the
topComplete Code Sample
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.XMl.Dll>
#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"books.xsl");
myXslTransform->Transform(S"books.xml", S"ISBNBookList.xml");
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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- 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: 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"books.xml", S"ISBNBookList.xml"); WithmyXslTransform->Transform(S"books.xml", S"ISBNBookList.xml", 0); back to the topREFERENCESFor more information about the XslTransform class, see the following Microsoft .NET Framework Class Library
documentation: For more information about the XslTransform class with the XslTransform object, see the following Microsoft .NET Framework Developer's
Guide documentation: For a practical comparison of XSLT and ASP .NET, see the following
MSDN Online Voices Extreme XML column: 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: For more general information about Visual C++ .NET or XML in .NET,
see the following Usenet newsgroups: back to the
top
Modification Type: | Major | Last Reviewed: | 4/26/2006 |
---|
Keywords: | kbHOWTOmaster kbXML kbhowto KB815653 kbAudDeveloper |
---|
|