Open In App

XSL Processor in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, XSLProcessor is a class that extends java.lang.Object class. It provides methods to create XSLStylesheet objects and to transform an input XML document. XSL stands for eXtensible Stylesheet Language which is used for creating the style sheets just like CSS, which describes how to show an XML document of a given type.

--> java.lang Package
    --> Object Class
        --> XSL Processor Class extends Object Class  

Methods of XSLProcessor Class

Methods Description
getParam(java.lang.String name) Uses getParam(String, String);
getParam(java.lang.String uri, java.lang.String name) Gets the value of a top-level stylesheet parameter.
 XSLStylesheet newXSLStylesheet(java.io.InputStream xsl) Constructs an XSLStylesheet using the given Inputstream XSL function document(”) is not supported as there is no way to re-access the input Stylesheet as XMLDocument.
 XSLStylesheet newXSLStylesheet(java.io.Reader xsl) Constructs an XSLStylesheet using the given Reader XSL function document(”) is not supported as there is no way to re-access the input Stylesheet as XMLDocument.
newXSLStylesheet(java.net.URL xsl) Constructs an XSLStylesheet using the given URL
newXSLStylesheet(XMLDocument xsl) Constructs an XSLStylesheet using the given XMLDocument
processXSL(XSLStylesheet xsl, java.io.InputStream xml, java.net.URL ref) Transforms the input XML document using given InputStream and stylesheet.
processXSL(XSLStylesheet xsl, java.io.Reader xml, java.net.URL ref) Transforms the input XML document using given Reader and stylesheet.
processXSL(XSLStylesheet xsl, java.net.URL xml, java.net.URL ref) Transforms the input XML document using given URL and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocument xml) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocument xml, ContentHandler handler) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocumentFragment inp) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocumentFragment xml, java.io.OutputStream os) Transforms the input XML using given XMLDocumentFragment and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocumentFragment xml, java.io.PrintWriter pw) Transforms the input XML using given XMLDocumentFragment and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocumentFragment inp, XMLDocumentHandler handler) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocument xml, java.io.OutputStream os) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocument xml, java.io.PrintWriter pw) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLDocument xml, XMLDocumentHandler handler) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLElement inp) Transforms the input XML document using given XMLDocument and stylesheet.
processXSL(XSLStylesheet xsl, XMLElement inp, ContentHandler handler) Transforms the input XML document using given XMLElement and stylesheet.
processXSL(XSLStylesheet xsl, XMLElement xml, java.io.OutputStream os) Transforms the input XML using given XMLElement and stylesheet.
processXSL(XSLStylesheet xsl, XMLElement xml, java.io.PrintWriter pw) Transforms the input XML using given XMLElement and stylesheet.
processXSL(XSLStylesheet xsl, XMLElement xml, XMLDocumentHandler handler) Transforms the input XML document using given XMLElement and stylesheet.
removeParam(java.lang.String uri, java.lang.String name) Removes the value of a top-level stylesheet parameter.
resetParams() Resets all the params set.
setBaseURL(java.net.URL url) Sets the base url to resolve include/import hrefs EntityResolver if set is used before using the base url.
setEntityResolver(EntityResolver eResolver) Sets the entity resolver to resolve include/import hrefs if not set, base url (if set) is used.
setErrorStream(java.io.OutputStream out) Generate an output stream for the output of warnings.
setLocale(java.util.Locale locale) This method applications can use this to set the locale for error reporting.
setOutputEncoding(java.lang.String externalenc) In this method if output encoding is not specified in XSL document, output charset as “UTF-8” in META element by default.
setParam(java.lang.String uri, java.lang.String name, java.lang.Object value) Sets the value of a top-level stylesheet parameter.
setXSLTVersion(XSLProcessor.XSLTVersion version) Sets the specification version to be used for transformation.
showWarnings(boolean flag) Used when switching to determine whether to output warnings.

Example 

Java




// Java Program to Illustrate XSLSample Class
 
// Class
public class XSLSample {
 
    // Main driver method
    public static void main(String args[]) throws Exception
    {
        if (args.length < 2) {
            System.err.println(
                "Usage: java XSLSample xslFile xmlFile.");
            System.exit(1);
        }
 
        // Creating a new XSLProcessor
        XSLProcessor processor = new XSLProcessor();
 
        // Register a base URL to resolve relative
        // references processor.setBaseURL(baseURL);
 
        // Or register an org.xml.sax.EntityResolver to
        // resolve relative references
        // processor.setEntityResolver(myEntityResolver);
 
        // Register an error log
        // processor.setErrorStream(new
        // FileOutputStream("error.log"));
 
        // Set any global parameters to the processor
        // processor.setParam(namespace, param1, value1);
        // processor.setParam(namespace, param2, value2);
 
        // resetParam is for multiple XML documents with
        // different parameters
 
        String xslFile = args[0];
        String xmlFile = args[1];
 
        // Create a XSLStylesheet
        //  The stylesheet can be created using one of
        //  following inputs:
        //
        // XMLDocument xslInput = /* using DOMParser; see
        // later in this code */ URL         xslInput = new
        // URL(xslFile); Reader      xslInput = new
        // FileReader(xslFile);
 
        InputStream xslInput = new FileInputStream(xslFile);
        XSLStylesheet stylesheet
            = processor.newXSLStylesheet(xslInput);
 
        // Prepare the XML instance document
        //   The XML instance can be given to the processor
        //   in one of
        // following ways:
        //
        // URL         xmlInput = new URL(xmlFile);
        // Reader      xmlInput = new FileReader(xmlFile);
        // InputStream xmlInput = new
        // FileInputStream(xmlFile); Or using DOMParser
 
        DOMParser parser = new DOMParser();
        parser.retainCDATASection(false);
        parser.setPreserveWhitespace(true);
        parser.parse(xmlFile);
        XMLDocument xmlInput = parser.getDocument();
 
        // Transform the XML instance
        //   The result of the transformation can be one of
        //   the following:
        //
        // 1. Return a XMLDocumentFragment
        // 2. Print the results to a OutputStream
        // 3. Report SAX Events to a ContentHandler
 
        // 1. Return a XMLDocumentFragment
        XMLDocumentFragment res;
        res = processor.processXSL(stylesheet, xmlInput);
 
        // Print the result to System.out
        res.print(System.out);
 
        // 2. Print the results to a OutputStream
        // processor.processXSL(stylesheet, xmlInput,
        // System.out);
 
        // 3. Report SAX Events to a ContentHandler
        // ContentHandler cntHandler = new
        // MyContentHandler();
        // processor.processXSL(stylesheet, xmlInput,
        // cntHandler);
 
        // Display message for successful execution
        System.out.println("Executed Successfully");
    }
}


Output:

Executed Successfully

XSL also adds: 

  • A transformation language considering XML documents: XLST stands for eXtensible Stylesheet Language Transformation. Initially, intended to perform complex styling operations, like creating the tables of contents and indexes, now it is used as a general-purpose XML processing language. Thus, XSLT is widely used apart from XSL, like creating HTML web pages from XML data.
  • The latest styling features, illustrated by an XML document type that defines a set of elements known as Formatting Objects, and attributes.

XSLT Processor for Java:

Oracle provides XSLT processing for Java, C, C++, and PL/SQL. This article describes the XSLT Processor for Java. XSLT is of two version i.e. a version 1.0, and also a 2.0 version currently in process which is a W3C Internet standard. XSLT also uses the navigational language XPath, and has corresponding versions 1.0 and 2.0. In Java, both XSLT and XPath are implemented by the XSLT Processor.



Last Updated : 25 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads