Cómo llevar a cabo operaciones básicas de XSLT

Puede utilizar las instancias de XSLTExecutable que se crean utilizando los métodos XFactory.prepareXSLT para llevar a cabo transformaciones de XSLT.

Acerca de esta tarea

Las hojas de estilo de XSLT se pueden pasar al método XFactory.prepareXSLT utilizando un objeto de origen JAXP. La instancia resultante de XSLTExecutable ofrece seguridad de hebras y se puede volver a utilizar para transformar varios documentos de entrada.

Procedimiento

Ejemplo

A continuación encontrará un ejemplo básico para preparar y ejecutar una transformación interpretada.
// 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);
A continuación encontrará un ejemplo básico de la preparación y ejecución de una transformación compilada.
// 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);
A continuación encontrará un ejemplo básico de la creación de una transformación de identidad.
// 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);
A continuación encontrará un ejemplo básico de la creación de una transformación que tiene en cuenta los esquemas.
// 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);

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_ops_xslt
File name: txml_ops_xslt.html