Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > XML applications > Use the XML API to perform operations > Performing basic operations
Performing basic XSLT operations
We can use the XSLTExecutable instances that are 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.
Procedure
- Use the XStaticContext object with the prepareXSLT method to pass in settings that affect how the XSLT stylesheet is prepared.
If no XStaticContext object is provided, the default settings are used.
XStaticContext methods for changing settings relevant to XSLT:
- declareFunction
- Declare a Java extension function
- setBaseURI
- Set the base URI used in the resolution of relative URIs (such as by the fn:resolve-uri function)
The default is the base URI of the expression, query, or stylesheet if the base URI is available. If the base URI is not available, the current working directory is used.
- setSourceResolver
- Set the source resolver used for includes and imports
The default source resolution behavior is to use the base URI of the expression, query, or stylesheet if available to resolve imports and includes. If the base URI is not available, the current working directory is used.
- setMessageHandler
- Set the message handler for prepare-time errors and other messages
The default behavior is to print errors, warnings, and informational messages to System.err and to generate an XProcessException in the case of an unrecoverable error when preparation cannot continue. If the message handler that is registered does not generate its own exception in the case of an unrecoverable error, an XProcessException occurs.
- setIntegerMathMode
- Set the integer math mode to one of the following:
- INTEGER_MATH_MODE_LIMITED_PRECISION (default)
- Use Java long to represent an integer value
- INTEGER_MATH_MODE_ARBITRARY_PRECISION
- Use Java BigInteger to represent an integer value
- INTEGER_MATH_MODE_OVERFLOW_DETECTION
- Use limited precision and generate execution-time errors when overflow is detected
See the XML API documentation for long and BigInteger for more information about the range of values supported for these types.
- setUseCompiler
- Indicate whether to generate a compiled executable or an interpreted executable
A compiled executable takes longer to generate but runs faster than an interpreted executable.
The default is interpreted.
- setDefaultCollation
- Set the default collation used for string comparison operations when the comparison operation does not specify a collation URI explicitly and there is no default collation URI declared within the stylesheet that is in scope for the operation
The default is the Unicode code-point collation URI.
- Provide an XDynamicContext object that can be used to pass settings to the XSLTExecutable.execute method.
The XDynamicContext object passed in to the XSLTExecutable.execute command defines the settings for that execution.
If no XDynamicContext object is provided, the default settings are used.
XDynamicContext methods for changing settings relevant to XSLT:
- bind
- Bind a value to an XSLT parameter
If no value is bound, the default value in the stylesheet is used.
- bindSequence
- Bind a sequence of values to an XSLT parameter
If no value is bound, the default value in the stylesheet is used.
- bindFunction
- Bind a Java method object to an extension function name as declared in the XStaticContext
- setBaseOutputURI
- Set the base output URI to be used when resolving result documents
The default is to use the base URI of the main result document if available. If the base URI is not available, the current working directory is used.
- setMessageHandler
- Set the message handler for execution-time errors and other messages
The default behavior is to print errors, warnings and informational messages to System.err and to generate an XProcessException in the case of an unrecoverable error. If the message handler that is registered does not generate its own exception in the case of an unrecoverable error, an XProcessException occurs.
The message handler registered with the dynamic context is also used for calls to XPath fn:trace and fn:error functions as well as the XSLT xsl:message instruction.
- setImplicitTimeZone
- Set the implicit time zone
The default is to use the system time zone as retrieved through the Java method java.util.TimeZone.getDefault().
- setResultResolver
- Set the result resolver used to resolve the href specified in xsl:result-document instructions
The default behavior is to use the base output URI to resolve result documents.
- setSourceResolver
- Set the source resolver used to resolve documents loaded with the XPath fn:doc function and the XSLT fn:document function
The default behavior is to resolve documents based on the base URI from the static context for fn:doc and for fn:document if no base node is supplied. If the base URI is not available, the current working directory is used.
- setXSLTInitialMode
- Set the initial mode
If no initial mode is specified, the unnamed default mode is used.
- setXSLTInitialTemplate
- Set the initial template rule
If no initial template rule is specified, the initial template is chosen according to the rules of the xsl:apply-templates instruction for processing the initial context node in the initial mode.
- bindCollation
- Bind a Java Collator object or a Locale to a collation URI
If the collation URI is referenced in the stylesheet but no Collator or Locale is bound to it (with the exception of the Unicode code-point collation URI, which is bound by default), an error is raised.
- setCollectionResolver
- Set the collection resolver used to resolve URIs specified in calls to the XPath fn:collection function to a collection of nodes
If the fn:collection function is invoked and no collection resolver is registered an error is raised.
- Achieve an identity transformation by calling the exportItem method on an XItemView object created from a source using the XItemFactory.
An identity XSLTExecutable cannot be created by preparing a null stylesheet.
- Achieve schema-aware transformations.
For schema-aware transformations, the processor picks up schemas registered with the XFactory instance through the registerSchema method or through the registerImportedSchemas method of the XSLTExecutable instance. But you should use the xsl:import-schema declaration to import them into the stylesheet as well, because this makes the stylesheet more portable.
For validating input documents, the schemas can be registered with the XFactory instance or declared in the XML file using the xsi:schemaLocation directive. In either case, validation needs to be enabled using the XFactory class setValidating method
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(); // 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(); // 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(); // 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(); // Execute the transformation executable.execute(input, result);
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
Choosing between the compiler and the interpreter
Use external variables
Use a source resolver at execution time
Use a result resolver at execution time
Register a collection resolver
Work with collations