This section presents the three style sheets. HTML Style SheetThe style sheet for HTML documents is in Listing 4.5. It builds a short table of contents before listing the various news items. Listing 4.5 html.xsl<?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"/> <xsl:template match="/"> <HTML><HEAD><TITLE>Pineapplesoft Daily</TITLE></HEAD> <BODY><H1>Pineapplesoft Daily</H1> <H2><A NAME="toc">Today's News</A></H2> <xsl:for-each select="News/Item"> <P><A HREF="#{ generate-id(.)}"> <xsl:value-of select="Title"/></A><BR/> <SMALL>by <xsl:value-of select="Author"/></SMALL><BR/> <xsl:value-of select="Abstract"/></P> </xsl:for-each> <H2>News Items</H2> <xsl:for-each select="News/Item"> <H3><A NAME="{ generate-id(.)}"><xsl:value-of select="Title"/></A></H3> <P><I>by <xsl:value-of select="Author"/></I></P> <xsl:for-each select="Para"> <P><xsl:value-of select="."/></P> </xsl:for-each> <A HREF="#toc"><SMALL>More News</SMALL></A><BR/> </xsl:for-each> </BODY></HTML> </xsl:template> </xsl:stylesheet> Figure 4.7 presents the result in a browser. Figure 4.7. The HTML document in a browser.
WML Style SheetListing 4.6 is the style sheet to generate WML. Again, it starts with a table of contents. Unlike the HTML document, however, it places each news item on a different card. Listing 4.6 wml.xsl<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" doctype-public="-//WAPFORUM//DTD WML 1.1//EN" doctype-system="http://www.wapforum.org/DTD/wml_1.1.xml"/> <xsl:template match="/"> <wml> <card id="toc" title="Pineapplesoft Daily"> <p align="center"><b>Today's News</b></p> <xsl:for-each select="News/Item"> <p><anchor><xsl:value-of select="Title"/><go href="#{ generate-id(.)} "/></anchor></p> </xsl:for-each> </card> <xsl:for-each select="News/Item"> <card id="{ generate-id(.)} " title="Pineapplesoft Daily"> <p align="center"><b><xsl:value-of select="Title"/></b></p> <p><small>by <xsl:value-of select="Author"/></small></p> <p><xsl:value-of select="Abstract"/></p> <p><small><anchor>More News...<go href="#toc"/></anchor></small></p> <xsl:for-each select="Para"> <p><xsl:value-of select="."/></p> </xsl:for-each> <p><anchor>More News...<go href="#toc"/></anchor></p> </card> </xsl:for-each> </wml> </xsl:template> </xsl:stylesheet> Unlike Listing 4.5, this style sheet generates an XML document. Indeed, WML follows the XML syntax. The style sheet also issues a DOCTYPE statement, as required by the WML specification. Figure 4.8 shows the result in a WAP browser. Figure 4.8. The WML document in a WAP browser.
RSS Style SheetThe last style sheet is in Listing 4.7. This style sheet generates an RSS document. Unlike the previous two style sheets, this one limits itself to a table of contents ”you will recall that RSS is not designed to handle large documents. RSS is essentially a table of contents of the portal Web site. Listing 4.7 rss.xsl<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" doctype-public="-//Netscape Communications//DTD RSS 0.91//EN" doctype-system="http://my.netscape.com/publish/formats/rss-0.91.dtd"/> <xsl:template match="/"> <rss version="0.91"><channel> <title>Pineapplesoft Daily</title> <description>Your source for technology news, trends and facts of interest to web developers.</description> <link>http://www.pineapplesoft.com</link> <language>en</language> <xsl:for-each select="News/Item"> <item> <title><xsl:value-of select="Title"/></title> <link><xsl:value-of select="/News/URL"/>#<xsl:value-of select="generate-id(.)"/></link> <description><xsl:value-of select="Abstract"/></description> </item> </xsl:for-each> </channel></rss> </xsl:template> </xsl:stylesheet> Warning The RSS style sheet is tricky ”it can be used safely with Xalan, but it might not work with other XSL processors. The problem is that the RSS document must include links to the news items. The easiest path is from RSS to the servlet. When a visitor follows a link, RSS will retrieve the news in the appropriate format: HTML or WML. But how do you link it to a specific news item on the news page? You can simply use a reference at the end of the URL: http://localhost:8080/publish/index#N8 . This is where it gets tricky. The style sheet uses generate-id() to create the references. This works because Xalan always generates an identifier based on the original XML document. If you run the style sheet twice on the same document, you get the same identifier. However, the XSL specification is not so strict. It guarantees only that generate-id() will generate unique identifiers for each run. Theoretically, you could run the style sheet twice on the same document and get different identifiers. Therefore, you might find that this trick does not work with other processors. In that case, simply include identifiers in the original XML document: <Item id="N8"/> . Figure 4.9 illustrates the result with My Userland, an RSS portal. Figure 4.9. Registering the RSS file on My Userland.
|