Predicates

A predicate can be used to filter a node-set based on the boolean expression it contains.

  • When the predicate result is a boolean.

    //book[@isbn='0-596-00058-8']

    Returns all book elements with an isbn attribute of '0-596-00058-8'.

  • When the predicate result is a number, this number is being evaluated against the position() of the context element.

    //book[2] == //book[position()=2]

    Returns all the book child elements that are the 2nd book children of their parents.

  • When the predicate result is a node-set, this will be evaluated as true when the node-set is non-empty.

    //book[@isbn]

    Returns all book elements in the XML Document with an isbn attribute.

  • When the predicate result is a string, this will be evaluated as true when the length of the string is non-zero.

    //book[ string( author/text())]

    Returns all book elements in the XML Document with an author element with a text() node that has a non-zero string value.