The
XML
Stylesheet
Language for
Transformations, or
XSLT, allows for conversion of an XML document into other forms of data. JAXP provides interfaces in package allowing applications to invoke an XSLT transformation. This interface was originally called TrAX (Transformation API for XML), and was developed by an informal collaboration between the developers of a number of Java XSLT processors. Main features of the interface are • a factory class allowing the application to select dynamically which XSLT processor it wishes to use (, , . • methods on the factory class to create a object, representing the compiled form of a stylesheet. This is a thread-safe object that can be used repeatedly, in series or in parallel, to apply the same stylesheet to multiple source documents (or to the same source document with different parameters) (), also , ), a method on the object to create a , representing the executable form of a stylesheet () This cannot be shared across threads, though it is serially reusable. The provides methods to set stylesheet parameters and serialization options (for example, whether output should be indented), and a method to actually run the transformation. (). Two abstract interfaces and are defined to represent the input and output of the transformation. This is a somewhat unconventional use of Java interfaces, since there is no expectation that a processor will accept any class that implements the interface - each processor can choose which kinds of or it is prepared to handle. In practice all JAXP processors supports several standard kinds of Source (, ) and several standard kinds of Result (, ) and possibly other implementations of their own.
Example The most primitive but complete example of XSLT transformation launching may look like this: /* file src/examples/xslt/XsltDemo.java */ package examples.xslt; import java.io.StringReader; import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class XsltDemo { public static void main(String[] args) throws TransformerFactoryConfigurationError, TransformerException { //language=xslt String xsltResource = """ world """; // language=XML String xmlSourceResource = """ """; StringWriter xmlResultResource = new StringWriter(); Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer( new StreamSource(new StringReader(xsltResource)) ); xmlTransformer.transform( new StreamSource(new StringReader(xmlSourceResource)), new StreamResult(xmlResultResource) ); System.out.println(xmlResultResource.getBuffer().toString()); } } It applies the following hardcoded XSLT transformation: world To the following hardcoded XML document: The result of execution will be hello world == Citations ==