MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

XPath

The XML Path Language (XPath) supplies a mechanism to address particular nodes or sets of nodes in an XML document. XPath expressions can be used to write precise expressions to select nodes without using procedural DOM statements. For example, we can address particular nodes using expressions like:

uct/title uct/version/number uct/author/@office

XPath Syntax

  • Expressions are separated by "/".

  • In general, each subexpression matches one or more nodes in the DOM tree.

  • Each sub-expression has the form: axis::node[condition1][condition2]... where axis can be used to select children, parents, descendants, siblings, and so on.

  • Symbols may be used for the possible axes:

    ExpressionWhat it selects in current context
    title"title" children
    *All children
    @office"office" attribute
    author[1] First author node
    /uct/title[last()] Last title within uct node at top level of document
    //authorAll author nodes that are descendant from top level
    .Context node
    ..Parent node
    version[number] Version nodes that have "number" children
    version[number='1.0']Version nodes for which "number" has content of "1.0"