You are previewing XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition.

XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition

  1. Copyright
  2. About the Author
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. Who This Book Is For
    2. What This Book Covers
    3. How This Book Is Structured
      1. Part I: Foundations
      2. Part II: XSLT and XPath Reference
      3. Part III: Exploitation
      4. Part IV: Appendices
    4. What You Need to Use This Book
    5. Conventions
    6. Downloading the Code
    7. Errata
    8. p2p.wrox.com
  6. List of Examples
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 6
    6. Chapter 12
    7. Chapter 13
    8. Chapter 15
    9. Chapter 16
    10. Chapter 17
    11. Chapter 18
    12. Chapter 19
    13. Chapter 20
    14. Appendix F
  7. I. Foundations
    1. 1. XSLT in Context
      1. 1.1. What Is XSLT?
      2. 1.2. How Does XSLT Transform XML?
      3. 1.3. The Place of XSLT in the XML Family
      4. 1.4. The History of XSL
      5. 1.5. XSLT 2.0 as a Language
      6. 1.6. Summary
    2. 2. The XSLT Processing Model
      1. 2.1. XSLT: A System Overview
      2. 2.2. The XDM Tree Model
      3. 2.3. The Transformation Process
      4. 2.4. Error Handling
      5. 2.5. Variables and Expressions
      6. 2.6. Summary
    3. 3. Stylesheet Structure
      1. 3.1. Changes in XSLT 2.0
      2. 3.2. The Modular Structure of a Stylesheet
      3. 3.3. The <xsl:stylesheet> Element
      4. 3.4. The <?xml-stylesheet?> Processing Instruction
      5. 3.5. Embedded Stylesheets
      6. 3.6. Declarations
      7. 3.7. Instructions
      8. 3.8. Simplified Stylesheets
      9. 3.9. Writing Portable Stylesheets
      10. 3.10. Whitespace
      11. 3.11. Summary
    4. 4. Stylesheets and Schemas
      1. 4.1. XML Schema: An Overview
      2. 4.2. Declaring Types in XSLT
      3. 4.3. Validating the Source Document
      4. 4.4. Validating the Result Document
      5. 4.5. Validating a Temporary Document
      6. 4.6. Validating Individual Elements
      7. 4.7. Validating Individual Attributes
      8. 4.8. The default-validation Attribute
      9. 4.9. Importing Schemas
      10. 4.10. Using xsi:type
      11. 4.11. Nillability
      12. 4.12. Summary
    5. 5. Types
      1. 5.1. What Is a Type System?
      2. 5.2. Changes in 2.0
      3. 5.3. Sequences
      4. 5.4. Atomic Values
      5. 5.5. Atomic Types
      6. 5.6. Schema Types and XPath Types
      7. 5.7. The Type Matching Rules
      8. 5.8. Static and Dynamic Type Checking
      9. 5.9. Summary
  8. II. XSLT and XPath Reference
    1. 6. XSLT Elements
      1. 6.1. xsl:analyze-string
      2. 6.2. xsl:apply-imports
      3. 6.3. xsl:apply-templates
      4. 6.4. xsl:attribute
      5. 6.5. xsl:attribute-set
      6. 6.6. xsl:call-template
      7. 6.7. xsl:character-map
      8. 6.8. xsl:choose
      9. 6.9. xsl:comment
      10. 6.10. xsl:copy
      11. 6.11. xsl:copy-of
      12. 6.12. xsl:decimal-format
      13. 6.13. xsl:document
      14. 6.14. xsl:element
      15. 6.15. xsl:fallback
      16. 6.16. xsl:for-each
      17. 6.17. xsl:for-each-group
      18. 6.18. xsl:function
      19. 6.19. xsl:if
      20. 6.20. xsl:import
      21. 6.21. xsl:import-schema
      22. 6.22. xsl:include
      23. 6.23. xsl:key
      24. 6.24. xsl:matching-substring
      25. 6.25. xsl:message
      26. 6.26. xsl:namespace
      27. 6.27. xsl:namespace-alias
      28. 6.28. xsl:next-match
      29. 6.29. xsl:non-matching-substring
      30. 6.30. xsl:number
      31. 6.31. xsl:otherwise
      32. 6.32. xsl:output
      33. 6.33. xsl:output-character
      34. 6.34. xsl:param
      35. 6.35. xsl:perform-sort
      36. 6.36. xsl:preserve-space
      37. 6.37. xsl:processing-instruction
      38. 6.38. xsl:result-document
      39. 6.39. xsl:sequence
      40. 6.40. xsl:sort
      41. 6.41. xsl:strip-space
      42. 6.42. xsl:stylesheet
      43. 6.43. xsl:template
      44. 6.44. xsl:text
      45. 6.45. xsl:transform
      46. 6.46. xsl:value-of
      47. 6.47. xsl:variable
      48. 6.48. xsl:when
      49. 6.49. xsl:with-param
      50. 6.50. Summary
    2. 7. XPath Fundamentals
      1. 7.1. Notation
      2. 7.2. Where to Start
      3. 7.3. Expressions
      4. 7.4. Lexical Constructs
      5. 7.5. Primary Expressions
      6. 7.6. Variable References
      7. 7.7. Parenthesized Expressions
      8. 7.8. Context Item Expressions
      9. 7.9. Function Calls
      10. 7.10. Conditional Expressions
      11. 7.11. The XPath Evaluation Context
      12. 7.12. Summary
    3. 8. XPath: Operators on Items
      1. 8.1. Arithmetic Operators
      2. 8.2. Value Comparisons
      3. 8.3. General Comparisons
      4. 8.4. Node Comparisons
      5. 8.5. Boolean Expressions
      6. 8.6. Summary
    4. 9. XPath: Path Expressions
      1. 9.1. Examples of Path Expressions
      2. 9.2. Changes in XPath 2.0
      3. 9.3. Document Order and Duplicates
      4. 9.4. The Binary « /» Operator
      5. 9.5. Axis Steps
      6. 9.6. Rooted Path Expressions
      7. 9.7. The « //» Abbreviation
      8. 9.8. Combining Sets of Nodes
      9. 9.9. Summary
    5. 10. XPath: Sequence Expressions
      1. 10.1. The Comma Operator
      2. 10.2. Numeric Ranges: The «to» Operator
      3. 10.3. Filter Expressions
      4. 10.4. The «for» Expression
      5. 10.5. Simple Mapping Expressions
      6. 10.6. The «some» and «every» Expressions
      7. 10.7. Summary
    6. 11. XPath: Type Expressions
      1. 11.1. Converting Atomic Values
      2. 11.2. Sequence Type Descriptors
      3. 11.3. The «instance of» Operator
      4. 11.4. The «treat as» Operator
      5. 11.5. Summary
    7. 12. XSLT Patterns
      1. 12.1. Patterns and Expressions
      2. 12.2. Changes in XSLT 2.0
      3. 12.3. The Formal Definition
      4. 12.4. An Informal Definition
      5. 12.5. Conflict Resolution
      6. 12.6. Matching Parentless Nodes
      7. 12.7. The Syntax of Patterns
      8. 12.8. Summary
    8. 13. The Function Library
      1. 13.1. A Word about Naming
      2. 13.2. Functions by Category
      3. 13.3. Notation
      4. 13.4. Code Samples
      5. 13.5. Function Definitions
      6. 13.6. Summary
    9. 14. Regular Expressions
      1. 14.1. Branches and Pieces
      2. 14.2. Quantifiers
      3. 14.3. Atoms
      4. 14.4. Subexpressions
      5. 14.5. Back-References
      6. 14.6. Character Groups
      7. 14.7. Character Ranges
      8. 14.8. Character Class Escapes
      9. 14.9. Character Blocks
      10. 14.10. Character Categories
      11. 14.11. Flags
      12. 14.12. Disallowed Constructs
      13. 14.13. Summary
    10. 15. Serialization
      1. 15.1. The XML Output Method
      2. 15.2. The HTML Output Method
      3. 15.3. The XHTML Output Method
      4. 15.4. The Text Output Method
      5. 15.5. Using the <xsl:output> declaration
      6. 15.6. Character Maps
      7. 15.7. Disable Output Escaping
      8. 15.8. Summary
  9. III. Exploitation
    1. 16. Extensibility
      1. 16.1. What Vendor Extensions Are Allowed?
      2. 16.2. Extension Functions
      3. 16.3. Keeping Extensions Portable
      4. 16.4. Summary
    2. 17. Stylesheet Design Patterns
      1. 17.1. Fill-in-the-Blanks Stylesheets
      2. 17.2. Navigational Stylesheets
      3. 17.3. Rule-Based Stylesheets
      4. 17.4. Computational Stylesheets
      5. 17.5. Summary
    3. 18. Case Study: XMLSpec
      1. 18.1. Formatting the XML Specification
      2. 18.2. Preface
      3. 18.3. Creating the HTML Outline
      4. 18.4. Formatting the Document Header
      5. 18.5. Creating the Table of Contents
      6. 18.6. Creating Section Headers
      7. 18.7. Formatting the Text
      8. 18.8. Producing Lists
      9. 18.9. Making Cross-References
      10. 18.10. Setting Out the Production Rules
      11. 18.11. Overlay Stylesheets
      12. 18.12. Stylesheets for Other Specifications
      13. 18.13. Summary
    4. 19. Case Study: A Family Tree
      1. 19.1. Modeling a Family Tree
      2. 19.2. Creating a Data File
      3. 19.3. Displaying the Family Tree Data
      4. 19.4. Summary
    5. 20. Case Study: Knight's Tour
      1. 20.1. The Problem
      2. 20.2. The Algorithm
      3. 20.3. Placing the Knight
      4. 20.4. Displaying the Final Board
      5. 20.5. Finding the Route
      6. 20.6. Running the Stylesheet
      7. 20.7. Observations
      8. 20.8. Summary
  10. IV. Appendices
    1. A. XPath 2.0 Syntax Summary
      1. A.1. Whitespace and Comments
      2. A.2. Tokens
      3. A.3. Syntax Productions
      4. A.4. Operator Precedence
    2. B. Error Codes
      1. B.1. Functions and Operators (FO)
      2. B.2. XPath Errors (XP)
      3. B.3. XSLT Errors (XT)
    3. C. Backward Compatibility
      1. C.1. Stage 1: Backward-Compatibility Mode
      2. C.2. Stage 2: Setting version="2.0"
      3. C.3. Stage 3: Adding a Schema
      4. C.4. Summary
    4. D. Microsoft XSLT Processors
      1. D.1. MSXML
      2. D.2. System.Xml
      3. D.3. Summary
    5. E. JAXP: The Java API for Transformation
      1. E.1. The JAXP Parser API
      2. E.2. The JAXP Transformation API
      3. E.3. Examples of JAXP Transformations
      4. E.4. Summary
    6. F. Saxon
      1. F.1. Using Saxon from the Command Line
      2. F.2. Using Saxon from a Java Application
      3. F.3. Using Saxon from a .NET Application
      4. F.4. Saxon Tree Models
      5. F.5. Extensibility
      6. F.6. Extensions
      7. F.7. The evaluate() Extension
      8. F.8. Summary
    7. G. Altova
      1. G.1. Running from within XMLSpy
      2. G.2. Conformance
      3. G.3. Extensions and Extensibility
      4. G.4. The Command Line Interface
      5. G.5. Using the API
      6. G.6. Summary
    8. H. Glossary
