Cover Image for XPath Predicate
107 views

XPath Predicate

In XPath, a predicate is a condition or expression used to filter nodes and select specific nodes that match the given criteria. Predicates are added to the end of an XPath expression within square brackets []. The predicate allows you to specify conditions that must be met for a particular node to be selected.

XPath predicates use various criteria, such as attributes, element values, positions, or combinations of conditions. Here are some examples of using predicates in XPath expressions:

  1. Selecting Elements with Specific Attribute Values:
//book[@category='fiction']

This XPath expression selects all <book> elements that have a “category” attribute with the value “fiction.”

  1. Selecting Elements with Multiple Criteria:
//book[@category='fiction' and @lang='en']

This XPath expression selects all <book> elements that have both a “category” attribute with the value “fiction” and a “lang” attribute with the value “en.”

  1. Selecting Elements with Specific Element Values:
//book[price>20]

This XPath expression selects all <book> elements that have a child element named “price” with a value greater than 20.

  1. Selecting Elements at Specific Positions:
//book[3]

This XPath expression selects the third <book> element in the document. The position is based on the document order.

  1. Combining Predicates:
//book[@category='fiction'][year>2000]

This XPath expression selects all <book> elements that have both a “category” attribute with the value “fiction” and a “year” child element with a value greater than 2000.

Predicates in XPath allow you to precisely target and filter nodes within an XML or HTML document, making it a powerful tool for querying and extracting specific data. They are used to refine the selection of nodes based on various conditions, making XPath expressions more flexible and capable of handling complex XML structures.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS