display | more...

Previous: XPath Functions - XPath


The following examples show how to apply an axis, node tests, and predicates.

The examples contained here refer to this document fragment:

<document>
  <chapter name="XPath">
    <para>This chapter introduces XPath, a language used to specify
          parts of an XML document...</para>

    <para align="center">This paragraph is centered for no real
          reason.</para>

    <para>This paragraph's alignment is not specified.</para>

    <para>Lots of XML examples seem to use the para tag.</para>
  </chapter>

  <chapter name="XSLT">
    <para>I was confused when I kept seeing this para show up in all
          the XPath and XSLT examples.</para>

    <para>I though that para was some keyword used in XPath.</para>

    <para align="center">It is not. </para>

  </chapter>

  <appendix>

    <para>This is a paragraph in the appendix.</para>

  </appendix>
</document>
  • / - This matches the root of the document, which contains as it's only child node the document element. This test does not match the document element itself.
  • para - This node test matches the child para elements of the context node. Notice that the axis is not specified; the child axis is the default. The optional predicate is omitted.
  • para[1] - Matches the first para element that is a child of the context node. The predicate is in this case a simple integer which specifies the node to retrieve from the node-set returned by the node test (refer to the previous example).
  • para[position()=1] - Same as the previous example, except the position() call is used to determine where each node matched by the node test is in the set. The previous example is actually a shortcut that is the same as this example.
  • para[attribute::align] - This matches all of the para elements that are children of the context node and that have an align attribute.
  • para[@align] - This is the same as the previous example; the "@" shortcut is used to refer to the attribute axis.
  • para[@align="center"] - Same as the previous example, except only child para elements which have an align attribute equal to "center" are matched.
  • para[@align="center"][1] - Same as the previous example, except only the first node is matched.
  • descendant::para - Matches all of the para elements which are descended from the context node. Recall that the descendant axis returns the child nodes, the grandchildren, the great-grandchildren, and so on.
  • .//para - Same as the previous example, using the shortcut for the descendant-or-self axis.
  • /descendant::para - Matched all of the para elements in the entire document.
  • //para - Matches all para elements in the entire document, same as above, but using the abbreviation.
  • child::chapter - Matched the chapter elements that are children of the context node.
  • child::appendix - Matches the appendix elements that are children of the context node.
  • child::*[self::chapter or self::appendix] - Matches all of the chapter and appendix elements that are children of the context node. Note that the * abbreviation is used to refer to all of the nodes in the child axis and that the self axis is used within the predicates to match only the node on which the predicate is being evaluated.
  • .. - Matches the parent node, using the shortcut for parent::node().
  • ../@align - Matches the align attribute of the parent node.
  • /chapter/para[@align="center"][1] - Matches the first para element that is a child of a chapter element that is in turn a child of the root node. This XPath expression also requires that the para element match has an align attribute that is equal to "center".
  • chapter[@name="XPath"]/para[2] - Matches the second para element in the chapter element having a name attribute that is equal to "XPath".

Log in or register to write something here or to contact authors.