Example 6-3. Example XML output
<?xml version="1.0" encoding="UTF-8"?>
<page>
<!-- the next element is optional: -->
<!-- <requiredFieldsMissing/> -->
<personalData>
<firstName required="true">Eric</firstName>
<lastName required="true">Burke</lastName>
<daytimePhone required="true">636-123-4567</daytimePhone>
<eveningPhone/>
<email required="true">burke_e@yahoo.com</email>
</personalData>
</page>
As you can see, the XML is very minimal. None of the captions, such
as "First Name:", are included,
because they are all specified in the XSLT stylesheets. Even the
asterisk character (*) is omitted, giving the XSLT author complete
control over how things are rendered. The XML is used only for data,
so you can use the stylesheets to include graphics, render the output
in a foreign language, or combine page fragments from other sources,
such as headers and footers, into your web pages.
<i>Fields marked with (*) are required.</i>
<form action="/chap6/personalData" method="post">
The template finally produces a table so that all of the headings and
text fields are properly aligned. As in earlier stylesheet examples,
this template creates the table, while another template creates each
row in the table:
<table border="0" cellpadding="2" cellspacing="0">
<!-- Select all immediate children, such as firstName,
lastName, daytimePhone, etc... -->
<xsl:apply-templates/>
</table>
<div align="center">
<hr/>
<input type="submit" name="submitBtn" value="Submit"/>
</div>
</form>
</xsl:template>
Since this particular instance of
<xsl:apply-templates/> does not utilize the
select attribute, all child elements will be
selected. The next template is designed to match each of the possible
types of elements that can appear and will be instantiated once for
each occurrence of <firstName>,
<lastName>, etc.:
<xsl:template
match="firstName|lastName|daytimePhone|eveningPhone|email">
This template first produces a <tr> element.
If this particular element has a required="true"
attribute, the XML data contains
<requiredFieldsMissing/>. The value of this
element is an empty string, the font is changed to bold and red. This
indicates to the user that a required field was missing. The font
weight and color are inserted as the style
attribute on the <tr> element as follows:
<tr>
<xsl:if test="@required='true'
and ../../requiredFieldsMissing
and .=''">
<xsl:attribute name="style">
<xsl:text>color:red; font-weight:bold;</xsl:text>
</xsl:attribute>
</xsl:if>
The template then produces its first <td>
tag, which contains the caption for the current field. It would be
nice if XSLT offered a lookup table mechanism for situations such as
this, but <xsl:choose> does get the job
done:
<td>
<xsl:choose>
<xsl:when test="name( )='firstName'">
First Name:</xsl:when>
<xsl:when test="name( )='lastName'">
Last Name:</xsl:when>
<xsl:when test="name( )='daytimePhone'">
Daytime Phone:</xsl:when>
<xsl:when test="name( )='eveningPhone'">
Evening Phone:</xsl:when>
<xsl:when test="name( )='email'">
Email:</xsl:when>
</xsl:choose>
</td>
This is still better than hardcoding the captions into the XML or
servlet because we can make changes to the stylesheet without
recompiling anything. You can even change the captions to a foreign
language without affecting any of the Java code, offering remarkable
flexibility to web page designers.