An XML Document in Need of Links
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=defnParts
» O'Reilly-XSLT-Mastering.XML.Transformati... 2264KB Mar 29 2010 05:03:43 AM
» An XML document must be contained in a single element
» XML declarations Document Type Definitions DTDs and XML Schemas
» Well-formed versus valid documents
» Tags versus elements XML Document Rules
» Namespaces XML Document Rules
» The Extensible Stylesheet Language XSL
» Document Object Model DOM Level 1
» Document Object Model DOM Level 2
» Namespaces in XML XML Standards
» Associating stylesheets with XML documents
» Installing Xalan Getting Started
» Our Sample Document A Sample Stylesheet
» Transforming the XML Document
» Stylesheet Results Transforming Hello World
» Parsing the Stylesheet How a Stylesheet Is Processed
» Parsing the Transformee How a Stylesheet Is Processed
» Lather, Rinse, Repeat How a Stylesheet Is Processed
» The xsl:template for greeting Elements
» Built-in template rule for element and root nodes
» Built-in template rule for modes
» Built-in template rule for text and attribute nodes
» Top-Level Elements Stylesheet Structure
» Other Approaches Stylesheet Structure
» The Hello World Java Program
» Goals of This Chapter Summary
» The Root Node The XPath Data Model
» Element Nodes The XPath Data Model
» Attribute Nodes The XPath Data Model
» Text Nodes The XPath Data Model
» Comment Nodes The XPath Data Model
» Processing Instruction Nodes The XPath Data Model
» Namespace Nodes The XPath Data Model
» Simple Location Paths Location Paths
» Relative and Absolute Expressions
» Selecting attributes Selecting Things Besides Elements with Location Paths
» Selecting the text of an element
» Selecting comments, processing instructions, and namespace nodes
» Using Wildcards Location Paths
» Numbers in predicates Predicates
» Functions in predicates Predicates
» Attribute Value Templates XPath Datatypes
» Output View The XPath View of an XML Document
» The Stylesheet The XPath View of an XML Document
» Summary XPath: A Syntax for Describing Needles and Haystacks
» Converting to boolean values
» Boolean examples The xsl:if Element
» xsl:for-each example The xsl:for-each Element
» How It Works Invoking Templates by Name
» Templates à la Mode Invoking Templates by Name
» Defining a Parameter in a Template
» Microsofts XSLT tools Global Parameters
» Setting global parameters in a Java program
» Are These Things Really Variables?
» Procedural design Implementing a String Replace Function
» Recursive design Implementing a String Replace Function
» Template Design Implementation A Stylesheet That Emulates a for Loop
» The Complete Example A Stylesheet That Emulates a for Loop
» XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Template Design A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Complications A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Summary Branching and Control Elements
» The ID, IDREF, and IDREFs Datatypes
» An XML Document in Need of Links
» A Stylesheet That Uses the id Function
» Limitations of IDs Generating Links with the id Function
» Defining a key Generating Links with the key Function
» A Slightly More Complicated XML Document in Need of Links
» The key function and the IDREFS datatype
» Solution 1: Replace the IDREFS datatype
» Solution 2: Use the XPath contains function
» Solution 3: Use recursion to process the IDREFS datatype
» Solution 4: Use an extension function
» Advantages of the key Function
» An Unstructured XML Document in Need of Links
» The generate-id Function Generating Links in Unstructured Documents
» Summary Creating Links and Cross-References
» Our First Example Sorting Data with xsl:sort
» Whats the deal with that syntax?
» Attributes The Details on the xsl:sort Element
» Another Example Sorting Data with xsl:sort
» Our First Attempt Grouping Nodes
» A Brute-Force Approach Grouping with xsl:variable
» Summary Sorting and Grouping Elements
» Recursive design An Aside: Doing Math with Recursion
» Generating output to initialize a variable
» Overview Invoking the document Function
» The document Function and Sorting
» Implementing Lookup Tables More Sophisticated Techniques
» Grouping Across Multiple Documents
» Summary Combining XML Documents
» Example: Generating multiple output files
» Example: Using extension functions from multiple processors
» Example: A library of trigonometric functions
» Example: Writing extensions in other languages
» Fallback Processing Extension Elements, Extension Functions, and Fallback Processing
» Extending the Saxon Processor
» Generating JPEG Files from XML Content
» About the Toot-O-Matic Case Study: The Toot-O-Matic
» Make It Easier to Create Tutorials
» Individual Panels Tutorial Layout
» Email Panel Zip File PDF Files
» Individual Panels XML Document Design
» Stylesheets and Modes XSLT Source Code
» Initializing Global Variables XSLT Source Code
» Generating the Main Menu Panel
» Generating the Section Indexes
» Generating the Individual Panels
Show more