Playing with Style SheetsEven if you are convinced of the virtue of XML and XSL as tools to organize your team's next Web project, you might still have some questions. This section deals with frequent issues that arise when deploying this solution. Teaching XSLOne of the hypotheses behind XslServlet is that designers know XSL. Clearly, at the time of this writing, this is optimistic. So is it worth all the effort? Not necessarily . First, XSLT is not so complicated that it can't be learned in a few days. You might find it easier to start with style sheets based on <xsl:for-each> . Listing 8.10 illustrates this. It looks almost like an HTML document peppered with <xsl:for-each> and <xsl:value-of> to extract values from the XML document. Listing 8.10 BugList .xsl (version cool)<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40" version="1.0"> <xsl:output method="html" encoding="ISO-8859-1"/> <xsl:template match="/"> <HTML> <HEAD><TITLE>Bugs List</TITLE></HEAD> <BODY BGCOLOR="#ffcc33" TEXT="#000000" LINK="#0000ff" VLINK="#800080" ALINK="#ffff00"> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD ALIGN="CENTER"> <IMG SRC="/images/psol.gif" ALIGN="LEFT" ALT="Pineapplesoft" HEIGHT="93" WIDTH="60"/> <xsl:for-each select="page/software-list/software"> <B><A><xsl:attribute name="HREF"> <xsl:text>/buglist/cool?softwareid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A></B> <xsl:if test="not(position()=last())"> </xsl:if> </xsl:for-each><BR/> <xsl:for-each select="page/programmer-list/programmer"> <B><A><xsl:attribute name="HREF"> <xsl:text>/buglist/cool?programmerid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A></B> <xsl:text> </xsl:text> </xsl:for-each> <B><A HREF="/buglist/cool">All bugs</A></B> <P><SMALL>This is the bug tracker application. Use it to report new bugs and/or fixes.<BR/> Use the above links to narrow your selection.</SMALL></P> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD><BR/> <P><IMG ALIGN="LEFT" SRC="/images/new.gif"/> <B><A HREF="/bugform/cool">New bug</A></B><BR/> Report a new bug. <BR CLEAR="LEFT"/></P> <xsl:for-each select="page/bug-list/bug"> <P><IMG ALIGN="LEFT"><xsl:attribute name="SRC"> <xsl:text>/images/</xsl:text> <xsl:value-of select="solved"/> <xsl:text>.gif</xsl:text> </xsl:attribute></IMG> <B><A><xsl:attribute name="HREF"> <xsl:text>/bugform/cool?id=</xsl:text> <xsl:value-of select="id"/> </xsl:attribute> <xsl:value-of select="name"/></A></B><BR/> <xsl:value-of select="software-name"/> <xsl:text> from </xsl:text> <xsl:value-of select="created"/> <BR CLEAR="LEFT"/></P> </xsl:for-each> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD ALIGN="CENTER"><BR/> <xsl:for-each select="page/software-list/software"> <A><xsl:attribute name="HREF"> <xsl:text>/buglist/cool?softwareid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A> <xsl:if test="not(position()=last())"> </xsl:if> </xsl:for-each><BR/> <A HREF="/buglist/fast">Text only</A> <xsl:for-each select="page/programmer-list/programmer"> <A><xsl:attribute name="HREF"> <xsl:text>/buglist/cool?programmerid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A> <xsl:text> </xsl:text> </xsl:for-each> <A HREF="/buglist/cool">All bugs</A> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#d3d3d3" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD VALIGN="MIDDLE" ALIGN="CENTER"> <P><SMALL><I>Applied XML Solutions</I> by Benoît Marchal (ISBN 0-7897-2430-8), <A HREF="http://www.mcp.com">Que</A>.<BR/> <A HREF="http://www.marchal.com">Follow this link</A> for the latest updates.</SMALL></P> </TD></TR> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet> Second, several companies are readying XSLT editors. Although not (yet) as convivial as HTML editors, they really simplify coding. Figure 8.6 shows the XSL Editor from IBM (still in preview at the time of this writing). Figure 8.6. IBM's XSL Editor simplifies style sheet writing.
Casting Your Servlet SkinIncidentally, Listing 8.10 demonstrates an added benefit of this technique. By selecting another skin, the visitor enjoys a different presentation. Compare Figure 8.7 with Figure 8.2. Figure 8.7. Change the URL for a different skin.
Furthermore, you can use the same technique to translate the Web site. Listing 8.11 is the style sheet for the French version of Pesticide. In a browser, it looks similar to Figure 8.8. Obviously, though, the data (coming from the database) is not translated. Figure 8.8. The French version requires no programming.
Listing 8.11 BugList.xsl (French version)<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40" version="1.0"> <xsl:output method="html" encoding="ISO-8859-1"/> <xsl:template match="/"> <HTML> <HEAD><TITLE>Liste des Bogues</TITLE></HEAD> <BODY BGCOLOR="#ffcc33" TEXT="#000000" LINK="#0000ff" VLINK="#800080" ALINK="#ffff00"> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD ALIGN="CENTER"> <IMG SRC="/images/psol.gif" ALIGN="LEFT" ALT="Pineapplesoft" HEIGHT="93" WIDTH="60"/> <xsl:for-each select="page/software-list/software"> <B><A><xsl:attribute name="HREF"> <xsl:text>/buglist/fr?softwareid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A></B> <xsl:if test="not(position()=last())"> </xsl:if> </xsl:for-each><BR/> <xsl:for-each select="page/programmer-list/programmer"> <B><A><xsl:attribute name="HREF"> <xsl:text>/buglist/fr?programmerid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A></B> <xsl:text> </xsl:text> </xsl:for-each> <B><A HREF="/buglist/fr">Tous les bogues</A></B> <P><SMALL>Voici l'application de suivi de bogues. Signalez les nouveaux bogues et/ou corrections.<BR/> Les liens ci-dessus adaptent la sélection.</SMALL></P> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD><BR/> <P><IMG ALIGN="LEFT" SRC="/images/new.gif"/> <B><A HREF="/bugform/fr">Nouveau bogue</A></B><BR/> Signalez un nouveau bogue.<BR CLEAR="LEFT"/></P> <xsl:for-each select="page/bug-list/bug"> <P><IMG ALIGN="LEFT"><xsl:attribute name="SRC"> <xsl:text>/images/</xsl:text> <xsl:value-of select="solved"/> <xsl:text>.gif</xsl:text> </xsl:attribute></IMG> <B><A><xsl:attribute name="HREF"> <xsl:text>/bugform/fr?id=</xsl:text> <xsl:value-of select="id"/> </xsl:attribute> <xsl:value-of select="name"/></A></B><BR/> <xsl:value-of select="software-name"/> <xsl:text> du </xsl:text> <xsl:value-of select="created"/> <BR CLEAR="LEFT"/></P> </xsl:for-each> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD ALIGN="CENTER"><BR/> <xsl:for-each select="page/software-list/software"> <A><xsl:attribute name="HREF"> <xsl:text>/buglist/fr?softwareid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A> <xsl:if test="not(position()=last())"> </xsl:if> </xsl:for-each><BR/> <A HREF="/buglist/fast">Version texte</A> <xsl:for-each select="page/programmer-list/programmer"> <A><xsl:attribute name="HREF"> <xsl:text>/buglist/fr?programmerid=</xsl:text> <xsl:value-of select="id"/></xsl:attribute> <xsl:value-of select="name"/></A> <xsl:text> </xsl:text> </xsl:for-each> <A HREF="/buglist/fr">Tous les bogues</A> </TD></TR> </TABLE> <TABLE BORDER="0" ALIGN="CENTER" BGCOLOR="#d3d3d3" WIDTH="600" CELLSPACING="0" CELLPADDING="0"> <TR><TD VALIGN="MIDDLE" ALIGN="CENTER"> <P><SMALL><I>Applied XML Solutions</I> par Benoît Marchal (ISBN 0-7897-2430-8), <A HREF="http://www.mcp.com">Que</A>.<BR/>Visitez <A HREF="http://www.marchal.com">www.marchal.com</A> ou abonnez-vous à la lettre gratuite, <A HREF="http://www.pineapplesoft.com">Pineapplesoft Link</A>, pour les dernières nouvelles.</SMALL></P> </TD></TR> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet> You could also use different style sheets to do the following:
What About Speed?How fast is the XSL processing? Does it penalize the servlet to go through all these steps? Of course, the XSL processing is one more step and it is doomed to slow down the servlet, but, in practice, it is rarely a problem. Indeed, unless your application has very strict time constraints, the user is unlikely to notice the difference. Furthermore, one of the beauties of adopting a standard such as XSL is that you benefit from improvements to the processor. When new and faster processors appear on the market, you can integrate them in your servlet. |