Trademark Logo Xalan-C/C++ Version 1.11
Xalan-C++ Basic usage patterns
Apache Foundation Xalan Project Xerces Project Web Consortium Oasis Open

Xalan-C++ Basic usage patterns

(top)

Introduction

To perform a transformation, use one of the XalanTransformer transform() methods. The transformation requires an XML source document and an XSL stylesheet. Both of these objects may be represented by instances of XSLTInputSource . You can construct an XSLTInputSource with a string (the system ID for a file or URI), an input stream, or a DOM.

If you are using an XSL stylesheet to perform a series of transformations, you can improve performance by calling transform() with a compiled stylesheet, an instance of XalanCompiledStylesheet. If you are transforming an XML source more than once, you should call transform() with a parsed XML source, an instance of XalanParsedSource. See Performing a series of transformations.

If you XML source document contains a stylesheet Processing Instruction (PI), you do not need to include a stylesheet object when you call transform().

The transformation output is represented by an XSLTResultTarget, which you can set up to refer to an output stream, the system ID for a file or URI, or a Formatter for one of the various styles of DOM output.

For detailed API documentation, see Xalan-C++ API. For an overview of the command-line utility, see Command-Line Utility.

(top)

Basic usage patten with the XalanTransformer C++ API

Using XalanTransformer and the C++ API, you can perform one or more transformations as described in the following steps.

note For a working sample that illustrates these steps, see the XalanTransform sample.

(top)

1. Include the required header files.

Always start with xalanc/Include/PlatformDefinitions.hpp, the Xalan-C++ base header file. Also include xercesc/util/PlatformUtils.hpp, xalanc/XalanTransformer/XalanTransformer.hpp, and any other header files your particular application requires.

#include <xalanc/Include/PlatformDefinitions.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>
...

(top)

2. Define namespaces

As of version 1.5, Xalan-C++ now uses C++ namespaces for those platforms which support them. A number of macros are provided to make using the Xalan-C++ (and Xerces-C++) namespaces easier.

In the following example, the XALAN_USING_XERCES and XALAN_USING_XALAN macros are used to declare that the program is using XMLPlatformUtils and XalanTransformer from the Xerces-C++ and Xalan-C++ namespaces respectively.

XALAN_USING_XERCES(XMLPlatformUtils)
XALAN_USING_XALAN(XalanTransformer)

These macros can be used immediately after the included header files (for global applicability in a given source file) or within functions and methods for local applicability.

note Whilst you can use the standard C++ namespace syntax directly, the Xerces-C++ and Xalan-C++ namespaces are linked to the version number. For example, the Xalan namespace is currently xalanc_1_11. The macros will automatically take care of this when code is re-compiled against a new version of the libraries. Using the namespaces directly will require each namespace related statement be updated by hand.

(top)

3. Initialize Xerces and Xalan.

Use the static initializers to initialize the Xalan-C++ and Xerces-C++ platforms. You must initialize Xerces-C++ once per process. You may initialize and terminate Xalan-C++ multiple times, but this is not recommended: it is inefficient and is not thread safe.

XMLPlatformUtils::Initialize();
XalanTransformer::initialize();

(top)

4. Create a XalanTransformer.

XalanTransformer theXalanTransformer;

(top)

5. Perform each transformation.

You can explicitly instantiate XSLTInputSource objects for the XML source document and XSL stylesheet, and an XSLTResultTarget object for the output, and then call XalanTransformer transform() with those objects as parameters. For example:

XSLTInputSource xmlIn("foo.xml");
XSLTInputSource xslIn("foo.xsl");
XSLTResultTarget xmlOut("foo-out.xml");
int theResult =
    theXalanTransformer.transform(xmlIn,xslIn,xmlOut)

Alternatively, you can call transform() with the strings (system identifiers), streams, and/or DOMs that the compiler needs to implicitly construct the XSLTInputSource and XSLTResultTarget objects. For example:

const char* xmlIn = "foo.xml";
const char* xslIn = "foo.xsl";
const char* xmlOut = "foo-out.xml";
int theResult =
    theXalanTransformer.transform(xmlIn,xslIn,xmlOut)

Keep in mind that XSLTInputSource and XSLTResultTarget provide a variety of single-argument constructors that you can use in this manner:

note Each transform() method returns an integer code, 0 for success. If an error occurs, you can use the getLastError() method to return a pointer to the error message.

(top)

6. Shut down Xalan.

When you shut down Xalan, you may also want to shut down Xerces and ICU support (if enabled). Keep the following considerations in mind:

Use the static terminators.

XalanTransformer::terminate();
XMLPlatformUtils::Terminate();
XalanTransformer::ICUCleanUp();

