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
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 are most frequently element names, but other node tests are
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 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
nodetest[position() mod 2 = 0]
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
Match "Norman" without regard to leading and trailing space.
Predicate expressions can be more-or-less arbitrarily complex.
The 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 "//")
following / following-sibling
Elements which occur after the current node, in document order
preceding / preceding-sibling
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 ".")
A Few More Patterns
Now that we've seen the unabbreviated syntax, let's look at a few more
Matches ancestor <table> elements
Matches the following <paramdef> siblings
Matches the sepchar attribute on the current element or any ancestor
of the current element
Applying Style Recursively
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.