XSLT transformacia v Jave

XSLT (Extensible Stylesheet Language Transformations) je jazyk určený na transformovanie XML dokumentov na iné XML dokumenty, alebo iné formáty ako napríklad HTML pre web, obyčajný text alebo do XSL Formatting Objects, ktoré môžu byť zkonvertované do iných formátov, ako napríklad PDF, PostScript, RTF či PNG.

Originálny dokument nie je zmenený, namiesto toho sa vytvorí nový dokument na základe už existujúceho. Vstupné dokumenty sú zväčša typu XML, ale môže byt použité čokoľvek, z čoho procesor dokáže zostaviť XQuery a XPath Data Model.[1]

V jednoduchom príklade ukazujem XSLT transformaciu pomocou programovacieho jazyka Java.

Súbor: library.xml

<?xml version="1.0" encoding="utf-8" ?>
<library>
    <book>
        <id>1</id>
        <name>Java 7</name>
    </book>
    <book>
        <id>2</id>
        <name>Java 8</name>
    </book>
</library>

Súbor: library_stylesheet.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <head>
                <meta charset="utf-8" />
            </head>
            <body>
                <table>
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:apply-templates />
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="book">
        <tr>
            <td><xsl:apply-templates select="id" /></td>
            <td><xsl:apply-templates select="name" /></td>
        </tr>
    </xsl:template>

    <xsl:template match="id">
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="name">
        <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>

Súbor: Transformation.java

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;

public class Transformation
{
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException
    {
        File xmlFile = new File(args[0]);
        File styleSheetFile = new File(args[1]);

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(xmlFile);

        StreamSource streamSource = new StreamSource(styleSheetFile);
        Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    }
}

Kompilácia a spustenie príkladu

user@ntb: ~ $ cd xslt
user@ntb: xslt $ ls -l
total 9.0K
-rwxr-xr-x+ 1 user users 1.7K Aug 21 18:06 Transformation.java
-rwxr-xr-x+ 1 user users  210 Aug 21 17:23 library.xml
-rwxr-xr-x+ 1 user users 1.1K Aug 21 17:23 library_stylesheet.xsl
user@ntb: xslt $ javac Transformation.java
user@ntb: xslt $ ls -l
total 13K
-rwxr-xr-x+ 1 user users 1.5K Aug 21 18:11 Transformation.class
-rwxr-xr-x+ 1 user users 1.7K Aug 21 18:06 Transformation.java
-rwxr-xr-x+ 1 user users  210 Aug 21 17:23 library.xml
-rwxr-xr-x+ 1 user users 1.1K Aug 21 17:23 library_stylesheet.xsl
user@ntb: xslt $ java Transformation library.xml library_stylesheet.xsl
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
</head>
<body>
<table>
<thead>
<tr>
<th>Id</th><th>Name</th>
</tr>
</thead>
<tbody>

<tr>
<td>1</td><td>Java 7</td>
</tr>

<tr>
<td>2</td><td>Java 8</td>
</tr>

</tbody>
</table>
</body>
</html>
user@ntb: xslt $

[1] https://www.w3.org/standards/xml/transformation