http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Overview

Downloads
Getting Started

FAQs

Sample Apps
Command Line
Usage Patterns

C++ API

Extensions

Release Notes

Bug reporting
Testing

Samples to help you get started
 

Each of the subdirectories in the Xalan-C++ samples directory contains the source files for a sample application. The executables for the samples are in the build subdirectory, which should be on the system path.

With most of the samples, you can use the following procedure:

  1. Go to the samples subdirectory containing the sample (use the DOS shell if you are running Windows)

  2. Run the sample from the command line (as indicated below)

  3. Examine the application source files. You may also want to modify the source files. Remember that if you modify a .cpp file, you must rebuild the executable and place it on the path before you can run the modified application.
Note Each sample application looks for input files in the current directory, the directory from which you run the application. The input files are in the samples subdirectory along with the sample source files. For the UNIX builds, application executables are in the bin subdirectory. For the Windows32 build, the application executable is in the build subdirectory (xml-xalan\c\Build\Win32\VC6\ Debug and Release). To run a sample, be sure the executable is on the path, and run it from the samples subdirectory that contains the input files.

XalanTransform
 

What it does: XalanTransform uses the XalanTransformer class and the associated C++ API to apply an XSL stylesheet file to an XML document file and write the transformation output to either an output file or to a stream. XalanTransform takes command-line arguments for the XML document to be transformed, the XSL stylesheet to apply, and an optional output file argument. If you omit the third argument, XalanTransform writes the transformation output to a stream that is sent to standard out (the screen).

You can run XalanTransform from the XalanTransform subdirectory with

XalanTransform foo.xml foo.xsl foo.out

Omit the third argument to write the transformation result to the screen. See also: Using the XalanTransformer class..


SimpleTransform
 

What it does: The SimpleTransform class uses the foo.xsl stylesheet to transform foo.xml, and writes the output to foo.out.

You can run it from the SimpleTransform subdirectory with

SimpleTransform

See also: Basic procedures for performing XSL transformations.


StreamTransform
 

What it does: The StreamTransform class processes character input streams containing a stylesheet and an XML document, and writes the transformation output to a character output stream. This sample illustrates the process for working with stylesheets and documents that you assemble in memory.

You can run it from the SimpleTransform subdirectory with

StreamTransform


UseStylesheetParam
 

What it does: Set a stylesheet parameter that the stylesheet uses during the transformation.

You can run it from the UseStylesheetParam subdirectory with

UseStylesheetParam key expression

where key is the parameter key (or name) and expression is a string expression enclosed in single quotes.

The example uses a stylesheet (foo.xsl) with a parameter named param1. The stylesheet accepts any string expression. Enclose the string expression in single quotes (so it is interpreted as an expression); if it includes more than a single word, enclose the resulting string in double quotes so the executable interprets it as a single argument. For example:

UseStylesheetParam param1 "'hello out there'"

See also: Setting stylesheet parameters.


TraceListen
 

What it does: Trace events during a transformation; the transformation uses birds.xsl to transform birds.xml and writes the output to birds.out.

You can run it from the TraceListen subdirectory with

TraceListen traceFlags

where traceFlags is one or more of the following:

  -TT (Trace the templates as they are being called)

  -TG (Trace each result tree generation event)

  -TS (Trace each selection event)

  -TTC (Trace the template children as they are being processed)

These flags are also available in the command-line utility (TestXSLT).

The core of this example is the following fragment:

// Set up a diagnostic writer to be used by the TraceListener...
XalanStdOutputStream  theStdErr(cerr);
XalanOutputStreamPrintWriter  diagnosticsWriter(theStdErr);

// Set up the TraceListener...
// traceTemplates, traceTemplateChildren, traceGenerationEvent,
// and TraceSelectionEvent are booleans set by the command line.
TraceListenerDefault theTraceListener(
        diagnosticsWriter,
        traceTemplates,
        traceTemplateChildren,
        traceGenerationEvent,
        traceSelectionEvent);

// Add the TraceListener to the XSLT processor...
theProcessor.setTraceSelects(traceSelectionEvent);
theProcessor.addTraceListener(&theTraceListener);

// Perform the transformation
....

CompileStylesheet
 

What it does: Use a compiled stylesheet to perform a series of transformations.

You can run it from the CompileStylesheet subdirectory with

CompileStylesheet

See also: Compiling stylesheets.


ThreadSafe (Windows32 Only)
 

What it does: multiple threads use a single compiled stylesheet (StylesheetRoot) and DOM source tree (XalanNode) to perform transformations concurrently. The application tracks the progress of the threads in messages to the screen, and each thread writes its own output file. Imagine a server application responding to multiple clients who happen to request the same transformation.

NoteThis sample uses Windows libraries, but could readily be adapted to run under Linux, AIX, or other environments. It could also be adapted to perform a variety of transformations, each with its own XML input.

You can run it from the ThreadSafe subdirectory with

ThreadSafe

See also: Compiling stylesheets.


XPathWrapper
 

This sample uses an executable (TestDriver in Windows, XPathWrapper in Linux and AIX), and two classes: XPathWrapper and XPathWrapperImp.

What it does: The executable passes XPathWrapper an XML file name, a context node location path, and an XPath expression. XPathWrapper in turn passes these arguments to the XPathWrapperImpl evaluate() method, which executes the XPath expression from specified context node in the XML document and returns the nodes it finds (if any).

