Java Reference
In-Depth Information
Solution
Use XSLT; it is fairly easy to use and does not require writing much Java.
Discussion
XSLT, the Extensible Stylesheet Language for Transformations, allows you a great deal of
control over the output format. It can be used to change an XML file from one vocabulary in-
to another, as might be needed in a business-to-business (B2B) application where informa-
tion is passed from one industry-standard vocabulary to a site that uses another. It can also be
used to render XML into another format such as HTML. Some open source projects even use
XSLT as a tool to generate Java source files from an XML description of the required meth-
ods and fields. Think of XSLT as a scripting language for transforming XML.
This example uses XSLT to transform a document containing people's names, addresses, and
so on—such as the file people.xml , shown in Example 20-4 —into printable HTML.
Example 20-4. people.xml
<people>
<people>
<person>
<person>
<name>
<name> Ian Darwin </name>
</name>
<email>
<email> http://www.darwinsys.com/contact.html </email>
</email>
<country>
<country> Canada </country>
</country>
</person>
</person>
<person>
<person>
<name>
<name> Another Darwin </name>
</name>
<email
<email type= "intranet" > afd@node1 </email>
</email>
<country>
<country> Canada </country>
</country>
</person>
</person>
</people>
</people>
You can transform the people.xml file into HTML by using the following command:
$ java xml.JAXPTransform people.xml people.xsl people.html
The output is something like the following:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Our People</title>
Search WWH ::




Custom Search