Cover | Table of Contents | Colophon
<account> is
a tag that represents an account element in a
computer program.
<h1> and
<p>. Because HTML is a markup language
instead of a metalanguage, you cannot add new tags and are at the
mercy of the browser vendor to properly implement those tags.
<employee>, while
others may use <worker> or
<associate>. In order to share data, the XML
data has to be transformed into a common format. This is where XSLT
shines -- it eliminates the need to write custom computer programs
to transform data. Instead, you simply create one or more XSLT
stylesheets.
<employee>, while
others may use <worker> or
<associate>. In order to share data, the XML
data has to be transformed into a common format. This is where XSLT
shines -- it eliminates the need to write custom computer programs
to transform data. Instead, you simply create one or more XSLT
stylesheets.
<xsl:template
match=...> and <xsl:apply-templates
select=...>. This should help to solidify your
understanding of the previous example and lay the groundwork for more
sophisticated processing. Although XSLT is a language, it is not
intended to be a general-purpose programming language. Because of its
specialized mission as a transformation language, the design
of XSLT works in the way that XML is structured, which is
fundamentally a tree data structure.
"/"
pattern matches the document itself, which is the root node of the
entire tree.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="schedule.xslt"?>
<schedule>
<owner>
<name>
<first>Eric</first>
<last>Burke</last>
</name>
</owner>
<appointment>
<when>
<date month="03" day="15" year="2001"/>
<startTime hour="09" minute="30"/>
<endTime hour="10" minute="30"/>
</when>
<subject>Interview potential new hire</subject>
<location>Rm 103</location>
<note>Ask Bob for an updated resume.</note>
</appointment>
<appointment>
<when>
<date month="03" day="15" year="2001"/>
<startTime hour="15" minute="30"/>
<endTime hour="16" minute="30"/>
</when>
<subject>Dr. Appointment</subject>
<location>1532 Main Street</location>
</appointment>
<appointment>
<when>
<date month="03" day="16" year="2001"/>
<startTime hour="11" minute="30"/>
<endTime hour="12" minute="30"/>
</when>
<subject>Lunch w/Boss</subject>
<location>Pizza Place on First Capitol Drive</location>
</appointment>
</schedule>
month="03") and child elements to represent its
data. XSLT has the ability to search for and transform both types of
data, as well as comments, processing instructions, and text. In our
current document, the appointments are stored in chronological order.
Later, we will see how to change the sort order using
<xsl:template
match="pattern"> and <xsl:apply-templates
select="node-set-expression"/>. In either case, XPath
syntax is used to locate various types of nodes.
<xsl:if> and
<xsl:choose> in the next chapter, both of
which rely on XPath's ability to represent boolean values of
true and false.
<xsl:apply-templates ...> to search for
patterns in an XML document. This type of processing is sometimes
referred to as a "
data driven" approach because the
data of the XML file drives the selection process. Another style of
XSLT programming is called "template driven," which
means that the template's code tends to drive the selection
process.
<xsl:for-each>
element, which is
reminiscent of traditional programming techniques. In this approach,
you explicitly loop over a collection of nodes without instantiating
a separate template as <xsl:apply-templates>
does. The syntax for <xsl:for-each> is as
follows:
<xsl:for-each select="president"> ...content for each president element </xsl:for-each>
select attribute can contain any XPath
location path, and the loop will iterate over each element in the
resulting node set. In this example, the context is
<president> for all content within the loop.
Nested loops are possible and could be used to loop over the list of
<vicePresident> elements.
<xsl:sort>
is added
as a child element to something else. By adding several consecutive
<xsl:sort> elements, you can accomplish
multifield sorting. Each sort can be in ascending or descending
order, and the data type for sorting is either
"number" or "text". The sort
order defaults to ascending. Some examples of
<xsl:sort> include:
<xsl:sort select="first"/> <xsl:sort select="last" order="descending"/> <xsl:sort select="term/@from" order="descending" data-type="number"/> <xsl:sort select="name/first" data-type="text" case-order="upper-first"/>
case-order attribute
specifies that uppercase letters should be alphabetized before
lowercase letters. The other accepted value for this attribute is
href attribute must
contain the ISBN of the book, which can be found in our original XML
data. An example of the URL we would like to generate is as follows:
<a href="http://www.amazon.com/exec/obidos/ASIN/0596000162">Java and XML</a>
<xsl:value-of
select="isbn"/> directly inside of the attribute.
However, XML does not allow you to insert the less-than
(<) character inside of an attribute value:
<!-- won't work... --> <a href="<xsl:value-of select="isbn"/>">Java and XML</a>
href="..." attribute as an XPath expression, since
the <a> tag is not part of XSLT. There are
two possible solutions to this problem.
<xsl:attribute>
is used to add one
or more attributes to elements. In the following template, an
href attribute is added to an
<a> element:
<xsl:template match="book">
<li>
<a> <!-- the href attribute is generated below -->
<xsl:attribute name="href">
<xsl:text>http://www.amazon.com/exec/obidos/ASIN/</xsl:text>
<xsl:value-of select="@isbn"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</li>
</xsl:template>
<li> tag is used because this is part of
a larger stylesheet that presents a bulleted list of links to each
book. The <a> tag, as you can see, is
missing its href attribute. The
<xsl:attribute> element adds the missing
href. Any child content of
<xsl:attribute> is added to the attribute
value. Because we do not want to introduce any unnecessary
whitespace, <xsl:text> is used. Finally,
<xsl:value-of> is used to select the
isbn attribute.
<xsl:if>
and <xsl:choose>. These allow a stylesheet
to produce different output depending on the results of a
boolean expression
, which must yield
true or false as defined by the
XPath specification.
<xsl:if> element is
comparable to the following Java code:
if (boolean-expression) {
// do something
}<xsl:if>
and <xsl:choose>. These allow a stylesheet
to produce different output depending on the results of a
boolean expression
, which must yield
true or false as defined by the
XPath specification.
<xsl:if> element is
comparable to the following Java code:
if (boolean-expression) {
// do something
}
<xsl:if test="boolean-expression"> <!-- Content: template --> </xsl:if>
test attribute is required and must contain a
boolean expression. If the result is true, the
content of this element is instantiated; otherwise, it is skipped.
The code in Example 3-1 illustrates several uses of
<xsl:if> and related XPath expressions. Code
that is highlighted will be discussed in the next several paragraphs.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!--******************************************************
** "/" template
***************************************************-->
<xsl:template match="/">
<html>
<body>
<h1>Conditional Processing Examples</h1>
<xsl:apply-templates select="presidents"/>
</body>
</html>
</xsl:template>
<!--******************************************************
** "presidents" template
***************************************************-->
<xsl:template match="presidents">
<h3>
List of
<xsl:if> or
<xsl:choose> to produce different content
depending on the value of the parameter that was passed.
<xsl:variable>
element and can be global or local. A global variable is defined at the
"top-level" of a stylesheet, which means that it is
defined outside of any templates as a direct child of the
<xsl:stylesheet> element. Top-level
variables are visible throughout the entire stylesheet, even in
templates that occur before the variable declaration.
<xsl:variable> declaration within that
template and to their descendants. The code in Example 3-2 showed this form of
<xsl:variable> as a mechanism to define the
font color.
<xsl:variable name="homePage">index.html</xsl:variable> <xsl:variable name="lastPresident"select="president[position() = last( )]/name"/> <xsl:variable name="empty"/>
<xsl:variable> specifies the variable value.
In the simple example listed here, the text
index.html is assigned to the
homePage variable. More complex content is
certainly possible, as shown earlier in Example 3-2.
select attribute. The value is an XPath
expression, so in this case we are selecting the name of the last
president in the list.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!--
** Show a name formatted like: "Burke, Eric Matthew"
-->
<xsl:template match="name" mode="lastFirstMiddle">
<xsl:value-of select="last"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="first"/>
<xsl:for-each select="middle">
<xsl:text> disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
<!--
** Show a name formatted like: "Eric Matthew Burke"
-->
<xsl:template match="name" mode="firstMiddleLast">
<xsl:value-of select="first"/>
<xsl:for-each select="middle">
<xsl:text> disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:text> disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="last"/>
</xsl:template>
</xsl:stylesheet>format-number(
)
function is provided by XSLT to convert
numbers such as 123 into formatted numbers such as
$123.00. The function takes the following form:
string format-number(number, string, string?)
<xsl:decimal-format> element. We will cover
only the first two parameters in this book. Interestingly enough, the
behavior of the format-number( ) function is
defined by the JDK 1.1.x version of the
java.text.DecimalFormat class. For complete
information on the syntax of the second argument, refer to the
JavaDocs for JDK 1.1.x.
format-number( ) function. The pattern
$#,##0.00 can properly format a number into just
about any U.S. currency. Table 3-2 demonstrates
several possible inputs and results for this pattern.
|
Number
|
Result
|
|---|---|
|
0
|
$0.00
|
|
0.9
|
$0.90
|
|
0.919
|
$0.92
|
<?xml version="1.0" encoding="UTF-8"?>
<log>
<message text="input parameter was null">
<type>ERROR</type>
<when>
<year>2000</year>
<month>01</month>
<day>15</day>
<hour>03</hour>
<minute>12</minute>
<second>18</second>
</when>
<where>
<class>com.foobar.util.StringUtil</class>
<method>reverse(String)</method>
</where>
</message>
<message text="cannot read config file">
<type>WARNING</type>
<when>
<year>2000</year>
<month>01</month>
<day>15</day>
<hour>06</hour>
<minute>35</minute>
<second>44</second>
</when>
<where>
<class>com.foobar.servlet.MainServlet</class>
<method>init( )</method>
</where>
</message>
<!-- more messages ... -->
</log>
http://jakarta.apache.org and is open source
software.
<project> and
<target>
in the XML build file; <project> must be the
document root element. It is common to have a "prepare"
target that builds the output directories and a "compile"
target that depends on the "prepare" target. If you tell
Ant to execute the "compile" target, it first checks to
see that the "prepare" target has created the necessary
directories. The structure of an
Ant build file looks like this:
<?xml version="1.0"?>
<project name="SampleProject" default="compile" basedir=".">
<!-- global properties -->
<property name="srcdir" value="src"/>
<property name="builddir" value="build"/>
<target name="prepare" description="Creates the output directories">
...tasks
</target>
<target name="compile" depends="prepare">
...tasks
</target>
<target name="distribute" depends="compile">
...tasks
</target>
</project>
HTTP
GET request are passed to the CGI script via the
QUERY_STRING environment variable. HTTP
POST data, on the other hand, is piped to the standard
input stream of the CGI script. CGI always sends results back to the
web server via its standard output.
RequestHandler
.
In the pattern outlined here, you create numerous subclasses of
RequestHandler. Each subclass is responsible for
validation and presentation logic for a small set of related
functions. One manageable strategy is to design one subclass of
RequestHandler for each web page in the
application. Another approach is to create fine-grained request
handlers that handle one specific task, which can be beneficial if
the same piece of functionality is invoked from many different
screens in your application.