(top)

Using a stylesheet processing instruction

If you want to use the stylesheet referred to by a stylesheet processing instruction in the XML document, simply call transform() without the second XSLTInputSource argument. For example:

// foo.xml contains a stylesheet PI
const char* xmlIn = "foo.xml";
const char* xmlOut = "foo-out.xml";
int theResult =
    theXalanTransformer.transform(xmlIn,xmlOut)

(top)

Setting Stylesheet Parameters

An XSL stylesheet can include parameters that are set at run time before a transformation takes place. Traditionally, a top-level parameter value is of text string type. The Xalan library now supports three types of top-level parameters that can be set. One is a text string parameter. Another is a number parameter of floating point type double. The third is a nodeset (XalanNode *) parameter, usually implemented as a parsed document.

Any XObject that is created outside of the transformation can be associated with a top-level parameter. The XalanTransformer has an XObject factory whereby top-level parameters can be owned by the XalanTransformer object.

To set a stylesheet parameter, use the XalanTransformer setStylesheetParam() method. The setStylesheetParam() method takes two arguments: the parameter name and the value. The value can be a string type, a number double type, an (XalanNode *) pointer to a nodeset or parsed document, or any XObjectPtr returned from an XObject factory.

Top level parameters are sticky. Once set to an instance of an XalanTransformer object, they can be used for multiple transformations. The XalanTransformer reset() method prepares a transformer for a new transformation. Use the clearStylesheetParams() method to release the top-level stylesheet parameters.

The Xalan command line utility currently supports only a text string value for a top-level stylesheet parameter. The single quotes are required to identify a text string value.

Xalan -p param1 "'string expression'"foo.xml foo.xsl

If the string expression includes spaces or other characters that the shell intercepts, first enclose the string in single quotes so Xalan-C++ interprets it as a string expression, and then enclose the resulting string in double quotes so the shell interprets it as a single argument.

The UseStylesheetParam sample application supports all three types of top-level stylesheet parameters.

The 'C' language interface XalanCAPI also supports the three types of top-level parameters. The sample program TestCAPIparms.c shows how to use top-level parameters with 'C' language programs.

note The Xalan command line utility should be revised to accommodate the number and nodeset types of top-level stylesheet parameters. Only text string values are currently supported.

Top-level stylesheet parameters of nodeset type (XalanNode *) are useful for the merging of multiple XML documents.

(top)

Processing output incrementally

XalanTransformer provides a transform() method that sends the output in blocks to a callback function, which enables you to begin processing the output while the transformation is still in process:

int
transform(const XSLTInputSource& xmlIn,
          const XSLTInputSource& xslIn,
          void* theOutputHandle,
          XalanOutputHandlerType theOutputHandler,
          XalanFlushHanderType theFlushHandler = 0);

For an example, see XalanTransformerCallback.

(top)

Performing a series of transformations

Before Xalan performs a standard transformation, it must parse the XML document and compile the XSL stylesheet into binary representations. If you plan to use the same XML document or stylesheet in a series of transformations, you can improve performance by parsing the XML document or compiling the stylesheet once and using the binary representation when you call transform().

XalanTransformer includes methods for creating compiled stylesheets and parsed XML documents: the compileStylesheet() method returns a pointer to a XalanCompiledStylesheet; the parseSource() method returns a pointer to a XalanParsedSource.

note In the case of failure, both methods return 0.

Example using a XalanCompiledStylesheet to perform multiple transformations:

XalanCompiledStylesheet* compiledStylesheet = 0;
compiledStylesheet = theXalanTransformer.compileStylesheet("foo.xsl");
assert(compiledStylesheet!=0);
theXalanTransformer.transform("foo1.xml", *compiledStylesheet, "foo1.out.");
theXalanTransformer.transform("foo2.xml", *compiledStylesheet, "foo2.out");
...

For a working sample, see the CompileStylesheet sample.

Example using a XalanParsedSource for multiple transformations:

XalanParsedSource* parsedXML = 0;
parsedXML = theXalanTransformer.parseSource("foo.xml");
assert(parsedXML!=0);
theXalanTransformer.transform(*parsedXML, "foo1.xsl", "foo-xsl1.out");
theXalanTransformer.transform(*parsedXML, "foo2.xsl", "foo-xsl2.out");
...

For a sample that uses both a parsed XML source and a compiled stylesheet, see ThreadSafe .

(top)

Working with DOM input and output

You can set up an XSLTResultTarget to produce a DOM when you perform a transformation. You can also use a DOM as input for a transformation.

The following code fragment illustrates the procedures for working with DOM output :


