Understanding Patterns
A pattern is the syntax you'll use to navigate around the source tree. As you've just seen, patterns are used in the match attribute of a template rule. As you'll soon see, patterns can also be used to select nodes within the tree (such as an element or attribute) so that you can retrieve its content. Patterns are also used in XSLT to name and call template rules, iterate over a group elements of like structure, and a lot more.
Pattern syntax is a subset of a more verbose notation called location paths and is a part of a general class called XPath expressions. You'll learn all about XPath expressions in Chapter 4. The good news is that basic pattern syntax is easy to learn and will allow you to perform 80% of what you'll need to navigate around a source tree.
Pattern syntax can be likened to the command-line syntax used in most operating systems to navigate through directories. For example, assuming you are in your root directory, you can change to a directory, subdir1, by typing in the appropriate command followed by the subdirectory's name; in this case, subdir1. You can go to the root directory, no matter where you are in the directory tree, by using the slash character, just as with the root template rule. You can create paths by stringing directory names together, like so:
subdir1/subdir2/subdir3
You can specify that this last path start at the root directory by prefixing the entire path with a slash character, like so:
/subdir1/subdir2/subdir3
The period character (.) selects the current directory, whereas two periods (..) selects the parent directory. You can even use the asterisk (*) as a wildcard character to select a group of files.
If you understand this, then (with a couple of minor additions) you already know how to navigate a source tree using pattern syntax. That is, the slash character takes you to the root node, naming a node takes you to that node, two periods takes you to the parent node, and so on. You can even string together a path through the source tree such as node1/node2/node3. This last pattern is called a step pattern and each node name in the path is called a step. Table 2.1 gives a quick overview of the pattern characters you can use.
Table 2.1 Characters Used to Construct Patterns
Character |
Description |
| |
Expresses alternatives; for example, (emph|b) |
. |
Selects the current node |
/ |
Used to compose a longer pattern |
// |
Matches descendants instead of children |
.. |
Selects the parent of the current node |
* |
Wildcard character; matches with all elements |
@ |
Selects an attribute |
In looking over Table 2.1, you'll notice there's one character that hasn't been mentioned. The "at" symbol (@) is used to select an attribute. I'll look briefly at selecting attributes and provide some additional examples of patterns later in this chapter. However, Chapter 3, "XPath," provides additional details on pattern syntax.