XSLT Cookbook

Book description

Critical for converting XML documents, and extremely versatile, the XSLT language nevertheless has complexities that can be daunting. The XSLT Cookbook is a collection of hundreds of solutions to problems that Extensible Stylesheet Language Transformations (XSLT) developers regularly face. The recipes range from simple string-manipulation and mathematical processing to more complex topics like extending XSLT, testing and debugging XSLT stylesheets, and graphics creation with SVG. Recipes can be run directly or tweaked to fit your particular application's needs more precisely. Each recipe walks through a problem and a solution, with explanations of the choices made and techniques used in creating that solution, and many recipes include alternate solutions and explore issues like convenience and performance. Topics covered include:

  • String manipulation

  • Mathematical processing

  • Date and time handling

  • Interactions between calendar systems

  • Selecting content in source documents

  • Efficient tree-manipulation

  • Conversions from XML to plain text

  • Tweaking XML documents with stylesheets

  • Using XSLT to query XML documents

  • Generating HTML with XSLT

  • Creating charts and graphs with SVG and XSLT

  • Generating C and XSLT code using XSLT

  • Processing Visio documents in XSLT

  • Working with XML Topic Maps (XTM)

  • Using XSLT to create SOAP documentation from WSDL

  • Extending XSLT with additional functions

  • Embedding XSLT in other processing

  • Testing and debugging XSLT stylesheets

  • Creating generic XSLT processors which work on many XML vocabularies

The XSLT Cookbook provides an ideal companion both for developers still figuring out XSLT's template-based approach who want to learn by example, and for developers who know XSLT and want a collection of quickly reusable recipes. XSLT frequently offers a number of ways to perform a transformation, and the best solution may not always be the most straightforward. The recipes in this Cookbook demonstrate and explain XSLT's template-based logic, a frequent stumbling block for developers new to XSLT. Among the variety of XSLT books now available, none has the explicit solution-oriented approach of this Cookbook.

