Overview of XPath

XPath is an expression language that was designed by the World Wide Web Consortium (W3C) to allow processing of XML data that conforms to the XQuery 1.0 and XPath 2.0 data model. XQuery is a functional programming language that was designed by the World Wide Web Consortium (W3C) to meet specific requirements for querying and modifying XML data. DB2® XPath supports a subset of the language constructs in the XPath 2.0 recommendation.

The XPath language provides several kinds of expressions that can be constructed from keywords, symbols, and operands. In most cases, the operands of various expressions, operators, and functions must conform to the expected types. DB2 ignores type errors in certain situations.

DB2 XPath can be used as an argument to the XMLTABLE SQL built-in table function, which is used to convert an XML value into a relational result set.

XPath expressions

The basic building block of XPath is the expression. DB2 XPath provides several kinds of expressions for working with XML data:

  • Primary expressions, which include the basic primitives of the language, such as literals, variable references, and function calls
  • Path expressions for locating nodes within a document tree
  • Arithmetic expressions for addition, subtraction, multiplication, division, and modulus
  • Comparison expressions for comparing two values
  • Logical expressions for using boolean logic
XPath expressions can be composed with full generality, which means that where an expression is expected, any kind of expression can be used. In general, the operands of an expression are other expressions. In the following example, the operands of a logical expression are the comparison expressions 1 = 1 and 2 = 2:
1 = 1 and 2 = 2

XPath processing

An XPath expression consists of an optional prolog that establishes the processing environment and an expression that generates a result. XPath processing occurs in two phases: the static analysis phase and the dynamic evaluation phase.

During the static analysis phase, the expression is parsed and augmented based on information that is defined in the prolog. The static context is used to resolve type names, function names, and variable names that are used by the expression. The static context includes all information that is available prior to evaluating an expression. The static phase occurs when the expression is first evaluated. If a required name is not found in the static context, an error is raised.

The dynamic evaluation phase occurs if no errors are detected during the static analysis phase. During the dynamic evaluation phase, the value of the expression is computed. A dynamic type is associated with each value as the value is computed. If an operand of an expression has a dynamic type that does not match the expected type, a type error is raised. If the evaluation generates no errors, a result is returned. The dynamic context includes information that is available at the time the expression is evaluated.

The result of an XPath expression is, in general, a heterogeneous sequence of XML nodes and atomic values. More specifically, the result of an XPath expression is an instance of the XPath data model.

The XPath 2.0 and XQuery 1.0 data model

The XPath 2.0 and XQuery 1.0 data model represents an XML document as a hierarchy (tree) of nodes that represent XML elements and attributes. Each value of the data model is a sequence that can contain zero, one, or more items. The items can be atomic values or nodes. Every XPath expression takes as its input an instance of the XPath 2.0 and XQuery 1.0 data model and returns an instance of the XPath 2.0 and XQuery 1.0 data model.

DB2 XPath data types

DB2 XPath supports the following data types:
  • xs:integer
  • xs:decimal
  • xs:double
  • xs:string
  • xs:boolean
  • xs:untypedAtomic
  • xs:date
  • xs:dateTime
  • xs:time
  • xs:duration
  • xs:yearMonthDuration
  • xs:dayTimeDuration

DB2 checks data types during the dynamic evaluation phase and the static analysis phase. When an expression encounters an inappropriate type, a type error is raised. For example, an XPath expression that uses the plus operator (+) to add two strings together results in a type error because the plus operator is only used in arithmetic expression to add numeric, yearMonthDuration, and dayTimeDuration values. Implicit type conversions and type substitutions occur, when possible, to provide the type that is expected by an expression.

The built-in function library

DB2 XPath provides a library of built-in functions for working with XML data. The library includes the following types of functions:

  • String functions
  • Numeric functions
  • Date and time functions
  • Functions that operate on boolean values
  • Functions that operate on sequences

These built-in functions are in the namespace with URI http://www.w3.org/2005/xpath-functions, which by default is associated with the prefix fn. The default function namespace is set to fn by default, which means that you can call functions in this namespace without specifying a prefix.

Function calls can be used anywhere in an XPath expression where an expression is expected.