|
XSL - Transformer in Java
This tutorial describes:
- XSL Implementations in J2SDK
- A Simple XSL Transformer Program
XSL Implementations in J2SDK
J2SDK 1.4.1_01 offers the following interfaces to support XSL:
- javax.xml.transform.TransformerFactory: The transformer factory class to help to create a transformer object.
- javax.xml.transform.Transformer: The abstract class representing a transformer.
- javax.xml.transform.Source: The interface representing source objects for the transformer to process.
- javax.xml.transform.Result: The interface representing result objects produced by the transformer.
One thing is missing here: who represents the transformation style rules? The answer
is that javax.xml.transform.Source also represents transformation style rules, because
they written in XML.
I have a simple program here, XSLClassChecker.java, to show what are the XSL
implementation classes:
/**
* XSLClassChecker.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
class XSLClassChecker {
public static void main(String[] args) {
try {
TransformerFactory f = TransformerFactory.newInstance();
System.out.println(f.toString());
Transformer t = f.newTransformer();
System.out.println(t.toString());
Source s = new StreamSource();
System.out.println(s.toString());
Result r = new StreamResult();
System.out.println(r.toString());
t.transform(s,r);
} catch (TransformerConfigurationException e) {
System.out.println(e.toString());
} catch (TransformerException e) {
System.out.println(e.toString());
}
}
}
Output:
org.apache.xalan.processor.TransformerFactoryImpl@119298d
org.apache.xalan.transformer.TransformerIdentityImpl@1f33675
javax.xml.transform.stream.StreamSource@1690726
javax.xml.transform.stream.StreamResult@9931f5
javax.xml.transform.TransformerException: No output specified
Note that:
- So J2SDK 1.4.1_01 is using the org.apache.xalan package for XSL transformation.
- The Identity transformer was used, because the program is specify any transformation rules.
- I got the "No output specified" exception, because the program was transforming from nothing
(empty source object) to nowhere (empty result object).
A Simple XSL Transformer Program
Here is my first XSL transformation program in Java:
/**
* XSLTransformer.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
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);
t.transform(s,r);
} catch (TransformerConfigurationException e) {
System.out.println(e.toString());
} catch (TransformerException e) {
System.out.println(e.toString());
}
}
}
Let's try this program with my hello.xml as the source file:
<?xml version="1.0"?>
<p>Hello world!</p>
and my hello.xsl as the template file that contains the transformation style rules:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="p">Hello world.! - From hello.xsl.</xsl:template>
</xsl:stylesheet>
Run "java XSLTransformer hello.xml hello.out hello.xsl", I got in hello.out:
<?xml version="1.0" encoding="UTF-8"?>
Hello world! - From hello.xsl.
|