Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Examples of Path Expressions
Before describing the different kinds of path expression in more detail, it may be helpful to look at some examples.
Expression | Description |
para | Selects all the |
@title | Selects all the title attributes of the context node. The result will either be empty or contain a single attribute node. |
book/author/first-name | Selects the |
para[@id] | Selects all the id attribute. |
para/@id | Selects the id attributes of all the |
/*/para | Selects all the * is a wildcard that selects all elements on the chosen axis. |
$sections/body | Selects all element children of nodes in the sequence identified by the variable $sections . A type error occurs if $sections contains an item that isn't a node. The results will be in document order even if the original sequence $sections isn't in document order. |
$sections[3]/body | Selects all element children of the third node in the sequence identified by the variable $sections . |
$sections/ . | Selects all the nodes that are present in the value of the variable $sections , but with duplicates removed, and sorted into document order. The only effect of the /. in this case is to force the reordering and deduplication. |
/contract/clause[3]/subclause[2] | Selects the second |
//figure | Selects all the elements in the document. |
city[not(@name = preceding-sibling::city/@name)] | Selects all the child name attribute that is the same as the name attribute of a preceding |
*/name() | Selects the names of the children of the context node. The path expressions given above all select nodes in a tree. However, the / operator can also be used as a simple mapping operator to compute atomic values for each node in a sequence; this example returns a sequence of strings, each being the name of a child element of the context node. I refer to this kind of expression as a simple mapping expression , and because this chapter is all about expressions that operate on nodes, I will cover simple mapping expressions in Chapter 10. |