Cover image for XSLT

Book description

XSLT documents a core technology for processing XML. Originally created for page layout, XSLT (Extensible Stylesheet Transformations) is now much more: a general-purpose translation tool, a system for reorganizing document content, and a way to generate multiple results-- such as HTML, WAP, and SVG--from the same content. What sets XSLT apart from other books on this critical tool is the depth of detail and breadth of knowledge that Doug Tidwell, a developer with years of XSLT experience, brings to his concise treatment of the many talents of XSLT. He covers XSLT and XPath, a critical companion standard, and addresses topics ranging from basic transformations to complex sorting and linking. He explores extension functions on a variety of different XSLT processors and shows ways to combine multiple documents using XSLT. Code examples add a real-world dimension to each technique. Useful as XSLT is, its peculiar characteristics make it difficult to get started in, and the ability to use advanced techniques depends on a clear and exact understanding of how XSLT templates work and interact. For instance, the understanding of "variables" in XSLT is deeply different from the understanding of "variables" in procedural languages. The author explains XSLT by building from the basics to its more complex and powerful possibilities, so that whether you're just starting out in XSLT or looking for advanced techniques, you'll find the level of information you need.

Table of Contents

  1. XSLT
    1. Dedication
    2. Preface
      1. Organization of This Book
      2. Where I’m Coming From
        1. I Believe in Open, Platform-Neutral, Standards-Based Computing
        2. I Assume You’re Busy
        3. I Don’t Care Which Standards-Compliant Tools You Use
        4. XSLT Is a Tool, Not a Religion
      3. Conventions Used in This Book
      4. How to Contact Us
      5. Acknowledgments
    3. 1. Getting Started
      1. The Design of XSLT
      2. XML Basics
        1. XML’s Heritage
        2. XML Document Rules
          1. An XML document must be contained in a single element
          2. All elements must be nested
          3. All attributes must be quoted
          4. XML tags are case-sensitive
          5. All end tags are required
          6. Empty tags can contain the end marker
          7. XML declarations
          8. Document Type Definitions (DTDs) and XML Schemas
          9. Well-formed versus valid documents
          10. Tags versus elements
          11. Namespaces
        3. DOM and SAX
          1. DOM
            1. A sample DOM tree
          2. SAX
        4. XML Standards
          1. XML 1.0
          2. The Extensible Stylesheet Language (XSL)
          3. XML Schemas
          4. The Simple API for XML (SAX)
          5. Document Object Model (DOM) Level 1
          6. Document Object Model (DOM) Level 2
          7. Namespaces in XML
          8. Associating stylesheets with XML documents
          9. Scalable Vector Graphics (SVG)
          10. Canonical XML Version 1.0
          11. XML digital signatures
          12. XML Pointer Language (XPointer) Version 1.0
          13. XML Linking Language (XLink) Version 1.0
      3. Installing Xalan
      4. Summary
    4. 2. The Obligatory Hello World Example
      1. Goals of This Chapter
      2. Transforming Hello World
        1. Our Sample Document
        2. A Sample Stylesheet
        3. Transforming the XML Document
        4. Stylesheet Results
      3. How a Stylesheet Is Processed
        1. Parsing the Stylesheet
        2. Parsing the Transformee
        3. Lather, Rinse, Repeat
        4. Walking Through Our Example
      4. Stylesheet Structure
        1. The <xsl:stylesheet> Element
        2. The <xsl:output> Element
        3. Our First <xsl:template>
        4. The <xsl:template> for <greeting> Elements
        5. Built-in Template Rules
          1. Built-in template rule for element and root nodes
          2. Built-in template rule for modes
          3. Built-in template rule for text and attribute nodes
          4. Built-in template rule for comment and processing instruction nodes
          5. Built-in template rule for namespace nodes
        6. Top-Level Elements
        7. Other Approaches
      5. Sample Gallery
        1. The Hello World SVG File
        2. The Hello World PDF File
        3. The Hello World Java Program
        4. The Hello World VRML File
      6. Summary
    5. 3. XPath: A Syntax for Describing Needles and Haystacks
      1. The XPath Data Model
        1. The Root Node
        2. Element Nodes
        3. Attribute Nodes
        4. Text Nodes
        5. Comment Nodes
        6. Processing Instruction Nodes
        7. Namespace Nodes
      2. Location Paths
        1. The Context
        2. Simple Location Paths
        3. Relative and Absolute Expressions
        4. Selecting Things Besides Elements with Location Paths
          1. Selecting attributes
          2. Selecting the text of an element
          3. Selecting comments, processing instructions, and namespace nodes
        5. Using Wildcards
        6. Axes
          1. Unabbreviated syntax
          2. Axis roll call
        7. Predicates
          1. Numbers in predicates
          2. Functions in predicates
      3. Attribute Value Templates
      4. XPath Datatypes
      5. The XPath View of an XML Document
        1. Output View
        2. The Stylesheet
      6. Summary
    6. 4. Branching and Control Elements
      1. Goals of This Chapter
      2. Branching Elements of XSLT
        1. The <xsl:if> Element
          1. Converting to boolean values
          2. Boolean examples
        2. The <xsl:choose> Element
          1. <xsl:choose> example
        3. The <xsl:for-each> Element
          1. <xsl:for-each> example
      3. Invoking Templates by Name
        1. How It Works
        2. Templates à la Mode
      4. Parameters
        1. Defining a Parameter in a Template
        2. Passing Parameters
        3. Global Parameters
          1. Xalan
          2. XT
          3. Microsoft’s XSLT tools
          4. Saxon
          5. Oracle
          6. Setting global parameters in a Java program
      5. Variables
        1. Are These Things Really Variables?
        2. Variable Scope
      6. Using Recursion to Do Most Anything
        1. Implementing a String Replace Function
          1. Procedural design
          2. Recursive design
      7. A Stylesheet That Emulates a for Loop
        1. Template Design
        2. Implementation
        3. The Complete Example
      8. A Stylesheet That Generates a Stylesheet That Emulates a for Loop
        1. XML Input
        2. Template Design
        3. Complications
      9. Summary
    7. 5. Creating Links and Cross-References
      1. Generating Links with the id() Function
        1. The ID, IDREF, and IDREFs Datatypes
        2. An XML Document in Need of Links
        3. A Stylesheet That Uses the id() Function
        4. Limitations of IDs
      2. Generating Links with the key() Function
        1. Defining a key()
        2. A Slightly More Complicated XML Document in Need of Links
        3. Stylesheets That Use the key() Function
          1. The key() function and the IDREFS datatype
          2. Solution #1: Replace the IDREFS datatype
          3. Solution #2: Use the XPath contains() function
          4. Solution #3: Use recursion to process the IDREFS datatype
          5. Solution #4: Use an extension function
        4. Advantages of the key() Function
      3. Generating Links in Unstructured Documents
        1. An Unstructured XML Document in Need of Links
        2. The generate-id() Function
      4. Summary
    8. 6. Sorting and Grouping Elements
      1. Sorting Data with <xsl:sort>
        1. Our First Example
        2. The Details on the <xsl:sort> Element
          1. What’s the deal with that syntax?
          2. Attributes
          3. Where can you use <xsl:sort>?
        3. Another Example
      2. Grouping Nodes
        1. Our First Attempt
        2. A Brute-Force Approach
        3. Grouping with <xsl:variable>
        4. The <xsl:key> Approach
      3. Summary
    9. 7. Combining XML Documents
      1. Overview
      2. The document() Function
        1. An Aside: Doing Math with Recursion
          1. Recursive design
          2. Generating output to initialize a variable
          3. Using format-number() to control output
      3. Invoking the document() Function
      4. More Sophisticated Techniques
        1. The document() Function and Sorting
        2. Implementing Lookup Tables
        3. Grouping Across Multiple Documents
      5. Summary
    10. 8. Extending XSLT
      1. Extension Elements, Extension Functions, and Fallback Processing
        1. Extension Elements
          1. Example: Generating multiple output files
          2. Example: Using extension functions from multiple processors
        2. Extension Functions
          1. Example: A library of trigonometric functions
          2. Example: Writing extensions in other languages
        3. Fallback Processing
      2. Extending the Saxon Processor
      3. More Examples
        1. Generating JPEG Files from XML Content
        2. Accessing a Database with an Extension Element
      4. Summary
    11. 9. Case Study: The Toot-O-Matic
      1. About the Toot-O-Matic
      2. Design Goals
        1. Make It Easier to Create Tutorials
        2. Show Our Audience That We Use the Technologies We Advocate
        3. See Just How Much We Could Accomplish Through Stylesheets
      3. Tutorial Layout
        1. Menu Panel
        2. Individual Panels
        3. Section Menu
        4. Feedback Panel
        5. Email Panel
        6. Zip File
        7. PDF Files
      4. XML Document Design
        1. Individual Panels
      5. XSLT Source Code
        1. Stylesheets and Modes
        2. Initializing Global Variables
        3. Generating the Main Menu Panel
        4. Generating the Section Indexes
        5. Generating the Individual Panels
        6. Generating the PDF Files
        7. Generating the JPEG Files
        8. Generating the Zip File
      6. Summary
    12. A. XSLT Reference
      1. <xsl:apply-imports>
      2. <xsl:apply-templates>
      3. <xsl:attribute>
      4. <xsl:attribute-set>
      5. <xsl:call-template>
      6. <xsl:choose>
      7. <xsl:comment>
      8. <xsl:copy>
      9. <xsl:copy-of>
      10. <xsl:decimal-format>
      11. <xsl:element>
      12. <xsl:fallback>
      13. <xsl:for-each>
      14. <xsl:if>
      15. <xsl:import>
      16. <xsl:include>
      17. <xsl:key>
      18. <xsl:message>
      19. <xsl:namespace-alias>
      20. <xsl:number>
      21. <xsl:otherwise>
      22. <xsl:output>
      23. <xsl:param>
      24. <xsl:preserve-space>
      25. <xsl:processing-instruction>
      26. <xsl:sort>
      27. <xsl:strip-space>
      28. <xsl:stylesheet>
      29. <xsl:template>
      30. <xsl:text>
      31. <xsl:transform>
      32. <xsl:value-of>
      33. <xsl:variable>
      34. <xsl:when>
      35. <xsl:with-param>
    13. B. XPath Reference
      1. XPath Node Types
        1. The Root Node
        2. Element Nodes
        3. Attribute Nodes
        4. Text Nodes
        5. Comment Nodes
        6. Processing-Instruction Nodes
        7. Namespace Nodes
      2. XPath Node Tests
      3. XPath Axes
      4. XPath Operators
        1. Mathematical Operators
        2. Boolean Operators
          1. Comparing values of various datatypes
        3. Expression Operators
      5. Datatypes
      6. The XPath Context
    14. C. XSLT and XPath Function Reference
      1. boolean() Function
      2. ceiling() Function
      3. concat() Function
      4. contains() Function
      5. count() Function
      6. current() Function
      7. document() Function
      8. element-available() Function
      9. false() Function
      10. floor() Function
      11. format-number() Function
      12. function-available() Function
      13. generate-id() Function
      14. id() Function
      15. key() Function
      16. lang() Function
      17. last() Function
      18. local-name() Function
      19. name() Function
      20. namespace-uri() Function
      21. normalize-space() Function
      22. not() Function
      23. number() Function
      24. position() Function
      25. round() Function
      26. starts-with() Function
      27. string() Function
      28. string-length() Function
      29. substring() Function
      30. substring-after() Function
      31. substring-before() Function
      32. sum() Function
      33. system-property() Function
      34. translate() Function
      35. true() Function
      36. unparsed-entity-uri() Function
    15. D. XSLT Guide
      1. How Do I Put Quotes Inside an Attribute Value?
      2. How Do I Convert All Attributes to Elements?
      3. How Do I List All the Elements in an XML Document?
      4. How Do I Implement an if Statement?
      5. How Do I Implement an if-else Statement?
      6. How Do I Implement a for Loop?
      7. How Do I Implement a case Statement?
      8. How Do I Group Elements in an XML Document?
      9. How Do I Group Elements Pulled from Multiple XML Documents?
      10. How Do I Combine XML Documents into a Single Master Document?
      11. How Do I Resolve Cross-References?
      12. How Do I Generate Some Text?
      13. How Do I Control Angle Brackets and Quote Marks in My Output?
    16. Glossary
    17. Index
    18. Colophon