xpath

Topics related to xpath:

Getting started with xpath

Finding elements containing specific text

Namespaces

XPath 1.0 doesn't have the concept of a default namespace.

Also, the namespace prefixes defined in the original XML document do not affect XPath - namespace prefixes have to be explicitly registered with the XPath provider, otherwise prefixes can't be used at all in the XPath expression.

Select nodes with names equal to or containing some string

local-name() result does not include prefix (lookup name() XPATH function for it)

Find nodes that have a specific attribute

Using [@attribute_name] we can select nodes that have the attribute irrespective of the value.

We can use any of the functions or combination of the functions such as starts-with and lowercase, for example, with this selector to suit our needs.

Get the count of nodes

We can use this in combination of other functions and axes to suit our needs.

Location paths and axes

An XPath location path is a series of location steps separated by a / character:

step1/step2/step3

A location step contains an axis, a node test, and an optional list of predicates. The axis and the node test are separated by two colon characters ::. The predicates are enclosed in square brackets:

axis::nodeTest[predicate1][predicate2]

The evaluation of a location path starts with a node set containing the context node given by the context of the expression, or the root node, if the location path starts with a /. At each step, each node N in the original node set is replaced with the set of nodes that

  • can be reached from N following the given axis,
  • matches the node test, and
  • matches all predicates.

The result of a location path expression is the final node set obtained after processing all location steps.

Finding elements containing specific attributes

Get nodes relative to the current node

These axes can be used in combination with other functions to suit our needs.

Select nodes based on their children

Check if a node is present

Boolean function has other uses

  1. Check if a string is empty
  2. Check if the argument is not a number (NaN) or is 0

Check if a node's text is empty

Boolean function has other uses

  1. Check if a node is present
  2. Check if the argument is not a number (NaN) or is 0

String function is used to return the string value of a node.