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

Article translations Article translations
Article ID: 815653 - View products that this article applies to.
Expand all | Collapse all

On This Page

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

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.

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

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
  1. In Visual Studio .NET, create a new Managed C++ Application project.

    Note In Visual Studio 2005, create a new CLR Console Application.
  2. Add the following code to add a reference to the System.Xml namespace:
    #using <System.XMl.Dll>
  3. 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;
    
  4. Declare the appropriate variables, and then declare an XslTransform object to transform XML documents.
    XslTransform* myXslTransform;
    
  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 ISBN list of books.
    myXslTransform->Load(S"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(S"books.xml", S"ISBNBookList.xml");
    
  8. Build and run the project. You may see the ISBNBookList.xml file in your project file folder.

Complete 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:
  1. Click Project, and then click <ProjectName> Properties.

    Note <ProjectName> is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. 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"books.xml", S"ISBNBookList.xml");

With
myXslTransform->Transform(S"books.xml", S"ISBNBookList.xml", 0);

REFERENCES

For 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:

Properties

Article ID: 815653 - Last Review: November 14, 2007 - Revision: 6.3
APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbxml kbhowto KB815653

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com