XSLT

What is XSLT and what is its use?

Extensible Stylesheet Language Transformations (XSLT) is designed to transform XML data to another format, for example HTML, XHTML or another XML format. An XSLT processor executes transformations using one or more XSLT stylesheets, which are also XML documents. 

TrAX

The TrAX (the Transformations API for XML) is a part of JAXP, which gives the possibility to work with any XSLT processor. The core of TrAX is defined in such packages:

javax.xml.transform: There are the TransformerFactory and Transformer classes, which are used to receive an object able to do transformations. After a transformer object is created, its transform() method is called with input and output.

javax.xml.transform.dom: Classes for the creation of input and output objects from a DOM.

javax.xml.transform.sax: Classes for creation input objects from a SAX parser and output objects from a SAX event handler.

javax.xml.transform.stream: Classes for creation input objects and output objects from an I/O stream.

Code to transform an XML document with an XSLT stylesheet and TrAX should follow such steps:

  1. Construct a Source object from the XSLT stylesheet.
  2. Construct a Source object from the input XML document which should be transformed.
  3. Construct a Result object into which the transformed XML document will be written.
  4. Invoke the TransformerFactory.newInstance( ) factory method to create a new TransformerFactory object.
  5. Pass the XSLT Source object to the TransformerFactory object's newTransform( ) method to create a Transform object.
  6. Pass the XML Source and the Result to the Transform object's transform( ) method.

Transform XML to HTML using XSLT in Java Example

Let's discuss the simplest example which transforms XML data to HTML with an XSLT stylesheet. The transformation result is written to newexam.html file.

The following XML file (exam.xml) is used in our example:

<?xml version="1.0"?>
<EXAM>
    <TITLE>OCEJWSD EXAM</TITLE>
    <OBJECTIVES>
        <OBJECTIVE>Objective 1</OBJECTIVE>
        <OBJECTIVE>Objective 2</OBJECTIVE>
    </OBJECTIVES>
    <DURATION>90</DURATION>
</EXAM>

The next XSLT stylesheet (exam.xsl) is used:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:strip-space elements="EXAM"/>
    <xsl:strip-space elements="OBJECTIVES"/>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="/EXAM/TITLE">
        <h1 align="center">
            <xsl:apply-templates/>
        </h1>
    </xsl:template>
    <xsl:template match="/EXAM/DURATION">
        <p>Duration:<xsl:apply-templates/>min.
        </p>
    </xsl:template>
    <xsl:template match="/EXAM/OBJECTIVES">
        <ol>
            <xsl:apply-templates/>
        </ol>
    </xsl:template>
    <xsl:template match="/EXAM/OBJECTIVES/OBJECTIVE">
        <li>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
</xsl:stylesheet>

 

package com.examclouds.xslt;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;

public class MyXSLTransformer {
    public static void main(String[] args) throws TransformerException {
        File xsltFile = new File("exam.xsl");
        File xmlFile = new File("exam.xml");
        //1
        Source xsltSource = new StreamSource(xsltFile);
        //2
        Source xmlSource = new StreamSource(xmlFile);
        //3
        Result result = new StreamResult("newexam.html");
        //4
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        //5
        Transformer transformer = transformerFactory.newTransformer(xsltSource);
        //6
        transformer.transform(xmlSource, result);
        System.out.print("Done!");
    }
}

The content of the created newexam.html file will be:

<html>
<body>
<h1 align="center">OCEJWSD EXAM</h1>
<ol>
<li>Objective 1</li>
<li>Objective 2</li>
</ol>
<p>Duration:90min.
        </p>
</body>
</html>

XML to XML Transformation using XSLT in Java Example

Let's transform XML data from the previous example (exam.xml) to such XML format: 

<?xml version="1.0" encoding="UTF-8"?><EXAM>
    <TITLE>OCEJWSD EXAM</TITLE>
    <OBJECTIVES>
        Objective 1
        Objective 2
    </OBJECTIVES>
    <DURATION>90minutes</DURATION>
</EXAM>

 The XSLT file will be like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <EXAM>
            <xsl:apply-templates/>
        </EXAM>
    </xsl:template>
    <xsl:template match="/EXAM/TITLE">
        <TITLE>
            <xsl:apply-templates/>
        </TITLE>
    </xsl:template>
    <xsl:template match="/EXAM/OBJECTIVES">
        <OBJECTIVES>
            <xsl:apply-templates/>
        </OBJECTIVES>
    </xsl:template>
    <xsl:template match="/EXAM/DURATION">
        <DURATION><xsl:apply-templates/>minutes</DURATION>
    </xsl:template>
</xsl:stylesheet>

The only change in the java code is that we will receive newexam.xml instead of newexam.html:

public class MyXSLTransformer {
    public static void main(String[] args) throws TransformerException {
        File xsltFile = new File("exam.xsl");
        File xmlFile = new File("exam.xml");
        //1
        Source xsltSource = new StreamSource(xsltFile);
        //2
        Source xmlSource = new StreamSource(xmlFile);
        //3
        Result result = new StreamResult("newexam.xml");
        //4
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        //5
        Transformer transformer = transformerFactory.newTransformer(xsltSource);
        //6
        transformer.transform(xmlSource, result);
        System.out.print("Done!");
    }
}
Read also:
Trustpilot
Trustpilot
Comments