Understanding the match and select Attributes

Understanding the match and select Attributes

There are two different XSLT attributes that you assign XPath expressions tothe match and the select attributes. You can use the match attribute with the <xsl:template> element, and the select attribute can be used in the <xsl:apply-templates> , <xsl:value-of> , <xsl:for-each> , <xsl:copy-of> , and <xsl: sort > XSLT elements.

The match attribute is restricted to using the current node or direct child nodes. In other words, you can only use the self and the child axesjust those twowith the match attribute. So in XSLT, match= axis :: node-test [ predicate ] is legal only if axis is the self or the child axis. This restriction was made to make implementing XSLT processors easier.

SHOP TALK : SHOP TALK: AN XSLT LOOPHOLE

On the other hand, there's a surprising loophole herethe predicate part of match= axis :: node-test [ predicate ] can hold any XPath expression at all: there's no limitation on which axes you can use. One gets the impression that in the original XSLT 1.0, the committee overlooked this possibility when they put the restriction on the match attribute.

As a result, you're not actually restricted to the self and the child axes in match expressions, because you can use completely general XPath expressions in the predicate of the XPath expression you assign to the match attribute.

So all you have to do is to use the self axis and put the general XPath expression you want to use in the predicate of the location path you assign to the match attribute. (Note, however, that not all XSLT processors may be able to handle completely general XPath expressions, even though they are legal here.)

This is one of the curiosities of XSLT 1.0, and we'll see whether the situation changes in XSLT 2.0 when it's released as a final recommendation.


There are no restrictions on the XPath expressions you can assign to the select attribute. This attribute can be used in the <xsl:apply-templates> , <xsl:value-of> , <xsl:for-each> , <xsl:copy-of> , and <xsl:sort> XSLT elements, and we'll see most of these elements in this chapter.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

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