Download Java And XSLT
Transcript
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. Design Choices The two stylesheets, editPersonalData.xslt and confirmPersonalData.xslt, had a lot in common. To keep things simple, they were written as two independent stylesheets. This is not the only way to implement this code, however. For instance, we cou ld have searched for common functionality and included that functionality from both stylesheets using <xsl:import> or <xsl:include>. This approach did not work here because, although the stylesheets were structured similarly, each template produced different output. As the web site gets more sophisticated, however, you will begin to encounter common page elements such as navigation bars that should not be duplicated in multiple places. Another approach would be to combine both stylesheets into a single stylesheet and pass a top-level parameter indicating whether to use