Selecting attributes Selecting Things Besides Elements with Location Paths

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.