Walking Through Our Example
2.4 Stylesheet Structure
As the final part of our introduction to XSLT, well look at the contents of the stylesheet itself. Well explain all the things in our stylesheet and discuss other approaches we could have taken.2.4.1 The xsl:stylesheet Element
The xsl:stylesheet element is typically the root element of an XSLT stylesheet. xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform version=1.0 First of all, the xsl:stylesheet element defines the version of XSLT were using, along with a definition of the xsl namespace. To be compliant with the XSLT specification, your stylesheet should always begin with this element, coded exactly as shown here. Some stylesheet processors, notably Xalan, issue a warning message if your xsl:stylesheet element doesnt have these two attributes with these two values. For all examples in this book, well start the stylesheet with this exact element, defining other namespaces as needed.2.4.2 The xsl:output Element
Next, we specify the output method. The XSLT specification defines three output methods: xml , html , and text . Were creating an HTML document, so HTML is the output method we want to use. In addition to these three methods, an XSLT processor is free to define its own output methods, so check your XSLT processors documentation to see if you have any other options. xsl:output method=html A variety of attributes are used with the different output methods. For example, if youre using method=xml , you can use doctype-public and doctype-system to define the public and system identifiers to be used in the the document type declaration. If youre using method=xml or method=html , you can use the indent attribute to control whether or not the output document is indented. The discussion of the xsl:output element in Appendix A has all the details.2.4.3 Our First xsl:template
Our first template matches , the XPath expression for the documents root element. xsl:template match= xsl:apply-templates select=greeting xsl:template page 282.4.4 The xsl:template for greeting Elements
The second xsl:template element processes any greeting elements in our XML source document. xsl:template match=greeting html body h1 xsl:value-of select=. h1 body html xsl:template2.4.5 Built-in Template Rules
Although most stylesheets well develop in this book explicitly define how various XML elements should be transformed, XSLT does define several built-in template rules that apply in the absence of any specific rules. These rules have a lower priority than any other templates, so theyre always overridden when you define your own templates. The built-in templates are listed here.2.4.5.1 Built-in template rule for element and root nodes
This template processes the root node and any of its children. This processing ensures that recursive processing will continue, even if no template is declared for a given element. xsl:template match=| xsl:apply-templates xsl:template This means that if the structure of a document looks like this: ?xml version=1.0? x y z y z The built-in template rule for element and root nodes means that we could write a template with match=z and the z element will still be processed, even if there are no template rules for the x and y elements.2.4.5.2 Built-in template rule for modes
This template ensures that element and root nodes are processed, regardless of any mode that might be in effect. See Section 4.3.2 in Chapter 4 for more information on the mode attribute. xsl:template match=| mode=x xsl:apply-templates mode=x xsl:template2.4.5.3 Built-in template rule for text and attribute nodes
This template copies the text of all text and attribute nodes to the output tree. Be aware that you have to actually select the text and attribute nodes for this rule to be invoked. xsl:template match=text| xsl:value-of select=. xsl:templateParts
» 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