T E C H
T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
author="glen"
htmlURL="http://developer.java.sun.com/developer/TechTips/2000/tt0509.html#
tip1"
textURL="http://developer.java.sun.com/developer/TechTips/txtarchive/May00_
GlenM.txt">
</tip>
</tips>
Here is a simple XSLT stylesheet that converts the Index.xml file
above into an HTML document. Notice that the stylesheet itself is
just another XML document.
<HTML xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<!-- File Xform.xsl -->
<BODY><H1>JDC Tech Tips Archive</H1>
<xsl:for-each select="/tips/tip">
<br>
<A HREF="{@htmlURL}"> HTML </A> |
<A HREF="{@textURL}"> TEXT </A> |
<xsl:value-of select="@title"/>
</br>
</xsl:for-each>
</BODY>
</HTML>
To transform the Index.xml document using the XSLT stylesheet,
you need to run both documents through an XSLT processor of your
choice. For example, you can use the open source Java Xalan
processor, which you can download from the Apache XML project
download page for Xalan
(http://xml.apache.org/xalan-j/index.html). To run the transform
using Xalan, set your class path to point to the Xerces and Xalan
JAR files and use the following command line:
java org.apache.xalan.xslt.Process -in Index.xml
-xsl Xform.xsl -out Index.html
The transform takes an input document (Index.xml), processes it
according to rules in the stylesheet (Xform.xsl), and produces
an HTML index in the file Index.html.
Now let's look at what happens when this stylesheet is processed.
The XSLT processor sees the version attribute on the root HTML
element, and infers that it should use the simplified stylesheet
syntax. In this case, the processor simply directs the content of
the stylesheet to the output document. It does this until it sees
an XSLT element. The first XSLT element is the line <xsl:for-each
select="/tips/tip">. So, the processor directs to the output
document the line before the XSLT element, that is, the line
beginning with <BODY>.
The for-each element in <xsl:for-each select="/tips/tip"> is an
iterator similar to a "for" loop in the Java programming
language. The value of the select attribute is an XPath
expression, and the contents of the for-each element will be
processed once for each element in the source document that
matches that expression. Simple XPath expressions such as this
one leverage the familiar "/"-delimited syntax of file systems.
In other words, "/tips/tip" matches all tip elements that are
- SUBSCRIBE
To subscribe to a JDC newsletter mailing list, go to the
Subscriptions page (http://developer.java.sun.com/subscription/),
choose the newsletters you want to subscribe to, and click Update.
- FEEDBACK
Comments? Send your feedback on the JDC Tech Tips to:
jdc-webmaster@sun.com
- ARCHIVES
You'll find the JDC Tech Tips archives at:
http://java.sun.com/jdc/TechTips/index.html
- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html
- LINKS TO NON-SUN SITES
The JDC Tech Tips may provide, or third parties may provide,
links to other Internet sites or resources. Because Sun has no
control over such sites and resources, You acknowledge and agree
that Sun is not responsible for the availability of such external
sites or resources, and does not endorse and is not responsible
or liable for any Content, advertising, products, or other
materials on or available from such sites or resources. Sun will
not be responsible or liable, directly or indirectly, for any
damage or loss caused or alleged to be caused by or in connection
with use of or reliance on any such Content, goods or services
available on or through any such site or resource.
JDC Tech Tips
April 24, 2001
Sun, Sun Microsystems, Java, Java Developer Connection,
JavaServer Pages, and JSP are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other
countries.