xml
  1. xml-xpath-node-operators

XML XPath Node Operators

  • XPath Node Operators are used to select particular nodes in an XML document.

  • XPath Axes and Operators are powerful and flexible tools that provide developers with a way to manipulate XML data.

  • XPath Axes and Operators are categorized into four groups:

  • Axis

  • Node Test

  • Operator

  • Function

In this tutorial, you will learn about XPath Node Operators and how to use them to select specific nodes in your XML documents.

Syntax

XPath Node Operators use a variety of syntax rules to select nodes from an XML document. Below is the list of some common XPath Node Operators and their syntax:

Axes

  • ancestor:: selects all the ancestors of the current node.
  • ancestor-or-self:: selects all the ancestors of the current node including the current node.
  • attribute:: selects all the attributes of the current node.
  • child:: selects all the children of the current node.
  • descendant:: selects all the descendants of the current node.
  • descendant-or-self:: selects all the descendants of the current node including the current node.
  • following:: selects all the nodes that come after the current node.
  • following-sibling:: selects all the siblings that come after the current node.
  • namespace:: selects all the namespace nodes of the current node.
  • parent:: selects the parent of the current node.
  • preceding:: selects all the nodes that come before the current node.
  • preceding-sibling:: selects all the siblings that come before the current node.
  • self:: selects the current node.

Node Test

  • * selects any element node.
  • @* selects any attribute node.
  • node() selects any node.
  • text() selects any text node.
  • comment() selects any comment node.
  • processing-instruction() selects any processing instruction node.
  • qname() selects any qualified name.

Operator

  • and logical and
  • or logical or
  • = equal
  • != not equal
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • + addition
  • - substraction
  • * multiplication
  • / division
  • mod modulo
  • div divide as in integer division

Function

  • starts-with() checks whether a string starts with a specific set of characters.
  • contains() checks whether a string contains a specific set of characters.
  • substring() returns a portion of a string.
  • substring-after() returns the substring after a specified substring.
  • substring-before() returns the substring before a specified substring.
  • string-length() returns the length of a string.
  • normalize-space() removes extra spaces from a string.
  • translate() replaces a set of characters with another set.

Example

Let's take a look at some examples of XPath Node Operators:

Axes

  • ancestor::book selects all the ancestor elements of the book element.
  • attribute::price selects the price attribute of the current node.
  • child::author selects all the child elements of the current node that are named author.
  • descendant::chapter selects all the descendant elements of the current node that are named chapter.
  • following::book selects all the book elements that come after the current node.
  • parent::chapter selects the parent of the current node if it is a chapter element.
  • preceding-sibling::paragraph selects all the paragraph elements that come before the current node.

Node Test

  • * selects any element node.
  • @* selects any attribute node.
  • node() selects any node.
  • text() selects any text node.
  • comment() selects any comment node.
  • processing-instruction() selects any processing instruction node.
  • qname() selects any qualified name.

Operator

  • name(book) = 'book' returns true if the current node is named book.
  • @lang = 'en' returns true if the lang attribute of the current node is en.
  • chapter > 3 returns true if the value of the chapter element is greater than 3.
  • price < 10 and price > 5 returns true if the price attribute is between 5 and 10.

Function

  • starts-with(title, 'Harry Potter') returns true if the title of the current node starts with 'Harry Potter'.
  • contains(title, 'Potter') returns true if the title of the current node contains the string 'Potter'.
  • substring(title, 1, 7) returns the characters from position 1 to 7 of the title of the current node.
  • substring-after(title, 'Harry ') returns the substring after 'Harry ' in the title of the current node.
  • substring-before(title, ':') returns the substring before ':' in the title of the current node.
  • string-length(title) returns the length of the title of the current node.
  • normalize-space(title) removes extra spaces from the title of the current node.
  • translate(title, 'abc', 'xyz') replaces 'a' with 'x', 'b' with 'y', and 'c' with 'z' in the title of the current node.

Output

The output of XPath Node Operators depends on the query used. The following are some examples of output:

  • ancestor::book selects all the ancestor elements of the book element.
  • attribute::price selects the price attribute of the current node.
  • child::author selects all the child elements of the current node that are named author.
  • descendant::chapter selects all the descendant elements of the current node that are named chapter.
  • following::book selects all the book elements that come after the current node.
  • parent::chapter selects the parent of the current node if it is a chapter element.
  • preceding-sibling::paragraph selects all the paragraph elements that come before the current node.

Explanation

XPath Node Operators provide developers with a powerful and flexible way to select nodes from an XML document. They can be used to select specific elements, attributes, and text nodes based on their position in the document tree.

The Axis syntax is used to determine the direction of the search. The Node Test syntax is used to specify what type of nodes should be included in the search. The Operator and Function syntax is used to test against the values of the nodes selected by the other syntaxes.

Use

XPath Node Operators are used in a variety of applications, including web development, data processing, and document management. They are particularly useful for selecting specific nodes in large XML documents, allowing developers to access only the data they need.

Important Points

  • XPath Node Operators are used to select specific nodes in an XML document.
  • Axes, Node Test, Operator, and Function are the four categories of XPath Node Operators.
  • Developers can use XPath Node Operators to filter XML data and access only the data they need.
  • XPath Node Operators provide powerful and flexible tools for manipulating XML data.

Summary

XPath Node Operators provide developers with a powerful and flexible way to select nodes from an XML document. They are used to filter XML data and access only the data that is needed. XPath Node Operators are categorized into Axes, Node Test, Operator, and Function. Developers can use these tools to manipulate XML data and make it more useful for their applications.

Published on: