XPATH

CPAN 330 XML
Lecture #9:XPATH
XPath is a language that is used to address sections of an XML document, and
one of the main advantages of this language is to be used with XSLT to pull
information from the source tree. XPath uses path expressions to identify nodes
in an XML document. These path expressions are called locations path. The
location path looks like the computer file system path expressions and enable
us to extract the exact information from the XML documents.
To move from one point to another in the XML document, Xpath expressions
give directions step-by-step. Each step is separated from the other by \. XPath
uses parent/child relationship on any XML document it searches for matching
nodes based on the template pattern.
The location path can be absolute or relative, simple, or complex. The absolute
path starts from the document root, whereas the relative path starts from the
context node.
Xpath expressions are written in Xpath own syntax, and not XML syntax. If these
expressions were written using XML syntax, we would not be able to use them as
values of XSLT attributes.
XPath uses square brackets to filter expressions and patterns. These are usually
called predicates. For example the following location path selects all the
customer elements that are child elements of the context node, and have child
element called salesRepresentative:
customer[salesRepresentative]
We can be more specific and select all the customer elements that have John
as Sales representative:
customer[salesRepresentative=’John’]
You can build a complex location path expression that is very specific. For
example:
Customer[@id=’no1’[/order[item=’shelves’]
The expression wills select all the order elements that have a child element
called item with a value shelves , and are child element of Customer element
that have the attribute id with a value ‘no1’
1
Pattern Operators
XPath supports the following pattern operators:
Operator
@
*
Description
Select an attribute within a given element.
Select all the child elements in the specific location path.
//
/
|
This operator is called the Recursive Descend Operator
Specify the relationship of a node to its parent or child node.
This operator is called the union operator, which allows us to
perform either/or search.
Filter expressions and patterns.
[]
Xpath Expression Operators
Expression Operators are mostly used with predicators and functions. XPath
supports the following set of operators:
Operator
+
*
Div
Mod
=
!=
>
>=
< (&lt; when used in an attribute value)
<=(&lt;= when used in an attribute value)
Or
And
Description
Numeric addition
Numeric subtraction
Numeric multiplication
Numeric division
The remainder of two divided
numbers ( modular)
Equal to
Not equal to
Greater than
Greater than or equal
Less than
Less than or equal
Logical and
Logical or
The following are some examples of location path:
Location path
/inventory
/inventory/item
/inventory/item[quantity >5]
Description
Select the root element inventory.
Select al the item elements that are children of
inventory element that is a child element of
the document root.
Select all the item elements that have child
2
element quantity with value greater that 5.
These item elements should be children of the
inventory element, that is a child element of
the document root.
//item
Select all the item elements in the document.
//@number
Select any attribute called number
/inventory/*
Select all the child elements of inventory
element, that is a child element of the
document root.
/inventory/item[last()]
Select the last item element that is a child
element of root element inventory.
//item|//price
Select all the item and price elements in the
document.
Students/student[@id=’no1’] Select all the student elements that have id
attribute with value “no1”. The student
element must be also a child element of the
root element Students
XPath Functions
XPath defines a library of standard functions for working with nodes, nodes-set,
strings, numbers and Boolean expressions. These functions are inherited by
XSLT. Following is a summary of the XPath functions. These functions are also
inherited by XSLT:

Node Functions
Name
count()
id()
last()
Description
Returns the number of nodes in a node-set
Selects elements by their unique ID
Returns the position number of the last node in the processed
node list
local-name() Returns the local part of a node. A node usually consists of a
prefix, a colon, followed by the local name
name()
Returns the name of a node
namespace- Returns the namespace URI of a specified node
uri()
position()
Returns the position in the node list of the node that is currently
being processed

String Functions:
3
Name
concat()
contains()
Description
Returns the concatenation of all its arguments
Returns true if the second string is contained within the first
string, otherwise it returns false
normalize-space() Removes leading and trailing spaces from a string
starts-with()
Returns true if the first string starts with the second string,
otherwise it returns false
string()
Converts the value argument to a string
string-length()
Returns the number of characters in a string
substring()
Returns a part of the string in the string argument
substring-after()
Returns the part of the string in the string argument that
occurs after the substring in the substr argument
substring-before() Returns the part of the string in the string argument that
occurs before the substring in the substr argument
translate()
Takes the value argument and replaces all occurrences of
string1 with string2 and returns the modified string

Number Functions
Name
ceiling()
Description
Returns the smallest integer that is not less than the number
argument
floor()
Returns the largest integer that is not greater than the number
argument
number() Converts the value argument to a number
round()
Rounds the number argument to the nearest integer
sum()
Returns the total value of a set of numeric values in a node-set

Boolean Functions
Name
boolean()
false()
lang()
not()
true()
Description
Converts the value argument to Boolean and returns true or false
Returns false
Returns true if the language argument matches the language of the
the xsl:lang element, otherwise it returns false
Returns true if the condition argument is false, and false if the
condition argument is true
Returns true
4
XPath Axis Names
There are different directions to move through an XML documents beside the
parent/child direction. XPath defines 13 axes that are used to move to different
directions. They are used by specifying the axis name, followed by :: , followed
by a node name. These axes are summarized in the following table:
Axis Name
ancestor
ancestor-or-self
attribute
child
descendant
descendant-or-self
following
following-sibling
namespace
parent
preceding
preceding-sibling
self
Description
Refers to the parent of the context node, or the parent’s
parent.
opposite of descendant-or-self.
Refers to the attribute of the context node.
Refers to the children of the context node (the default)
Refer to any children, or children of children of the current
node.
Refers to the descendant of the context node including
itself. // is a shortcut for this axis.
Refer to the following sibling of the context node including
their descendant ( but not including the descendant of the
context node).
Refers to the following sibling elements of the context
node.
Refers to a namespace node of the context node
Refers to the parent of the context node
Refers to the preceding sibling of the context node
including their descendant but not including the
descendant of the context node).
Refers to the preceding sibling elements of the context
node.
Refers to the context node. We can use a period as a
shortcut for self::node()
Examples
Ex1: compA.xml
This example will demonstrate how to transform XML document into another
XML document with different structure. Assume that compA wants to share
order information with compB using XML. compA has customer order
document with the following structure:
5
The other company has the order document in the following structure:
First we have to associate the new XSLT document (compA.xsl) with
compA.xml.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="compA.xsl" ?>
<order>
6
<number> 1234</number>
<salesperson>John Mark</salesperson>
<item>First-class</item>
<quantity>16</quantity>
<date>
<month>1</month>
<day>13</day>
<year>2000</year>
</date>
<customer>Sally Simpson </customer>
</order>
Then we will write the XSLT document that performs the required transformation.
The file compA.xsl has the following content:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="order">
<xsl:attribute name="number">
<xsl:value-of select="/order/number"/>
</xsl:attribute>
<date>
<xsl:value-of select="/order/date/month"/>/<xsl:value-of
select="/order/date/day"/>/<xsl:value-of select="/order/date/year"/>
</date>
<customer>
<xsl:attribute name ="name">
<xsl:value-of select="/order/customer"/>
</xsl:attribute>
<xsl:attribute name ="representative">
<xsl:text> Company A </xsl:text>
</xsl:attribute>
</customer>
<item>
<xsl:apply-templates select="/order/item"/>
<quantity><xsl:value-of select="/order/quantity"/></quantity>
</item>
</xsl:element>
</xsl:template>
<xsl:template match="item">
7
<part-number>
<xsl:choose>
<xsl:when test=". ='First-class'">E16-25A</xsl:when>
<xsl:when test=". ='second-class'">E16-25b</xsl:when>
<xsl:otherwise>00</xsl:otherwise>
</xsl:choose>
</part-number>
<description><xsl:value-of select="."/></description>
</xsl:template>
</xsl:stylesheet>
To following is a screenshot of the output displayed using Architag XRay XML
editor :
Ex2: compA1.xml
This example is the same as Ex1, but we are using explicitly the axes names of
Xpath within compA1.xsl. The output of this example should be similar to the
output of Ex1.
8
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="compA1.xsl" ?>
<order>
<number> 1234</number>
<salesperson>John Mark</salesperson>
<item>First-class</item>
<quantity>16</quantity>
<date>
<month>1</month>
<day>13</day>
<year>2000</year>
</date>
<customer>Sally Simpson </customer>
</order>
The XSLT file compA1.xsl has the following content:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="order">
<xsl:attribute name="number">
<xsl:value-of select="order/child::number"/>
</xsl:attribute>
<xsl:element name="date">
<xsl:for-each select="order/child::date/*">
<xsl:value-of select="self::node()"/>
<xsl:if test="position() !=last() " >
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:element>
<xsl:element name="customer">
<xsl:attribute name ="name">
<xsl:value-of select="//date/following-sibling::customer"/>
</xsl:attribute>
<xsl:attribute name ="representative">
<xsl:text> Company A </xsl:text>
</xsl:attribute>
9
</xsl:element>
<item>
<xsl:apply-templates select="order/child::item"/>
<quantity><xsl:value-of select="order/child::quantity"/></quantity>
</item>
</xsl:element>
</xsl:template>
<xsl:template match="order/item">
<xsl:element name="part-number">
<xsl:choose>
<xsl:when test="self::node() ='First class'">E16-25A</xsl:when>
<xsl:when test="self::node() ='Second class'">E16-25b</xsl:when>
<xsl:otherwise>00</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:element name="description"><xsl:value-of select="self::node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Ex3:XpathFun_Axes.xml
This example will show you how to use some of the Xpath functions and Axes
names.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XpathFun_Axes.xsl" ?>
<!-- invoice document -->
<invoice number="123" >
<customer-number> 54634</customer-number>
<salesperson id="2121">John Mark</salesperson>
<items>
<!-- items -->
<item>books</item>
<quantity>16</quantity>
<item>blank papers</item>
<quantity>16</quantity>
</items>
<!-- invoice date -->
<invoice-date>
<month>1</month>
<day>13</day>
<year>2000</year>
10
</invoice-date>
</invoice>
The XSLT file XpathFun_Axes.xsl has the following content:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
Th following will select the processing instruction of the context node (/)
<br />
<xsl:value-of select="processing-instruction() "/>
<br />
Th following will select the comment of the context node (/)
<br />
<xsl:value-of select="comment() "/>
<br />
Th following will select the comment of the invoice element
<br />
<xsl:value-of select="invoice/comment() "/>
<br />
The following will select all the nodes' names of all the child lements of
invoice
<br />
<xsl:for-each select="invoice/*">
<br />
<xsl:value-of select="name() "/>
</xsl:for-each>
<br />
The following will select all the attributes of invoice
<br />
<xsl:value-of select="invoice/attribute::*" />
<br />
The following will select all the descandant of the invoice , including itself
<br />
<xsl:value-of select="invoice/descendant-or-self::*" />
<br />
The follownig will select the ancestor of quantity <br/>
<xsl:value-of select="//ancestor::quantity" />
<br />
the following will select the child of items <br />
<xsl:value-of select="invoice/items/child::item"/>
<br />
The following will select invoice-date that is the following sibling of items,
11
that is a child of invoice.
<br />
<xsl:value-of select="invoice/items/following-sibling::invoice-date" />
<br />
The following is the sum of the ordered items
<br />
<xsl:value-of select="sum(invoice/items/quantity) "/>
</xsl:template>
</xsl:stylesheet>
Following is a screen shot of the output displayed in Internet Explorer:
12
13