89

How to transform XML with XSLT processor in Java using the JDK?

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Venkat
  • 20,802
  • 26
  • 75
  • 84
  • 2
    Good question, +1. See my answer for pointers to Saxon's documentation and for an important notice that in many cases one doesn't need to know Java in order to use a Java-based XSLT processor. – Dimitre Novatchev Jan 05 '11 at 13:38
  • 11
    This answer has a precise answer, and it is not very obvious to a newcomer what the answer is. This question should be reopened. – Raedwald Aug 08 '16 at 17:41
  • 1
    On topic, since..you know, using programmer tools in a programmer language is off topic here...smh. – rogerdpack Feb 08 '19 at 19:02

4 Answers4

167

Here is sample for using java api for transformer, as @Raedwald said:

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

public class TestMain {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File("transform.xslt"));
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new File("input.xml"));
        transformer.transform(text, new StreamResult(new File("output.xml")));
    }
}

The input can also be from a string or DOMSource, the output can be to a DOMSource etc.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Askar Kalykov
  • 2,553
  • 1
  • 22
  • 43
12

I am new to xslt. Can anybody guide me how to xslt processing with java?

This depends on which Java-based XSLT processor you are using. Each XSLT processor has its own API.

For example, Saxon 6.5.5 (for XSLT 1.0) and Saxon 9.1.07 (for XSLT 2.0) are written in Java. The documentation is at http://www.saxonica.com/documentation/documentation.xml

Almost all XSLT processors have a command-line utility, which doesn't require writing a program in order to perform an XSLT transformation.

For example, here is: how to start a Saxon 9.x transformation from the command line.

Here is how I always use Saxon from the command-line:

java -Xms2048M  -Xmx10000M  -jar  
     C:\xml\Parsers\Saxon\Ver.9.1.0.7\J\saxon9.jar 
    -t  -repeat:1  -o %out%  %xml%  %xsl%  %param[ name=\"value\"]%

where %out% is the name of the output file, %xml% is the xml file, %xsl% is the primary xslt file and %param[ name=\"value\"]% is a name-value list of external parameters (I almost always leave this empty).

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • 9
    There's a standard API for XSLT, see the package `javax.xml.transform`. I'd use that in favor of any third-party library with its own API, unless there's some compelling reason for using a specific third-party library. – Jesper Jan 05 '11 at 14:28
  • 1
    @Jesper: We are *fortunate* that using APIs is not the only way! The majority of XSLT programmers that use any XSLT processor are *not* Java programmers. It would be a totally unfortunate and unjustified loss if they were deprived from using such great XSLT processors as Saxon or Xalan. In fact, producing Java-based XSLT processors that required Java programming in order to use them, wouldn't be profitable at all. – Dimitre Novatchev Jan 05 '11 at 14:35
  • 5
    @Dimitre But the question was about how to do XSLT processing with Java; talking about other ways to do it (not via an API) is not relevant. If you're going to do it using some API, use the standard API unless there is a compelling reason to use some non-standard API. – Jesper Jan 06 '11 at 10:49
  • 1
    @Jesper: Not to make known the way of using an XSLT processor without any programming would be a disservice to the OP and any other reader and would mislead them to believe that writing a program was the only way to do this. My answer is a clear message: There *is* a better and more convenient way; write a program only if you *have* to. – Dimitre Novatchev Jan 06 '11 at 13:38
10

The Java standard library provides an (XSLT) transformation interface for XML parsing. See the API documentation for the classes javax.xml.transform.Transformer and javax.xml.transform.TransformerFactory.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
6

JAXP provides a implementation independent way of working with XSLT transformations. Here is the tutorial to get you started. If you are working with huge XSLT and/or working with multiple XSLT's then there is also an option of caching the parsed XSLT templates for performance reasons. This article explains how to cache xslt's

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327