Transforming XML Data with the TransformXML Method

So far you've learned how to use stylesheets to transform XML data files to HTML format to create a web page. While rendering XML files into HTML for display in a web browser is the most popular use of stylesheets, XML data files can also be transformed into other XML files using the XSLT transforms.

In this section we will learn how to use the TransformXML method to apply an Extensible Stylesheet (XSL) to an XML data file and write the resulting XML to another XML data file.

The TransformXML method takes a number of arguments that are presented in Table 29-3. In its simplest form, the TransformXML method looks like this:

Application.TransformXML DataSource:="C:\Learn_XML\InternalContacts.xml", TransformSource:="C:\Learn_XML\Extensions.xsl", _ OutputTarget:="C:\Learn_XML\EmpExtensions.xml"

The above statement can be used inside a VBA procedure stub to programmat-ically apply the specified stylesheet.

Table 29-3: Arguments of the TransformXML method (in order of appearance)

Argument Type

Data Type


DataSource (required)


Specifies the full path of the XML data file that will be transformed.

TransformSource (required)


Specifies the full path of the XSL stylesheet to apply to the XML data file specified in the DataSource argument.




Specifies the full path of the resulting XML data file after applying the XSL stylesheet.

WellFormedXMLOutput (optional)


Set this argument to True to create a well-formed XML document. Set this argument to False to encode the resulting XML file in UTF-16 format. The default is False.

ScriptOption (optional)

AcTransformXMLScriptOption Use one of the following constants:

Constant Value acDisableScript 2 acEnableScript 0 acPromptScript 1

acPromptScript is the default.

Use this argument to specify the action that should be taken if the XSL file contains scripting code.

Custom Project 29-3 demonstrates how to transform an XML data file into another XML file. We will start by creating a custom stylesheet named Exten-sions.xsl that will transform the InternalContacts.xml file (generated from the

Part V

Northwind database Employees table) into an XML file named EmpExtensions.xml. Next, we will write a VBA procedure that exports the XML source file and performs the transformation. Finally, we will import the resulting XML data file into Access.

0 0

Post a comment