What is Xpath?

1 min

Xpath is a powerful query language for selecting nodes in an HTML document. Learn about the key features and aspects of Xpath.

What is Xpath?

XPath, short for XML Path Language, is a query language designed for selecting nodes from an XML(HTML) document. In addition to its primary function, XPath can also be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML/HTML document. It is a crucial part of technologies like XSLT, XQuery, and XPointer.

Here are some key features and aspects of XPath:

  1. Path Expressions: XPath uses path expressions to navigate through elements and attributes in an XML document. These expressions are similar to file paths in a filesystem.
  • / selects from the root node.
  • // selects nodes in the document from the current node that match the selection, no matter where they are.
  • . selects the current node.
  • .. selects the parent of the current node.
  • @ selects attributes.
  1. Node Selection: XPath defines several types of nodes, including element nodes, attribute nodes, text nodes, and more. Path expressions can be used to select these nodes.
  • Example: /bookstore/book selects all book elements under the bookstore element.
  1. Predicates: Predicates are used to find a specific node or a node that contains a specific value. They are enclosed in square brackets.
  • Example: //book[price>35.00] selects all book elements with a price element greater than 35.00.
  1. Functions: XPath includes over 100 built-in functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, and more.
  • Example: contains(@lang, 'en') checks if the lang attribute contains the string ‘en’.
  1. Axes: Axes define a node-set relative to the current node. There are many axes available, such as child, parent, ancestor, descendant, following, and more.
  • Example: child::book selects all book children of the current node.
  1. Operators: XPath supports standard arithmetic operators, Boolean operators, and comparison operators.

XPath is widely used in various applications such as:

Extracting data from XML/HTML documents. Navigating through an XML/HTML document’s structure. Transforming XML/HTMLL documents using XSLT. Defining parts of an XML/HTML document to be processed by XQuery.