XSLTransformer.java - XML Transformer

This section provides a tutorial example on how to write an XML transformer program, XSLTransformer.java, using XSL (Extensible Stylesheet Language).

Here is my first XML transformation program using XSL (Extensible Stylesheet Language): XSLTransformer.java:

/* XSLTransformer.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
class XSLTransformer {
   public static void main(String[] args) {
      try {
         File sf = new File(args[0]); // source file
         File rf = new File(args[1]); // result file
         File tf = new File(args[2]); // template file
         TransformerFactory f = TransformerFactory.newInstance();
         Transformer t = f.newTransformer(new StreamSource(tf));
         Source s = new StreamSource(sf);
         Result r = new StreamResult(rf);
      } catch (TransformerConfigurationException e) {
      } catch (TransformerException e) {

Let's try this program with my hello.xml as the source file:

<?xml version="1.0"?>
<body>Hello world!</body>

and my hello.xsl as the template file that contains the transformation style rules:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 <xsl:template match="body">Hello world.! - From hello.xsl.</xsl:template>

Run "java XSLTransformer hello.xml hello.out hello.xsl", I got in hello.out:

herong> java XSLTransformer hello.xml hello.out hello.xsl

herong> more hello.xsl
<?xml version="1.0" encoding="UTF-8"?>
Hello world! - From hello.xsl.

The result shows that my XML file has been transformed correctly.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

XSL (Extensible Stylesheet Language)

 XSL (Extensible Stylesheet Language) - Implementation in JDK

XSLTransformer.java - XML Transformer

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB