WAS v8.5 > Develop applications > XML applications > Use the XML API to perform operations > Performing basic operations

Performing basic XSLT operations

We can use the XSLTExecutable instances created using XFactory.prepareXSLT methods to perform XSLT transformations. 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.


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);


Related


Use static and dynamic contexts
Use external functions
Use external functions with XSLT
Use a source resolver at prepare time
Use a message handler
Use a message handler and managing exceptions
Choose between the compiler and the interpreter
Use external variables
Use a source resolver at execution time
Use a result resolver at execution time
Registering a collection resolver
Work with collations


+

Search Tips   |   Advanced Search