Pattern Syntax |
The XPath pattern syntax is described formally in the XPath specification. Here we're going to look at the syntax in abstract, but less formal terms:
An XPath pattern is a 'location path', a location path is absolute if it begins with a slash ("/") and relative otherwise
A relative location path consists of a series of steps, separated by slashes
A step is an axis specifier, a node test, and a predicate
The formal syntax is made more complicated by the fact that it must describe both the abbreviated and unabbreviated syntaxes
Node Tests
Node tests are most frequently element names, but other node tests are possible:
Matches <name> element nodes
Matches any element node
Matches <name> element nodes in the specified namespace
Matches any element node in the specified namespace
Matches comment nodes
Matches text nodes
Matches processing instructions
Matches processing instructions with the specified target (<?target ...?>
Matches any node
Predicates
Predicates occur after the node test in square brackets. A wide range of expressions are possible.
Matches the first node
Most node tests return nodes in document order, only the tests which select ancestor or preceding elements return nodes in reverse document order. The practical result of this is that the "first" node is almost always the one closest to the context node, although parenthesis can change the effective order.
Matches the last node
Matches even nodes
Matches the element(s) with id attributes whos value is "foo"
Matches elements that don't have an id attribute
Match <author> elements that have <firstname> children with the content "Norman".
Match "Norman" without regard to leading and trailing space.
Predicate expressions can be more-or-less arbitrarily complex.
Axis SpecifiersThe axis of a node test determines what general category of nodes may be considered for the following node test. There are thirteen axes:
Ancestors of the current node
Ancestors, including the current node
Attributes of the current node (abbreviated "@")
Children of the current node (the default axis)
Descendants of the current node
Descendants, including the current node (abbreviated "//")
Elements which occur after the current node, in document order
Elements which occur before the current node, in document order (returned in reverse-document order)
The namespace nodes of the current node
The parent of the current node (abbreviated "..")
The current node (abbreviated ".")
Now that we've seen the unabbreviated syntax, let's look at a few more patterns
Matches ancestor <table> elements
Matches the following <paramdef> siblings
Matches the sepchar attribute on the current element or any ancestor of the current element
One model for applying style is to allow the process to run recursively, driven primarily by the document. A series of templates is created, such that there is a template to match each context, then these templates are recursively applied starting at the root of the document.
<xsl:template>
<xsl:template match="section/title"> <h2><xsl:apply-templates/></h2> </xsl:template><xsl:apply-templates>
<xsl:apply-templates select="th|td"/>There are two obstacles to overcome when using the recursive model, how to arbitrate between multiple patterns that match and how to process the same nodes in different contexts.