Getting lots of questions about XPath on the #XML IRC channel so figured I'd write a quick cheat sheet. Sometimes the hardest part of learning something is knowing what it's called.
There are 7 Types of Nodes:
- Root Node
/
- Element Node
- Text Node text
()
- Attribute Node
@
- Comment Node
comment()
- Processing Instruction
processing-instruction()
- Namespace Node
There are 13 AXIS Specifiers from your current node:
-
ancestor::
-
ancestor-or-self::
-
attribute::(or @)
-
child::(or nothing)
-
descendant::
-
descendant-or-self::
-
following::
-
following-sibling::
-
namespace::
-
parent::or (or ..)
-
preceding::
-
preceding-sibling::
-
self:: or (.)
Note that the double colon "::" immediately follows the use of an axis name.
Predicates:
Predicates are used to qualify expressions (i.e filter nodes). Predicates are specifiable on any node set and appear in sqare brackets after the node test.
Example:
version[3] |
Functions:
-
last()
- When processing a list of nodes, if the nodes are numbered from one, last() returns the last number.
-
position()
- returns the value of the context position
-
name()
- returns the name of a node. Good for getting things like the element name for tests.
-
concat()
- take two or more arguments and joins the results together end-to-end.
-
count()
- returns the number of nodes present in the node-set
-
not()
- returns the negation of its argument.
Examples:
Actually reading the XPath Expressions is probably the easiest way to learn them. Keep in mind that from any position in the document you can write an XPath expression to get to any other node in that document.
para |
matches all para children in the current context |
para/emphasis |
matches all emphasis elements that have a parent of para |
//title |
matches all title elements anywhere in the document |
.//title |
matches all title elements that are descendants of the current context |
item[1] |
matches the "first" item |
item[position()=last()] |
matches the last item (in a given list) |
topic[@level='basic'] |
matches topic elements with level attributes whose value is basic |
//chapter[2]/title |
The title of the second chapter |
preceding::partNumber |
all preceding partNumber elements |