Performing basic XSLT operations

You can use the XSLTExecutable instances that are created using XFactory.prepareXSLT methods to perform XSLT transformations.

About this task

XSLT stylesheets can be passed to the XFactory.prepareXSLT method using a JAXP Source object. The resulting XSLTExecutable instance is thread safe and can be reused to transform multiple input documents.

Procedure

Example

The following is a basic example of preparing and executing an interpreted transformation.
// Create the factory
XFactory factory = XFactory.newInstance();

// Create a StreamSource for the stylesheet
StreamSource stylesheet = new StreamSource("simple.xsl");

// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet);

// Create the input source
Source input = new StreamSource("simple.xml");

// Create the result
Result result = new StreamResult(System.out);

// Execute the transformation
executable.execute(input, result);
The following is a basic example of preparing and executing a compiled transformation.
// Create the factory
XFactory factory = XFactory.newInstance();

// Create a StreamSource for the stylesheet
StreamSource stylesheet = new StreamSource("simple.xsl");

// Create a new static context
XStaticContext staticContext = factory.newStaticContext();

// Enable the compiler
staticContext.setUseCompiler(true);

// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet, staticContext);

// Create the input source
Source input = new StreamSource("simple.xml");

// Create the result
Result result = new StreamResult(System.out);

// Execute the transformation
executable.execute(input, result);
The following is a basic example of creating an identity transformation.
// Create the factory
XFactory factory = XFactory.newInstance();
           
// Create the item factory
XItemFactory itemFactory = factory.getItemFactory();

// Create the input source
Source input = new StreamSource("simple.xml");
           
// Create the XItemView object from the input source
XItemView item = itemFactory.item(input);
           
// Create an XOutputParameters object
XOutputParameters params = factory.newOutputParameters();

// Set parameters
params.setMethod("xml");
params.setEncoding("UTF-8");
params.setIndent(true);
           
// Create the result
Result result = new StreamResult(System.out);
           
// Serialize to the result
item.exportItem(result, params);
The following is a basic example of creating a schema-aware transformation.
// Create the factory
XFactory factory = XFactory.newInstance();

// Enable validation
factory.setValidating(true);

// Create the schema source
StreamSource schema = new StreamSource("schema.xsd");

// Register the schema
factory.registerSchema(schema);

// Create the stylesheet source
StreamSource stylesheet = new StreamSource("schema.xsl");

// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet);

// Create the input source
StreamSource input = new StreamSource("schema.xml");

// Create the result
StreamResult result = new StreamResult(System.out);

// Execute the transformation
executable.execute(input, result);



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms of Use | Feedback

Last updatedLast updated: Sep 19, 2011 4:16:02 PM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=matt&product=was-base-dist&topic=txml_ops_xslt
File name: txml_ops_xslt.html