An XML Document in Need of Links

page 84 glentry term id=servletservletterm defn An application program, written in the Java programming language, that is executed on a web server. A reference to a servlet appears in the markup for a web page, in the same way that a reference to a graphics file appears. The web server executes the servlet and sends the results of the execution if there are any to the web browser. Contrast with xref refid=applet . defn glentry glentry term id=wildcard-charwildcard characterterm defn See xref refid=pattern-matching. defn glentry glossary In this XML listing, each term element has an id attribute that identifies it uniquely. Many xref elements also refer to other terms in the listing. Notice that each time we refer to another term, we dont use the actual text of the referenced term. When we write our stylesheet, well use the XPath id function to retrieve the text of the referenced term; if the name of a term changes as buzzwords go in and out of fashion, some marketing genius might want to rename the pattern-matching character, for example, we can rerun our stylesheet and be confident that all references to the new term contain the correct text. Finally, some term elements have an xreftext element because some of the actual terms are longer than wed like to use in a cross-reference. When we have an xref to the term ASCII American Standard Code for Information Interchange, it would get pretty tedious if the entire text of the term appeared throughout our document. For this term, well use the xreftext attributes value, ensuring that the cross-reference contains the less-intimidating text ASCII .

5.1.3 A Stylesheet That Uses the id Function

Lets look at our desired output. What we want is an HTML document, such as that shown in Figure 5-1 , that displays the various definitions in an easy-to-read format, with the cross- references formatted as hyperlinks. In the HTML document, well need to address several things in our stylesheet: • The title and the h1 contain the first and last terms in the glossary. We can use XPath expressions to generate that information. • The xref elements have been replaced with the xreftext attribute of the referenced term element, if there is one. If that attribute doesnt exist, xref is replaced by the text of the term element. Well use the id function to find the referenced term , and well use XSLTs control elements to check if the xreftext attribute exists. • The hyperlinks generated from the xref elements refer to a named anchor point elsewhere in the HTML document. If xref elements refer to a given term , we have to create a named anchor a name=... at the location of the referenced term . To simplify things, well generate a named anchor for each term automatically, using the id attribute required to be unique by our DTD as the name of the anchor. page 85 • We need to process any seealso elements, as well. These elements are handled similarly to the xref elements, the main difference being that the refids attribute of the seealso element can refer to more than one glossary entry. Figure 5-1. HTML document with generated cross-references Heres the template that takes care of our first task, generating the HTML title and the h1 : xsl:template match=glossary html head title xsl:textGlossary Listing: xsl:text xsl:value-of select=glentry[1]term xsl:text - xsl:text xsl:value-of select=glentry[last]term title head body h1 xsl:textGlossary Listing: xsl:text xsl:value-of select=glentry[1]term xsl:text - xsl:text xsl:value-of select=glentry[last]term h1 xsl:apply-templates select=glentry body html xsl:template We generate the title and h1 using the XPath expressions glentry[1]term for the first term in the document, and using glentry[last]term for the last term. Our next step is to process all the glentry elements. Well generate an HTML paragraph for each one, and then well generate a named anchor point, using the id attribute as the name of the anchor. Heres the template: xsl:template match=glentry p b a name={id} xsl:value-of select=term xsl:text: xsl:text b xsl:apply-templates select=defn