





















|
| |
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 DOM.
For detailed API documentation, see Xalan-C++ API. For an overview of the
command-line utility, see Command-Line Utility.
|
 |  |  |  | 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.
 | For a working sample that illustrates these steps, see the XalanTransform sample. |
 |  |  |  | 3.Create a XalanTransformer |  |  |  |  |
| |
 |  |  |  | XalanTransformer theXalanTransformer; |  |  |  |  |
|
 |  |  |  | 4. 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:
XSLTInputSource(const char* systemID);
XSLTInputSource(const XMLCh* systemID);//Unicode chars
XSLTInputSource(istream* stream);
XSLTInputSource(XalanNode* node);
XSLTResultTarget(char* fileName);
XSLTResultTarget(XalanDOMString& fileName);
XSLTResultTarget(ostream* stream);
XSLTResultTarget(ostream& stream);
XSLTResultTarget(Writer* characterStream);
XSLTResultTarget(XalanDocument* document);
XSLTResultTarget(XalanDocumentFragment* documentFragment);
XSLTResultTarget(XalanElement* element);
 | 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. |
|
|
 |  |  |  | Setting stylesheet parameters |  |  |  |  |
| |
An XSL stylesheet can include parameters that are set at run time before a transformation takes place. When we generate the HTML documents that make up the Xalan doc set, for example, we send the stylesheet an id parameter along with each XML source document. The id identifies that document and enables the stylesheet to integrate it into the overall doc set.
To set a stylesheet parameter, use the XalanTransformer setStylesheetParam() method. The setStytlesheetParam() method takes two arguments: the parameter name and the expression. For example:
 |  |  |  | const XalanDOMString& key="param1";
const XalanDOMString& expression="'Hello World'";
theXalanTransformer.setStylesheetParam(key, expression);
// foo.xsl defines a stylesheet parameter named param1.
theXalanTransformer.transform("foo.xml","foo.xsl","foo-out.xml") |  |  |  |  |
 | If the expression is a string, enclose it in single quotes to make it a string expression. |
You can include the -param flag with two arguments when you call the command line utility. The first argument is the parameter name or key, and the second argument is the string expression (in single quotes). For example:
TestXSLT -in foo.xml -xsl foo.xsl -param param1 'boo'
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. For example:
TestXSLT -in foo.xml -xsl foo.xsl -param param1 "'hello there'"
The UseStylesheetParam sample application also uses a command-line parameter.
|
 |  |  |  | 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.
 | 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.
|
| |
TraceListener is a debugging abstract base class implemented by TraceListenerDefault. You can use TraceListener to trace any combination of the following:
- Calls to templates
- Calls to template children
- Selection events
- Result tree generation events
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 command-line utility (testXSLT) and TraceListen both use TraceListenerDefault to write events to the screen.
|
| |
You can use the International Components for Unicode (ICU) to extend support for encoding, number
formatting, and sorting.
- Encoding
Xerces-C++ and Xalan-C++ use UTF-16 encoding to work with Unicode data.
If you integrate the ICU with Xerces-C++, both Xerces-C++ and Xalan-C++ use ICU support for
UTF-16 encoding.
- format-number()
This XSLT function includes two or three arguments (the third is
optional): number, format pattern, and decimal-format name. Xalan-C++ ignores the format
pattern and optional decimal-format name. If you install ICU support for format-number(),
this function is fully supported with all its arguments.
- xsl:sort
If you install ICU support for xml:sort, Xalan-C++ implements Unicode-style collation.
To get the ICU:
- Download and unzip the International Components for Unicode (ICU) 1.8.1 source files from the IBM
developerWorks open source zone.
- Do an ICU build -- see the Windows NT or Unix build instructions in the readme.html that
accompanies the download.
Important For Windows, be sure to install the ICU on the same drive and at
the same level as Xalan-C++ and Xerces-C++.
- Set the ICU_DATA environment variable as indicated in the readme.html.
 |  |  |  | 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:
- Include the ICUBridge headers.
- Substitute ICU support for format-number(), xsl:number, and/or xsl:sort.
- Windows: Provide your application access to the ICUBridge library.
Linux, AIX, or HP-UX 11: 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 <ICUBridge/ICUBridge.hpp>
#include <ICUBridge/FunctionICUFormatNumber.hpp>
#include <ICUBridge/ICUXalanNumberFormatFactory.hpp>
#include <ICUBridge/ICUBridgeCollationCompareFunctor.hpp> |  |  |  |  |
For Windows be sure ICUBridge.dll,the ICUBridge library, is on the path.
For Linux, rebuild libxalan-c1_2.so with XALAN_USE_ICU defined, and place it on the shared library path
(LD_LIBRARY_PATH for Red Hat Linux 6.1) or copy it to /usr/lib.
For AIX, rebuild libxalan-c1_2.a with XALAN_USE_ICU defined, and place it on the load library
path (LIBPATH) or copy it to /usr/lib.
For HP-UX 11, rebuild libxalan-c1_2.sl with XALAN_USE_ICU defined, and place it on the shared library
path (SHLIB_PATH) or copy it to /usr/lib.
For Solaris, rebuild libxalan-c1_2.so with XALAN_USE_ICU defined, and place it on the shared library
path (LD_LIBRARY_PATH) or copy it to /usr/lib.
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 StylesheetExecutionContextDefault object
// (named theExecutionContext in the following fragment),
// and install the ICUCollationCompareFunctor.
ICUBridgeCollationCompareFunctor theICUFunctor;
theExecutionContext.installCollationCompareFunctor(&theICUFunctor); |  |  |  |  |
|
|
|
|