Everything youve done in this book so far has been fairly straightforward, except for one thing: match patterns, which have been a little mys-terious. Weve used various match patterns, such as /PLANETS in <xsl: template> elements, without offering a lot of systematic explanation on how these patterns really work, as in this case:
<xsl:template match="/PLANETS"> <HTML> <HEAD> <TITLE> The Planets Table </TITLE> </HEAD> <BODY> . . . </BODY> </HTML> </xsl:template>
This chapter is going to provide all you need to know to create match patterns in XSLT. You use match patterns in the <xsl:template> , <xsl:key> , and <xsl:number> elements; weve been working with <xsl:template> from the beginning of the book, and youre going to see <xsl:key> in Chapter 9 and <xsl:number> in Chapter 5. In particular, you use the match attribute of <xsl:template> and <xsl:key> , and the count and from attributes of <xsl: number> , to match patterns.
You also can use match patterns in the select attribute of elements, such as <xsl:apply-templates> , <xsl:value-of> , <xsl:for-each> , and <xsl:copy-of> . Heres one important thing you need to know, however: The select attribute of these elements is more powerful than the match , count , and from attributes of <xsl:template> , <xsl:key> , and <xsl:number> , because you can use full XPath expressions, not just match patterns in select .
Match patterns are a subset of XPath expressionsthat is, all match patterns are valid XPath expressions, but not all XPath expressions are match patterns. The only XPath expressions that can be patterns are those that return a node set (even a node set with one node) and that use paths that specify only child or attribute nodes.
Match patterns are defined in the XSLT recommendation itself, whereas XPath expressions are defined in the XPath recommendation (www.w3.org/TR/xpath); however, the two are compatible because all match patterns are also XPath expressions.
Creating Full XPath Expressions
Chapter 7, Using and Understanding XPath, shows how to create full XPath expressions. You can use full XPath expressions in XSLT in the following places: in the select attribute of the <xsl:apply-templates> , <xsl:value-of> , <xsl:for-each> , <xsl:param> , <xsl:variable> , <xsl:wit-param> , <xsl:copy-of> , and <xsl: sort > elements; in attribute value templates; in the test attribute of <xsl:if> and <xsl:when> elements; in the value attribute of <xsl:number> ; and in the predicates of match patterns.
To make things just a little more confusing, it turns out that you actually can use XPath expressions in a special, optional part (and only this part) of match patterns: the predicate . As youre going to see in this chapter, predicates are XPath expressions that evaluate to either true/false values or numbers , which you enclose in brackets, [ and ] . For example, the pattern PLANET[ NAME ="Venus"] matches the <PLANET> children of the context node that have <NAME> children with text equal to Venus. The expressions inside the [ and ] are true XPath expressions with some restrictions that youll see in this chapter.
There is no question that writing match patterns takes some experience, so I include many examples in this chapter.
Microsoft and Nonstandard Match Patterns
Microsoft supports match patterns with its MSXML3 XML processor, but theres one thing you should know: Microsoft also supports a great deal of non-standard, non-W3C syntax in its match patterns. Im going to stick to the official, W3C, version in this chapter, and if you happen to read Microsoft documentation on match patterns, keep in mind that much of what you read is Microsoft-only.