// Use the Xerces DOM parser to create a DOMDocument.

#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMImplementation.hpp>

#include <xalanc/XMLSupport/FormatterToXML.hpp>
#include <xalanc/XMLSupport/XalanTransformer.hpp>

XALAN_USING_XERCES(DOMDocument)
XALAN_USING_XERCES(DOMImplementation)

XALAN_USING_XALAN(FormatterToXML)
XALAN_USING_XALAN(XalanTransformer)


    // If you want to produce DOM output, create an empty Xerces Document
    // to contain the transformation output.
    
    DOMDocument * theDOM = 
        DOMImplementation::getImplementation()->createDocument();    

    // Now create a FormatterListener which can be used by the transformer
    // to send each output node to the new Xerces document

    FormatterToXercesDOM theFormatter(theDOM);

    // Now do the transform as normal
    XalanTransformer theXalanTransformer
    int theResult = theXalanTransformer.transform(
        "foo.xml", 
        "foo.xsl", 
        theFormatter);
...

    // After you perform the transformation, the DOMDocument contains
    // the output.
note You can also follow the same process but use a FormatterToDeprecatedXercesDOM if you require a DOM_Document output. However this is discouraged, as support for the deprecated DOM may be removed in future releases of Xalan-C++

If you want to use a Xerces DOM object as input for a transformation without wrapping the DOM in a XercesParserLiaison, see passing in a Xerces DOM.

(top)

Limitations

Performance is much better when Xalan-C++ uses native source tree handling rather than interacting with the Xerces DOMParser.

If you are using the deprecated DOM, the Xerces DOMParser by default, creates a DOM_XMLDecNode in the DOM tree to represent the XML declaration. The Xalan bridge for the Xerces DOM does not support this non-standard node type. Accordingly, you must call DOMParser::setToCreateXMLDeclTypeNode(false) before you parse the XML file. If not, the behavior is undefined, and your application may crash.

(top)

Passing in a Xerces DOM to a transformation

You may want to use a Xerces DOM that was created without using the XalanTransformer class. As the following code snippet illustrates, you can use XercesDOMWrapperParsedSource to pass in a Xerces DOM as the source for an XSL transformation.

#include <xercesc/parsers/DOMParser.hpp>
#include <xalanc/XalanTransformer/XercesDOMWrapperParsedSource.hpp>

void parseWithXerces(XalanTransformer &xalan, 
                     const XSLTInputSource &xmlInput,
                     const XalanCompiledStylesheet* styleSheet,
                     const XSLTResultTarget &output,
                     XMLFileReporter &logFile)
{
    XercesDOMParser theParser;
  
    // Turn on validation and namespace support.
    theParser.setDoValidation(true);
    theParser.setDoNamespaces(true);

    // Parse the document
  
    theParser.parse(xmlInput);
    DOMDocument *theDOM = theParser.getDocument();
    theDOM->normalize();

    XercesDOMSupport theDOMSupport;
    XercesParserLiaison theParserLiaison;
  
    // Use the DOM to create a XercesDOMWrapperParsedSource, 
    // which you can pass to the transform method.
    try
    {
        const XercesDOMWrapperParsedSource parsedSource(
                                   theDOM,
                                   theParserLiaison,
                                   theDOMSupport,
                                   XalanDOMString(xmlInput.getSystemId()));

        xalan.transform(parsedSource, stylesheet, output);

    }
    catch (....)
    {
      ...
    }
}  

(top)

Working with XPath expressions

XSL stylesheets use XPath expressions to select nodes, specify conditions, and generate text for the result tree. XPath provides an API that you can call directly. For example, you may want to select nodes programmatically and do your own processing without a stylesheet. Xalan-C++ provides an XPathEvaluator interface to simplify the process of executing XPath expressions.

For an example that executes XPath expressions against XML source files, see SimpleXPathAPI (takes advantage of the XPathEvaluator interface) and XPathWrapper.

(top)

Using TraceListener

TraceListener is a debugging abstract base class implemented by TraceListenerDefault. You can use TraceListener to trace any combination of the following:

To construct a TraceListener with TraceListenerDefault, you need a PrintWriter and a boolean for each of these four tracing options. You can then use the XSLTEngimeImpl setTraceSelects and addTraceListener methods to add the TraceListener to an XSLTProcessor. See the TraceListen sample application.

The TraceListen uses TraceListenerDefault to write events to the screen.

(top)

Using the ICU

You can use the International Components for Unicode (ICU) to extend support for encoding, number formatting, and sorting.

If you choose to build Xalan with ICU, you will need to rebuild Xerces with ICU as well.