NoteYou can use this sample as an aid when you want to find out what a given XPath expression returns from a given context node in an XML file.

Run this sample from the XPathWrapper subdirectory with

Windows:
  TestDriver XMLFile ContextNode XPathExpression

UNIX:
  XPathWrapper XMLFile ContextNode XPathExpression

where XMLFile is an XML source file, ContextNode is the location path to the context node, and XPathExpression is an XPath expression to apply to that context node. The XPathWrapper subdirectory contains an XML file named xml.foo (part of it appears below).

<?xml version="1.0"?>
<doc>
  <name first="David" last="Marston"/>
  <name first="David" last="Bertoni"/>
  ...
  <name first="Paul" last="Dick"/>
</doc>

You can try command lines like

Windows:
  TestDriver foo.xml /doc name/@last

UNIX:
  XPathWrapper foo.xml /doc name/@last

and

Windows:
  TestDriver foo.xml / '//name[position()="4"]/@first'

UNIX:
  XPathWrapper foo.xml / '//name[position()="4"]/@first'

NoteIf an XPathWrapper argument includes characters (such as *) that the shell intercepts, enclose the argument in double quotes.

See also: Working with XPath expressions.


ExternalFunctions
 

What it does: implement, install, and illustrate the usage of three extension functions. The functions return a square root, a cube, and a string with the current date and time. The sample stylesheet (foo.xsl) gets the area of a cube and units of measurement from an XML document (foo.xml), computes the length of each side of a cube and the volume of the cube, and enters the date and time of the transformation. The output appears in foo.out.

Run this sample from the ExternalFunctions subdirectory with

ExternalFunctions

See also: Extension Functions.


ApacheModuleXSLT
 

What it does: runs as an Apache module on an Apache Web server; performs transformations and returns the output to a Web browser. You configure Apache to respond to a given URL request for an output file (html or txt file in the configuration below) by applying an xsl stylesheet file to an xml document file (both with the specified name in a given location) and returning the transformation output to the client.

This sample also illustrates use of the XalanTransformer class and the C API defined in src/XalanTransformer/XalanCAPI.h. It returns transformation output in blocks to a callback function, which enables the browser to start displaying the result before the transformation has been completed.

In the Windows distribution, this ApacheModuleXSLT is named ApacheModuleXSLT.dll. In the Linux distribution, it is named mod_xslt.so. For the other UNIX distributions, assuming you have installed the Apache server on that platform, you can use the makefile to build the Apache module (mod_xslt with the appropriate library suffix).

NoteYou may need to adjust the Visual C++ or makefile settings to locate the required Apache header files. As shipped, the Visual C++ project file looks in \Apache Group\Apache\src\include, and the UNIX makefile looks in usr/lib.

To build (or rebuild) the Apache module, follow the instructions in Steps for doing a Windows build or Steps for doing a UNIX build. For UNIX platforms, you do the build with
make ApacheModuleXSLT.

Setting up and using ApacheModuleXSLT
 

To use ApacheModuleXSLT, do the following:

  1. (UNIX only) Be sure the Xalan and Xerces libraries are on your library path (you can accomplish this by copying them to /usr/lib; see Setting up the path/library path), and copy the Apache module to /usr/lib/apache.

  2. Add LoadModule and (UNIX only) AddModule entries to the Apache configuration file: httpd.conf.

    Windows: LoadModule mod_xslt xml-xalan\c\Build\Win32\VC6\Release\ApacheModuleXSLT.dll

    UNIX: AddModule mod_xslt.c
            and
            LoadModule mod_xslt /usr/lib/apache/mod_xslt.xx

    where xx is the appropriate library suffix for the UNIX platform ("so" or "a").

  3. Add a <Location> entry to httpd.conf that indicates where xml/xsl file pairs are to be found, and what target file extensions to recognize. We suggest the following:

    <Location /xslt>
      AddHandler .html
      AddHandler .txt
    </Location>

    This <Location> element instructs the module to respond to requests for xxx.html and xxx.txt files in the in the xslt subdirectory (under the document root; see next item) by applying the xxx.xsl stylesheet to xxx.xml (both in that directory) and returning the transformation result to the browser.

    For example, a request for foo.html instructs the module to apply foo.xsl to foo.xml and return the result.

    Note: It is up to the stylesheet to apply the appropriate xsl:output method to the output. Whether the user specifies html or txt is, of itself, immaterial.

  4. Put xml/xsl file pairs in the <Location> subdirectory (xslt in the example)) under the document root directory specified in httpd.conf by the DocumentRoot and <Directory> settings. Alternatively, you can modify these settings to point to xml-xalan/c/samples/ApacheModuleXSLT, which includes an xslt subdirectory with xml/xsl file pairs (foo.xml/xsl, apachemod.xml/xsl).

  5. Start the Apache server.

  6. From a Web browser, call the module with a URL as follows:
    http://serverName/xslt/xxx.html
    where serverName is the Apache server (such as www.myServer.com) and xxx is the name of an xml/xsl pair of files (such as foo.xml and foo.xsl) in the xslt subdirectory under the DocumentRoot directory.

    For example,
    http://www.myServer.com/xslt/apachemod.html
    instructs ApacheModuleXSLT to apply the apachemod.xsl stylesheet to the apachemod.xml XML document (both files in the xslt directory under the Apache DocumentRoot directory) and return the transformation result to the browser.



Copyright © 2000 The Apache Software Foundation. All Rights Reserved.