Location Paths

Contact Us or call 1-877-932-8228
Location Paths

Location Paths

Location paths are used to point to and select portions of an XML document. The syntax of a location path is shown below.

Syntax

axis::node_test[predicate]

The table below explains the parts of the location path.

Location Path Parts
Term Definition
axis Indicates the relationship between the selected node and the context node.
node test Provides the name or class of the nodes to reference.
predicate Further filters the nodeset.

We will examine each part of the location path soon, but first let's look at some examples, some of which have been taken as is from the W3C documentation; others have been slightly modified.

  1. child::firstname selects the firstname element children of the context node
  2. child::* selects all element children of the context node
  3. child::text() selects all text node children of the context node
  4. child::node() selects all the children of the context node, whatever their node type
  5. attribute::name selects the name attribute of the context node
  6. attribute::* selects all the attributes of the context node
  7. descendant::firstname selects the firstname element descendants of the context node
  8. ancestor::name selects all name ancestors of the context node
  9. ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the context node as well
  10. descendant-or-self::para selects the para element descendants of the context node and, if the context node is a para element, the context node as well
  11. self::para selects the context node if it is a para element, and otherwise selects nothing
  12. child::chapter/descendant::para selects the para element descendants of the chapter element children of the context node
  13. child::*/child::para selects all para grandchildren of the context node
  14. / selects the document root (which is always the parent of the document element)
  15. /descendant::para selects all the para elements in the same document as the context node
  16. /descendant::olist/child::item selects all the item elements that have an olist parent and that are in the same document as the context node
  17. child::para[position()=1] selects the first para child of the context node
  18. child::para[position()=last()] selects the last para child of the context node
  19. child::para[position()=last()-1] selects the last but one para child of the context node
  20. child::para[position()>1] selects all the para children of the context node other than the first para child of the context node
  21. following-sibling::chapter[position()=1] selects the next chapter sibling of the context node
  22. preceding-sibling::chapter[position()=1] selects the previous chapter sibling of the context node
  23. /descendant::figure[position()=42] selects the forty-second figure element in the document
  24. /child::doc/child::chapter[position()=5]/child::section[position()=2] selects the second section of the fifth chapter of the doc document element
  25. child::para[attribute::type="warning"] selects all para children of the context node that have a type attribute with value warning
  26. child::para[attribute::type='warning'][position()=5] selects the fifth para child of the context node that has a type attribute with value warning
  27. child::para[position()=5][attribute::type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
  28. child::chapter[child::title='Introduction'] selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
  29. child::chapter[child::title] selects the chapter children of the context node that have one or more title children
  30. child::*[self::chapter and self::appendix] selects the chapter and appendix children of the context node
  31. child::*[self::chapter or self::appendix][position()=last()] selects the last chapter or appendix child of the context node

Location paths are either relative (i.e., starting with the context node) or absolute (i.e., starting with the root node of the XML document. A location path is divided in location steps. Each step consists of an axis followed by a node test. The node test may have predicates, which are used to further specify the node to be accessed. In the next sections, we will examine the different portions of these location steps.

Next