How to apply an XSL transformation to XML for streaming by using Visual C++ .NET or Visual C++ 2005 (815655)
The information in this article applies to:
- Microsoft Visual C++ .NET (2002)
- Microsoft Visual C++ .NET (2003)
- 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 TASKSUMMARYThis 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 topRequirementsThe 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 topHow to Apply XSL TransformationsThis 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:
#using <System.XML.Dll>
- 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"books.xml");
- Populate an XPathDocument object with the sample file,
Books.xml.
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("books.xml"); - 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(); - 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"Books.xsl"); - 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"ISBNBooks.xml",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 ();
- 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. - Build and then run your project. The results of the
transformation appear in the console window:
<?xml version="1.0" encoding="utf-16"?><root><bookstore><book ISBN="1-861003-11-0"><price>8.99</price>
</book><book ISBN="0-201-63361-2"><price>11.99</price>
</book><book ISBN="1-861001-57-6"><price>9.99</price>
</book></bookstore></root>
back to the topComplete Code Sample
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
#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"books.xml");
Xml::Xsl::XslTransform *pMyXslTransform = new Xml::Xsl::XslTransform();
pMyXslTransform->Load(S"Books.xsl");
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:
- Click Project, and then click ProjectName Properties.
Note ProjectName represents 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 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: These steps apply to the whole article. back to the top
Modification Type: | Major | Last Reviewed: | 4/26/2006 |
---|
Keywords: | kbIO kbSample kbHOWTOmaster kbhowto KB815655 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|