Table of contents

  1. XSLT Cookbook
    1. Preface
      1. Structure of This Book
      2. Conventions Used in This Book
      3. How to Contact Us
      4. Acknowledgments
    2. 1. Strings
      1. Testing if a String Ends with Another String
        1. Problem
        2. Solution
        3. Discussion
      2. Finding the Position of a Substring
        1. Problem
        2. Solution
        3. Discussion
      3. Removing Specific Characters from a String
        1. Problem
        2. Solution
        3. Discussion
      4. Finding Substrings from the End of a String
        1. Problem
        2. Solution
        3. Discussion
      5. Duplicating a String N Times
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Reversing a String
        1. Problem
        2. Solution
        3. Discussion
      7. Replacing Text
        1. Problem
        2. Solution
        3. Discussion
      8. Converting Case
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Tokenizing a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Making Do Without Regular Expressions
        1. Problem
        2. Solution
        3. Discussion
      11. Using the EXSLT String Extensions
        1. Problem
        2. Solution
        3. Discussion
    3. 2. Numbers and Math
      1. Formatting Numbers
        1. Problem
        2. Solution
          1. Use xsl:decimal-format in conjunction with format-number( )
          2. Use xsl:number
        3. Discussion
          1. Formatting numbers into columns using a fixed number of decimal places
          2. Formatting money like U.S. accountants
          3. Formatting numbers for many European countries
          4. Converting numbers to Roman numerals
          5. Creating column numbers like a spreadsheet
          6. Formatting numbers using Arabic characters
      2. Rounding Numbers to a Specified Precision
        1. Problem
        2. Solution
        3. Discussion
      3. Converting from Roman Numerals to Numbers
        1. Problem
        2. Solution
        3. Discussion
      4. Converting from One Base to Another
        1. Problem
        2. Solution
        3. Discussion
      5. Implementing Common Math Functions
        1. Problem
        2. Solution
          1. Absolute value: math:abs(x)
          2. Square root: math:sqrt(x)
          3. Logarithms: math:log10(number), math:log(number), and math:logN(x,base)
          4. Power: math:power(base,power)
          5. Factorial
        3. Discussion
      6. Computing Sums and Products
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Finding Minimums and Maximums
        1. Problem
        2. Solution
        3. Discussion
      8. Computing Statistical Functions
        1. Problem
        2. Solution
        3. Discussion
      9. Computing Combinatorial Functions
        1. Problem
        2. Solution
        3. Discussion
      10. Testing Bits
        1. Problem
        2. Solution
        3. Discussion
    4. 3. Dates and Times
      1. Introduction
        1. See Also
      2. Calculating the Day of the Week
        1. Problem
        2. Solution
        3. Discussion
      3. Determining the Last Day of the Month
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Getting Names for Days and Months
        1. Problem
        2. Solution
        3. Discussion
      5. Calculating Julian and Absolute Day Numbers from a Specified Date
        1. Problem
        2. Solution
        3. Discussion
      6. Calculating the Week Number for a Specified Date
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Working with the Julian Calendar
        1. Problem
        2. Solution
        3. Discussion
      8. Working with the ISO Calendar
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Working with the Islamic Calendar
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Working with the Hebrew Calendar
        1. Problem
        2. Solution
        3. Discussion
      11. Formatting Dates and Times
        1. Problem
        2. Solution
        3. Discussion
      12. Determining Secular and Religious Holidays
        1. Problem
        2. Solution
        3. Discussion
    5. 4. Selecting and Traversing
      1. Optimizing Node Selections
        1. Problem
        2. Solution
          1. Avoid unnecessary reliance on default processing rules
          2. Avoid using the descendant, descendant-or-self, preceding, or following axes when they aren’t necessary
          3. Prefer “selecting” and “matching” over “filtering”
          4. Cache frequently used node sets in variables
          5. Use xsl:key if nodes are frequently selected by static criteria
        3. Discussion
          1. Avoid unnecessarily reliance on default processing rules
          2. Prefer “selecting” and “matching” over “filtering”
          3. Cache frequently used node sets in variables
          4. Use xsl:key if nodes will be selected by static criteria frequently
      2. Determining if Two Nodes Are the Same
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Ignoring Duplicate Elements
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Selecting All but a Specific Element
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Performing a Preorder Traversal
        1. Problem
        2. Solution
        3. Discussion
      6. Performing a Postorder Traversal
        1. Problem
        2. Solution
        3. Discussion
      7. Performing an In-Order Traversal
        1. Problem
        2. Solution
        3. Discussion
      8. Performing a Level-Order Traversal
        1. Problem
        2. Solution
        3. Discussion
      9. Processing Nodes by Position
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    6. 5. XML to Text
      1. Dealing with Whitespace
        1. Problem
        2. Solution
          1. Too much whitespace
          2. Too little whitespace
        3. Discussion
        4. See Also
      2. Exporting XML to Delimited Data
        1. Problem
        2. Solution
          1. Create a CSV file from flat attribute-encoded elements
          2. Create a CSV file from flat element-encoded data
          3. Handle more complex mappings
        3. Discussion
      3. Creating a Columnar Report
        1. Problem
        2. Solution
        3. Discussion
      4. Displaying a Hierarchy
        1. Problem
        2. Solution
        3. Discussion
      5. Numbering Textual Output
        1. Problem
        2. Solution
          1. Number siblings sequentially
          2. Start from a number other than one
          3. Number elements globally
          4. Number elements globally within a subcontext
          5. Number hierarchically
        3. Discussion
      6. Wrapping Text to a Specified Width and Alignment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 6. XML to XML
      1. Converting Attributes to Elements
        1. Problem
        2. Solution
        3. Discussion
      2. Converting Elements to Attributes
        1. Problem
        2. Solution
        3. Discussion
      3. Renaming Elements or Attributes
        1. Problem
        2. Solution
        3. Discussion
      4. Merging Documents with Identical Schema
        1. Problem
        2. Solution
        3. Discussion
      5. Merging Documents with Unlike Schema
        1. Problem
        2. Solution
          1. Incorporate one document as a subpart of a parent document
          2. Weave two documents together
          3. Join elements from two documents to make new elements
        3. Discussion
        4. See Also
      6. Splitting Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Flattening an XML Hierarchy
        1. Problem
        2. Solution
        3. Discussion
      8. Deepening an XML Hierarchy
        1. Problem
        2. Solution
          1. Add structure based on existing data
          2. Add structure to correct a poorly designed document
        3. Discussion
          1. Add structure based on existing data
          2. Add structure to correct a poorly designed document
      9. Reorganizing an XML Hierarchy
        1. Problem
        2. Solution
        3. Discussion
    8. 7. Querying XML
      1. Performing Set Operations on Node Sets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      2. Performing Set Operations on Node Sets Using Value Semantics
        1. Problem
        2. Solution
        3. Discussion
      3. Determining Set Equality by Value
        1. Problem
        2. Solution
        3. Discussion
      4. Performing Structure-Preserving Queries
        1. Problem
        2. Solution
        3. Discussion
      5. Joins
        1. Problem
        2. Solution
        3. Discussion
      6. Implementing the W3C XML Query-Use Cases in XSLT
        1. Problem
        2. Solution
        3. Discussion
    9. 8. XML to HTML
      1. Using XSLT as a Styling Language
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      2. Creating Hyperlinked Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Creating HTML Tables
        1. Problem
        2. Solution
        3. Discussion
      4. Creating Frames
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Creating Data-Driven Stylesheets
        1. Problem
        2. Solution
        3. Discussion
      6. Creating a Self-Contained HTML Transformation
        1. Problem
        2. Solution
        3. Discussion
      7. Populating a Form
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 9. XML to SVG
      1. Transforming an Existing Boilerplate SVG
        1. Problem
        2. Solution
        3. Discussion
      2. Creating Reusable SVG Generation Utilities for Graphs and Charts
        1. Problem
        2. Solution
          1. Axis generation
          2. Bar generation
          3. XY plots
          4. Pie-slice generation
          5. Open-Hi-Lo-Close plots
        3. Discussion
      3. Creating a Tree Diagram
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Creating Interactive SVG-Enabled Web Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    11. 10. Code Generation
      1. Generating Constant Definitions
        1. Problem
        2. Solution
        3. Discussion
      2. Generating Switching Code
        1. Problem
        2. Solution
        3. Discussion
      3. Generating Message-Handling Stub Code
        1. Problem
        2. Solution
        3. Discussion
      4. Generating Data Wrappers
        1. Problem
        2. Solution
        3. Discussion
      5. Generating Pretty Printers
        1. Problem
        2. Solution
        3. Discussion
      6. Generating a Test Data-Entry Web Client
        1. Problem
        2. Solution
        3. Discussion
      7. Generating Test-Entry Web CGI
        1. Problem
        2. Solution
        3. Discussion
      8. Generating Code from UML Models via XMI
        1. Problem
        2. Solution
        3. Discussion
      9. Generating XSLT from XSLT
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    12. 11. Vertical XSLT Application Recipes
      1. Converting Visio VDX Documents to SVG
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      2. Working with Excel XML Spreadsheets
        1. Problem
        2. Solution
        3. Discussion
      3. Generating XTM Topic Maps from UML Models via XMI
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Generating Web Sites from XTM Topic Maps
        1. Problem
        2. Solution
          1. Sorting algorithms
          2. Programming languages
          3. Root topic
          4. Page elements and layout
        3. Discussion
        4. See Also
      5. Serving SOAP Documentation from WSDL
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    13. 12. Extending and Embedding XSLT
      1. Saxon Extension Functions
      2. Saxon Extension Elements
      3. Xalan Java 2 Extension Functions
      4. Java Extension Function Using the Class Format Namespace
      5. Java Extension Function Using the Package Format Namespace
      6. Java Extension Function Using the Java Format Namespace
      7. Scripting Extension Function Using Inline Script Code
      8. Xalan Java 2 Extension Elements
      9. Java Extension Element
      10. Scripting Extension Elements
      11. MSXML Extension Functions
        1. See Also
      12. Using Saxon’s and Xalan’s Native Extensions
        1. Problem
        2. Solution
          1. You want to output to more than one destination
          2. You want to split a complex transformation into a series of transformations in a pipeline
          3. You want to work with dates and times
          4. You need a more efficient implementation of set operations
          5. You want extended information about a node in the source tree
          6. You want to interact with a relational database
          7. You want to dynamically evaluate an XPath expression created at runtime
          8. You want to change the value of a variable
          9. You want to write first-class extension functions in XSLT
        3. Discussion
        4. See Also
      13. Extending XSLT with JavaScript
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Adding Extension Functions Using Java
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Adding Extension Elements Using Java
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Using XSLT from Perl
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Using XSLT from Java
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    14. 13. Testing and Debugging
      1. Using xsl:message Effectively
        1. Problem
        2. Solution
        3. Discussion
      2. Tracing the Flow of Your Stylesheet Through Its Input Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Automating the Insertion of Debug Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Including Embedded Unit Test Data in Utility Stylesheets
        1. Problem
        2. Solution
        3. Discussion
      5. Structuring Unit Tests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Testing Boundary and Error Conditions
        1. Problem
        2. Solution
          1. Boundary-condition testing
          2. Error-condition testing
        3. Discussion
    15. 14. Generic and Functional Programming
      1. Introduction
        1. Extending the Content of Global Variables
        2. Using Template Tags
        3. See Also
      2. Creating Polymorphic XSLT
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Creating Generic Element Aggregation Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Creating Generic Bounded Aggregation Functions
        1. Problem
        2. Solution
        3. Discussion
      5. Creating Generic Mapping Functions
        1. Problem
        2. Solution
        3. Discussion
      6. Creating Generic Node-Set Generators
        1. Problem
        2. Solution
        3. Discussion
    16. Index
    17. Colophon

Product information

  • Title: XSLT Cookbook
  • Author(s):
  • Release date: December 2002
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596003722