To get the ICU:

  1. Download and unzip the International Components for Unicode(ICU) 3.2 source files from the IBM developerWorks open source zone.

  2. Do an ICU build -- see the Windows NT or Unix build instructions in the build_instruct.html that accompanies the download.
    Important For Windows, define the environment variable ICUROOT and then restart Visual C++ or Visual Studio .NET. in order for the ICUROOT variable to take effect.

(top)

Enabling ICU support for encoding

If you want to enable ICU support for encoding, you must integrate ICU with your Xerces-C++ build. Xerces-C++ uses ICU for input encoding. See Building ICU for Xerces-C++.

Once you have integrated the ICU with Xerces-C++, Xalan-C++ automatically uses ICU support for output encoding (the xsl:output encoding attribute).

(top)

Enabling ICU support for number formatting and sorting

If you only want to use the ICU to support number formatting and sorting, you do not need to integrate the ICU with Xalan-C++, but you must do the following in the application where you want to enable ICU support:

  1. Define the environment variable ICUROOT.

  2. Substitute ICU support for format-number(), xsl:number, and/or xsl:sort.

  3. Rebuild the Xalan library to include the ICUBridge.

ICUBridge

All Xalan-C++ references to ICU are centralized in the ICUBridge module, which supplies the infrastructure for enabling ICU support for number formatting and sorting.

#include <xalanc/ICUBridge/ICUBridge.hpp>
#include <xalanc/ICUBridge/FunctionICUFormatNumber.hpp>
#include <xalanc/ICUBridge/ICUXalanNumberFormatFactory.hpp>
#include <xalanc/ICUBridge/ICUBridgeCollationCompareFunctor.hpp>

For Windows, do a clean build of the Xalan library using the "XalanICU.dsw" workspace (for Visual C++ users) or "XalanICU.sln" solution (for Visual Studio .NET users).

For UNIX:

  1. Define the XALAN_USE_ICU environment variable.

  2. Set the XALANROOT environment variable to the path to to the ICU root (unless you have copied the ICU library to /usr/lib).

  3. Rebuild the Xalan library (libxalan-c.so.111 for Linux, libxalan-c111.so for AIX, libxalan-c.sl.111.0 for HP-UX 11, and libxalan-c1_11.so for Solaris).

  4. Be sure the Xalan library is on the library path (LD_LIBRARY_PATH for Red Hat Linux, LIBPATH for AIX, SHLIB_PATH for HP-UX 11, LD_LIBRARY_PATH for Solaris).
note The command you use for setting environment variables depends on the shell you are using.
For Bourne Shell, K Shell, or Bash use export ENVAR="val"
For C Shell, use setenv ENVAR "val"
where ENVAR is the environment variable name and val is the environment variable setting. You can check the setting of an environment variable with echo $ENVAR
To define XALAN_USE_ICU, set its value to "1".

Number formatting

To enable ICU support for the XSLT format-number() function, do the following:

// Install ICU support for the format-number() function.
FunctionICUFormatNumber::FunctionICUFormatNumberInstaller theInstaller;

Sorting

To enable ICU support for xsl:sort, do the following:

// Set up a StylesheetExecutionContextDefaultobject
// (named theExecutionContext in the following fragment),
// and install the ICUCollationCompareFunctor.
ICUBridgeCollationCompareFunctortheICUFunctor;
theExecutionContext.installCollationCompareFunctor(&theICUFunctor);

(top)

Basic XalanTransformer usage pattern with the C API

We also include a simple C interface for using the XalanTransformer class. See XalanTransformer/XalanCAPI.h. The ApacheModuleXSLT sample illustrates the use of this C API.

Basic strategy:

(top)

1. Include the XalanTransformer C API header

#include <XalanTransformer/XalanCAPI.h>

(top)

2. Initialize Xalan and Xerces

XalanInitialize();

(top)

3. Create a Xalan transformer

XalanHandle xalan = NULL;
xalan = CreateXalanTransformer();

(top)

4. Perform each transformation

For example:

const char * xmlfilename = "foo.xml";
const char* xslfilename = "foo.xsl";
const char* outfilename = "foo.out";
int theResult = 0;
theResult = XalanTransformToFile(xmlfilename,
                                 xslfilename,
                                 outfilename,
                                 xalan);
note If the XML input file contains a stylesheet processing instruction that you want to use, use an empty XSLTInputSource for the stylesheet argument.

(top)

5. Shut down Xalan

XalanTerminate();

(top)

Variations with the XalanTransformer C API

The Xalan C API supports approximately the same set of options as the C++ API. In particular, you can

For a sample that sends output in blocks to a callback function, see ApacheModuleXSLT.

(top)