Syntax

  • XPath uses a similar pattern as used by Unix to identify directories and files.

  • Use '/' to specify the root of the XML Document (like it is used in Unix to identify the root of the file system).

    /bookstore/book

    Returns all elements with the name 'book' that have as root an element with the name 'bookstore'.

  • Use '.' to address the current context element (axis).

    ./book
    ./*

    When the context is '/bookstore', this returns respectively all elements with the name book and all bookstore child elements.

  • Use '//' to specify any descendant element or the context element.

    .//book

    Returns all descendant elements or the context element with the name 'book'.

  • Use the '*' wildcard to match any element (or attribute).

    /bookstore/*

    Returns all elements that have as root an element with the name 'bookstore'.

  • A context node can be defined for an XPath expression, this forces the XPath expression to be evaluated from the context node's location.

    book
    *

    When the context is '/bookstore', this returns respectively all child elements with the name book and all bookstore child elements.

  • Use '..' to address a context's parent element (axis).

    ../book
    ../*

    When the context is '/bookstore/book', this returns respectively all elements with the name book and all bookstore child elements.

  • Use '@' to specify an attribute.

    //book/@isbn
    //book/@*

    The first expression returns all isbn attributes specified on book elements, the second returns all attributes specified on book elements.

  • Use '|' to specify multiple XPath expressions.

    //book/@isbn | //book

    Return all isbn attributes specified on book elements and return all book elements.

  • Use '$' to indicate a variable.

    //book[@isbn=$number]

    Return all book elements where the 'isbn' attribute has the value of the $number variable.

    Note

    Only when the XPath implementation supports this.