Template Content

   

XPath

XPath 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"] 

Axes

XPath 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 :

  • following and preceding ”Contain the nodes that are after or before the current node, excluding descendants, attributes, and namespace nodes.

  • following-sibling and preceding-sibling ”Contain the following or preceding siblings of the current node.

  • attribute ”Contains the attributes of the current node.

  • namespace ”Contains the namespace nodes of the context node.

  • self ”Contains the context node itself.

  • descendant-or-self and ancestor -or-self ”Contain the current node and its descendant or ancestor nodes.

Note

The .. , // , and @ characters are shorthand for the parent , descendant-or-self , and attribute axes.


   


Applied XML Solutions
Applied XML Solutions
ISBN: 0672320541
EAN: 2147483647
Year: 1999
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net