Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

XML Path Language (XPath)

Part:   1   2  3 

This tutorial describes:

  • What is XML Path Language (XPath)
  • Data Types, Literals and Variables
  • Evaluation Context
  • Build-in Functions
  • Expressions and Location Paths
  • Using XPath in XSL Templates

What is XML Path Language (XPath)

XPath: An expression evaluation language to produce a value that represents sub structure of an XML structure.

You can compare XPath with regular expression, which is an expression language to produce a value that represents sub structure of a text string.

You can compare XPath with the DOS path name convention, which is a simple expression language to produce a value that represents a sub structure of a file system.

Writting an XPath expression involves the following aspects:

  • Data types, literals and variables
  • Evaluation context
  • Operations
  • Build-in functions

XPath is used currently in both XSLT and XPointer.

Data Types, Literals and Variables

XPath supports 4 data types:

  • Boolean: A data type with two possible values: true and false.
  • Number: A data type representing floating-point numbers with double-precision defined by IEEE 754.
  • String: A data type representing sequences of characters from the same character used by XML.
  • Note set: A data type representing unordered collections of nodes defined by Document Object Model (DOM).

Boolean: A data type with two possible values: true and false. Operations that produce boolean type of data objects:

=     Equal to 
!=    Not equal to 
<     Less than
>     Greater than 
<=    Less than or equal to
>=    Greater than or equal to
or    Logical or
and   Logical and

Number: A data type representing floating-point numbers with double-precision defined by IEEE 754. Operations that produce numbers:

+     Addition
-     Subtraction
*     Multiplication
div   Division
mod   Remainder

String: A data type representing sequences of characters from the same character used by XML.

Note set: A data type representing unordered collections of nodes defined by Document Object Model (DOM).

Evaluation Context

XPath expressions are always evaluated with respect to a context, which consists of:

  • Context node: A node referring to the current node in the source XML structure.
  • Context position: An integer indicating position of the context node in the current node set.
  • Context size: An integer indicating the number of nodes in the current node set.
  • Variable bindings: A collection of pairs of variable names and values.

The context node is referring to the current node in the source XML structure, which is represented as a tree of different types of nodes according to the Document Object Model (DOM):

  • Root node: The top and first node of the XML structure.
  • Element node: A node that has child nodes.
  • Text nodes: A node represents a unit of text in the content of a parent node.
  • Attribute node: A node represents an attribute.
  • Namespace node: A node represents a name declaration statement.
  • Processing instruction node: A node represents a processing instruction statement.
  • Comment node: A node represents a comment statement.

For more details, please see my notes on DOM.

(Continued on next part...)

Part:   1   2  3 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XML Path Language (XPath)