Selecting comments, processing instructions, and namespace nodes

page 46

3.2.5 Using Wildcards

XPath features three wildcards: • The asterisk , which selects all element nodes in the current context. Be aware that the asterisk wildcard selects element nodes only; attributes, text nodes, comments, or processing instructions arent included. You can also use a namespace prefix with an asterisk: in our sample sonnet, the XPath expression auth: returns all element nodes in the current context that are associated with the namespace URL http:www.authors.com . • The at-sign and asterisk , which selects all attribute nodes in the current context. You can use a namespace prefix with the attribute wildcard. In our sample sonnet, auth: returns all attribute nodes in the current context that are associated with the namespace URL http:www.authors.com . • The node node test, which selects all nodes in the current context, regardless of type. This includes elements, text, comments, processing instructions, attributes, and namespace nodes. In addition to these wildcards, XPath includes the double slash , which indicates that zero or more elements may occur between the slashes. For example, the XPath expression line selects all line elements, regardless of where they appear in the document. This is an absolute XPath expression because it begins with a slash. You can also use the double slash at any point in an XPath expression; the expression sonnetline selects all line elements that are descendants of the sonnet element at the root of the XML document. The expressions sonnetline and sonnetdescendant-or-self::line are equivalent. The double slash is a very powerful operator, but be aware that it can make your stylesheets incredibly inefficient. If we use the XPath expression line , the XSLT processor has to check every node in the document to see if there are any line elements. The more specific you can be in your XPath expressions, the less work the XSLT processor has to do, and the faster your stylesheets will execute. Thinking back to our filesystem metaphor, if I go to a Windows command prompt and type dirs c:\.xml , the operating system has to look in every subdirectory for any .xml files that might be there. However, if I type dir s c:\doug\projects\xml-docs\.xml , the operating system has far fewer places to look, and the command will execute much faster. page 47

3.2.6 Axes

To this point, weve been able to select child elements, attributes, text, comments, and processing instructions with some fairly simple XPath expressions. Obviously, we might want to select many other things, such as: • All ancestors of the context node • All descendants of the context node • All previous siblings or following siblings of the context node siblings are nodes that have the same parent To select these things, XPath provides a number of axes that let you specify various collections of nodes. There are thirteen axes in all; well discuss all of them here, even though most of them wont be particularly useful to you. To use an axis in an XPath expression, type the name of the axis, a double colon :: , and the name of the element you want to select, if any. Before we define all of the axes, though, we need to talk about XPaths unabbreviated syntax.

3.2.6.1 Unabbreviated syntax

To this point, all the XPath expressions weve looked at used the XPath abbreviated syntax. Most of the time, thats what youll use; however, most of the lesser-used axes can only be specified with the unabbreviated syntax. For example, when we wrote an XPath expression to select all of the line elements in the current context, we used the abbreviated syntax: xsl:apply-templates select=line If you really enjoy typing, you can use the unabbreviated syntax to specify that you want all of the line children of the current context: xsl:apply-templates select=child::line Well go through all of the axes now, pointing out which ones have an abbreviated syntax.

3.2.6.2 Axis roll call

The following list contains all of the axes defined by the XPath standard, with a brief description of each one. child axis Contains the children of the context node. As weve already mentioned, the XPath expression child::lineschild::line is equivalent to linesline . If an XPath expression such as sonnet doesnt have an axis specifier, the child axis is used by default. The children of the context node include all comment, element, processing instruction, and text nodes. Attribute and namespace nodes are not considered children of the context node. parent axis Contains the parent of the context node, if there is one. If the context node is the root node, the parent axis returns an empty node-set. This axis can be abbreviated with a double period .. . The expressions parent::sonnet and ..sonnet are equivalent. If the context node does not have a sonnet element as its parent, these XPath expressions return an empty node-set.