XSLT Transformations from Java and C++ with the DOM

Michael C. Rawlins, author of Using XML with Legacy Business Applications (Addison-Wesley Professional, 2003), presents the basic techniques for using XSLT with the DOM from C++ with MSXML, and Java with JAXP and Xalan.

From the author of

From the author of

Introduction

In my book and in the two previous articles in this series I have frequently
sung the praises of the World Wide Web Consortium's Document Object Model
(DOM). Although several APIs have emerged as attractive alternatives to the DOM
since it was first developed, it remains one of the most powerful and flexible
means of processing XML. This article concludes the series by presenting the
basics for using the DOM with another XML-related technology that's very
useful to application developers: Extensible Stylesheet Language for
Transformations (XSLT). We'll focus on how to perform XSLT transformations
on DOM documents. This capability is very important in environments in which an
application may be required to produce or consume XML documents that don't
conform to the formats that it normally uses. For example, an order-entry
application may be coded to accept XML purchase orders using a proprietary
format, but customers may send orders formatted according to national or
industry standards. Transforming input documents as a preprocessing step or
output documents as a post-processing step, using XSLT in either case, can ease
life in such heterogeneous environments.

As powerful as the DOM is, it's still being enhanced. One of the tasks
still not addressed by even Level 3 of the DOM Recommendation is performing XSLT
transformations on all or part of a DOM document. Fortunately, many major DOM
implementations offer extensions that provide this functionality. Unfortunately,
because these extensions are outside of the DOM recommendation, their
implementations vary. We'll look at two implementations:

C++ with Microsoft's XML Core Services (MSXML)

Java with Java Architecture for XML Processing (JAXP) and with the Apache
Foundation's Xalan XSLT processor

For this article we're concerned with how to perform XSLT
transformations on DOM documents, and not with how to code the stylesheets used
in XSLT transformations. However, we need an example, so we'll use a very
basic "Hello World" transformation for both the C++ and Java programs.
Listing 1 shows the source document.

The stylesheet contains one template element that matches on the document
root. It's a literal result template, containing the entire body of the
result document. If you're familiar with XSLT, you'll note that
any source document will produce the desired result when transformed with
this stylesheet.

Although the C++ and Java implementations differ in the details and
interfaces, the basic approach is the same in both:

Load the source document into a DOM document.

Load the stylesheet into a DOM document.

Create an empty DOM document for the result.

Perform the transformation, producing the result document.

Save the result document.

XSLT processing in both implementations is richer and more capable than that
presented in this article. For example, either source or target can be objects
other than DOM documents. But because many situations deal with a DOM-to-DOM
transformation we'll focus on that task.