Java Reference
In-Depth Information
<table border="1">
<tr>
<th>Name</th>
<th>EMail</th>
</tr>
<xsl:for-each select="people/person">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="email"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
I haven't shown my XSLT-based
JAXPTransform
program yet. To transform XML using
XSL, you use a set of classes called an
XSLT processor
, which Java includes as part of
JAXP. Another freely available XSLT processor is the Apache XML Project's Xalan. To use
JAXP's XSL transformation, you create an XSL processor by calling the factory method
TransformerFactory.newInstance().newTransformer()
, passing in a
Streamsource
for
the stylesheet. You then call its
transform()
method, passing in a
StreamSource
for the
XML document and a
StreamResult
for the output file. The code for
JAXPTransform
ap-
pears in
Example 20-6
.
Example 20-6. JAXPTransform.java
public
public class
class
JAXPTransform
JAXPTransform
{
/**
* @param args three filenames: XML, XSL, and Output (this order is historical).
* @throws Exception
*/
public
public static
static
void
void
main
(
String
[]
args
)
throws
throws
Exception
{
// Require three input args
iif
(
args
.
length
!=
3
) {
System
.
out
.
println
(
"Usage: java JAXPTransform inputFile.xml inputFile.xsl outputFile"
);
System
.
exit
(
1
);