O'Reilly logo

Chapter 12. XSLT Patterns

A pattern is used in XSLT to define a condition that a node must satisfy in order to be selected. The most common use of patterns is in the match attribute of <xsl:template>, where the pattern says which nodes the template rule applies to. For example, <xsl:template match ="abstract" introduces a template rule that matches every <abstract> element. This chapter defines the syntax and meaning of XSLT patterns.

Patterns (sometimes called match patterns) are used in just six places in an XSLT stylesheet:

  • In the match attribute of <xsl:template>, to define the nodes in a source document to which a template applies

  • In the match attribute of <xsl:key>, to define the nodes in a source document to which a key definition applies

  • In the count and from attributes of <xsl:number>, to define which nodes are counted when generating numbers

  • In the group-starting-with and group-ending-with attributes of <xsl:for-each-group>, to identify a node that acts as the initial or final node in a group of related nodes

Patterns and Expressions

Most of the patterns found in stylesheets are simple and intuitive. For example:

Pattern

Meaning

title

Matches any <title> element

chapter/title

Matches any <title> element whose parent is a <chapter> element

speech[speaker = "Hamlet"]

Matches any <speech> element that has a child <speaker> element whose value is «Hamlet»

section/para[1]

Matches any <para> element that is the first <para> child of a <section> element

The rules for the more complex patterns, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.