Location Path Examples

The following examples illustrate the use of location paths.

Path Description

child::node()

Select all the children of the context node, whatever their node type.

attribute::name

Select the name attribute of the context node.

attribute::*

Select all the attributes of the context node.

descendant::para

Select the <para> element descendants of the context node.

ancestor::div

Select all <div> ancestors of the context node.

ancestor-or-self::div

Select the <div> ancestors of the context node and, if the context node is a <div> element, select the context node as well.

descendant-or-self::para

Select the <para> element descendants of the context node and, if the context node is a <para> element, select the context node as well.

self::para

Select the context node if it is a <para> element; otherwise select nothing.

child::chapter/descendant::para

Select the <para> element descendants of the <chapter> element children of the context node.

child::*/child::para

Select all <para> grandchildren of the context node.

/

Select the document root (which is always the parent of the document element).

/descendant::para

Select all the <para> elements in the same document as the context node.

/descendant::olist/child::item

Select all the <item> elements that have an <olist> parent and that are in the same document as the context node.

child::para[position()=1]

Select the first <para> child of the context node.

child::para[position()=last()]

Select the last <para> child of the context node.

child::para[position()=last()-1]

Select the next-to-last <para> child of the context node.

child::para[position()&gt;1]

Select all the <para> children of the context node, except for the first <para> child of the context node.

/descendant::figure[position()=42]

Select the forty-second <figure> element in the document.

/child::doc/child::chapter[position()=5]/child::section[position()=2]

Select the second <section> element contained in the fifth <chapter> element of the <doc> document element.

child::para[attribute::type="warning"]

Select all <para> children of the context node that have a typeattribute with the value "warning".

child::para[attribute::type="warning"][position()=5]

Select the fifth <para> child of the context node that has a typeattribute with the value "warning".

child::para[position()=5][attribute::type="warning"]

Select the fifth <para> child of the context node if that child has a typeattribute with the value "warning".

child::chapter[child::title="Introduction"]

Select the <chapter> children of the context node that have one or more <title> children with string value equal to "Introduction".

child::chapter[child::title]

Select the <chapter> children of the context node that have one or more <title> children.

child::*[self::chapter or self::appendix]

Select the <chapter> and <appendix> children of the context node.

child::*[self::chapter or self::appendix][position()=last()]

Select the last <chapter> or <appendix> child of the context node.

See Also

Reference

XPath Examples