XPathXPath select elements in the source XML document. The paths are very similar to paths on a file system. Elements are separated by the / character. A single / points to the root of the document. The * , . , and .. characters behave as you would expect (all nodes, current node, and node parent). Here are some absolute paths, starting from the root of the document: / /Body/Title /Body/attribute::* /Body/Section/Para/Image The following, on the other hand, are some relative paths, starting from the current node: . Title Invoice/Date ../Code The / separator selects only direct descendants, whereas the // separator selects all descendants: //Para Body//Image To select attributes, prefix them with the @ character: Total/Amount/@Currency @xml:lang For the text content of a node, use the text() function: Para/text() Use predicates to restrict the path to elements that meet a certain condition. Predicates appear between square brackets after the element to which they apply: /Body/Para[2] Invoice/Product[@type="book"]/Code Section[Status="Draft" and Editor="PZ"] AxesXPath uses axes to control how the processor walks the tree. By default, the processor uses the child axis, but you can select any axis by prefixing the element with the axis name followed by :: . Axes are used for advanced style sheets when you need fine control over which nodes will be selected: child::Para Section/following::Footnote The most useful axes are as follows :
Note The .. , // , and @ characters are shorthand for the parent , descendant-or-self , and attribute axes. |