Chapter 6: Patterns


Overview

A pattern defines 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 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

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, however, are quite technical-so I'm afraid some of the explanations in this chapter are not going to be easy reading.

Patterns are defined in terms of the name , type, and content of a node, and its position relative to other nodes in the tree. To understand how patterns work you therefore need to understand the tree model(described in Chapter 2) and the different kinds of node.

Patterns look very similar to XPath expressions, and it turns out that they are closely related. However, patterns and expressions are not quite the same thing. In terms of its syntax, every pattern is a valid XPath expression, but not every XPath expression is a valid pattern. It wouldn't make any sense to use the expression «2+2 » as a pattern, for example-which nodes would it match?

The full rules for expressions are given in XPath 2.0 Programmer's Reference. Expressions are defined in the XPath 2.0 Recommendation, which allows them to be used in contexts other than XSLT stylesheets. For example, XPath expressions are used in the XPointer specification to define hyperlinks between documents, and they are used in some Document Object Model (DOM) implementations as a way for applications to navigate around the DOM data structure. Patterns, however, are local to the XSLT Recommendation, and they are found only in stylesheets.

It would have been quite possible for XSLT to define both the syntax and the meaning of patterns quite independently of the XPath rules for expressions, but this would risk unnecessary inconsistency. What the XSLT language designers chose to do instead was to define the syntax of patterns in such a way that every pattern was sure to be a valid expression, and then to define the formal meaning of the pattern in terms of the meaning of the expression.

Look at the simplest pattern in the earlier examples, «title » . If «title » is used as an expression, it's an abbreviation for «./child::title » , and it means "select all the <title> children of the context node." How do we get from that to a definition of the pattern «title » as something that matches all <title> elements?

The section The Formal Definition gives the formal definition of patterns in terms of expressions. In practice, it's easier to think of most patterns as following their own rules-rather like the intuitive examples listed earlier-and referring to the formal definition only to resolve difficult cases. So I'll follow the formal explanation with an informal definition that's not only more intuitive, but also closer to the way most implementations are likely to work.

When patterns are used in template rules, we need to consider what happens if the same node is matched by more than one pattern. This situation is discussed in the section Conflict Resolution.

A new feature of the XPath 2.0 data model is that it is possible to create nodes in a tree whose root node is not a document node. Such trees cause additional complications for the semantics of pattern matching. The section Matching Parentless Nodes explains how these nodes are handled.

The bulk of the chapter is then devoted to an explanation of the syntax of patterns, and the usage of each syntactic construct that can appear in a pattern.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net