[XML] XPath – Patterns/ Location Path

XSLT Processors traverse a tree-like representation of an XML Document. Each of the document components is represented by a node in the tree.


1. XPath Patterns

A pattern specifies the node’s name and ancestry. It is a limited form of XPath location path to match XSLT templates to nodes in the source tree.

  • Steps: A Pattern can be a one node name. But many patterns consist of a number of steps. Each step is separated using the ‘/’ symbol.
  • Root Identifier: The ‘/’ sym bol has a significant role when it is placed before the first step. This implies that element named in the first step is a child of the root node.
  • Attribute patterns: A pattern can be used to identify an attribute instead of an element. When the last step is the attribute, the prefix ‘@’ is needed.
  • Intermediate step wildcard: The ‘*’ symbol may be used to represent the name of any element in the document.
  • The ‘//’ symbol indicates any number of intermediate elements of any kinds.
  • Using patterns in XSLT: In XSLT, patterns are used in the match attribute of the key and template elements. Using ‘|’ symbol, you can use multiple patterns.
<template match="title"> <!-- Name -->
<template match="/book/title"> <!-- Root -->
<template match="book//title"> <!-- intermediate step wildcard -->
<template match="title|author"> <!-- Multiple -->
<template match="book/@version"> <!-- Attribute -->


2. Location Path

Using patterns, it is possible to take steps only downward into the structure of the document. Using a Location Path, it is also possible to begin a journey from any location in the document and arrive at a different destination.

Patterns is defined in XSLT standard and Location Paths are defined in the XPath standard.


3. Context Node

A context node is the node at which the processor is currently processing.

  • absolute path : starts from the top of the document(/) and arrives to the destination
  • relative path : is always evaluated relative to the context node
<template match="/book/author/first-name"> <!-- absoulte -->
<template match="author/first-name"> <!-- relative(if you are at the book element now.) -->


4. Axes

Axes specify the relationship between the context node and the nodes that each step selects. There are 13 axes in XPath 1.0 Recommendation.

  • self:: (forward): contains just the context node itself
  • attribute:: (forward): contains the attributes of the context node
  • child:: (forward): contains the children of the context node
  • descendant:: (forward): contains the descendants of the context node
  • descendant-or-self:: (forward): contains the context node and the descendants of the context node
  • parent:: (reverse): axis contains the parent of the context node, if there is one
  • ancestor:: (reverse): contains the ancestors of the context node
  • ancestor-or-self:: (reverse): contains the context node and the ancestors of the context node
  • following-sibling:: (forward): contains all the following siblings of the context node
  • preceding-sibling:: (reverse): contains all the preceding siblings of the context node
  • following:: (forward): contains all nodes in the same document that follow the context node in document order, but not descendant, namespace, and  attribute nodes
  • preceding:: (reverse): contains all nodes in the same document that precede the context node in document order, but not ancestor, namespace, and attribute nodes
  • namespace (forward): contains the namespace nodes of the context node


5. Examples for Axes

The following XML file and DTD are being used to explain how the XPath Axes work.

  • DTD File : mybook.dtd
  • XML File : mybook.xml
< !ELEMENT book (head, content*) >
< !ELEMENT head (title, author, date) >
< !ELEMENT title (#PCDATA) >
< !ELEMENT author (#PCDATA) >
< !ATTLIST author gender (male|female) #REQUIRED >
< !ELEMENT date (mm,dd,yy) >
<?xml version="1.0" standalone="no"?>
<!DOCTYPE book SYSTEM "mybook.dtd">
    <title>Lord of the Rings</title>
    <author gender="male">J.R.R. Tolkien</author>

(1) self:: , attribute::

<xsl:template match="author">
  author = <xsl:value-of select="self::node()" />
  gender = <xsl:value-of select="attribute::gender" />
author = J.R.R. Tolkien
gender = male

(2) child:: , descendant:: descendant-or-self::

<xsl:template match="date">
  count1 = <xsl:value-of select="count(child::node())" />
  count2 = <xsl:value-of select="count(descendant::node())" />
  count3 = <xsl:value-of select="count(descendant-or-self::node())" />
  day = <xsl:value-of select="child::dd" />
  month = <xsl:value-of select="descendant::mm" />
  year = yy" />
count1 = 7
count2 = 10
count3 = 11
day = 20
month = 03
year = 2012

(3) parent:: , ancestor:: ancestor-or-self::

<xsl:template match="date">
  count1 = <xsl:value-of select="count(ancestor::node())" />
  count2 = <xsl:value-of select="count(ancestor-or-self::node())" />
  title = <xsl:value-of select="parent::node()/title" />
  author = <xsl:value-of select="ancestor::head/author" />
  year = <xsl:value-of select="ancestor-or-self::date/yy" />
count1 = 3
count2 = 4
title = Lord of the Rings
author = J.R.R. Tolkien
year = 2012

(4) following-sibling:: , preceding-sibling::

<xsl:template match="date">
  count1 = <xsl:value-of select="count(following-sibling::node())" />
  count2 = <xsl:value-of select="count(preceding-sibling::node())" />
  year = <xsl:value-of select="following-sibling::yy" />
  month = <xsl:value-of select="preceding-sibling::mm" />
count1 = 3
count2 = 3
year = 2012
month = 03


6. Principal Node Types

Every axis has a principal node type:

  • attribute axis : the principal node type is an attribute 
  • namespace axis : the principal node type is a namespace 
  • other axes : the principal node type is an element


7. Node Types

There are seven kinds of nodes in XPath :

  • Root Node
  • Element Node
  • Attribute Node
  • Text Node
  • Comment Node
  • Processing Instruction Node
  • Namespace Node


8. Node Tests

Node Tests are NOT functions.

  • Element: “name
  • Attribute: attribute:: | @
  • Comment: comment() 
  • PI: processing-instruction() 
  • Text node: text() 
  • General Node: node() 
  • Any Node: *


9. Predicates

Using test expressions, you can select particular elements and attributes base on their values.

The test is placed in square brackets ([ ]) after the location path. The square brackets and the test inside them are known as a predicate.

<xsl:template match="head">
  count1 = <xsl:value-of select="count(child::author[@gender='male'])" />
  count2 = <xsl:value-of select="count(child::author[@gender='female'])" />


count1 = 1
count2 = 0


10. Location Step Patterns

Each step pattern is actually made up of three parts.

  • axis : specifies the tree relationship between the nodes selected by the location step and the context node
  • node test : specifies the node type and expanded-name of the nodes selected by the location step
  • predicates : use arbitrary expressions to further refine the set of nodes selected by the location step.
axis::node test[predicate]


11. Axis in Step Patterns

The axis is separated from the node test with a double-colon(::). In patterns, the only kinds of axes you can have are the child:: axis and the attribute:: axis.

If you don’t specify an axis, then it’s the same as specifying the child:: axis. And you can use @ instead of attribute:: . So it’s rare to actually specify axes in patterns.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s