SlideShare a Scribd company logo
XPath
i
AbouttheTutorial
XPath is a query language that is used for traversing through an XML document. It is used
commonly to search particular elements or attributes with matching patterns.
This tutorial explains the basics of XPath. It contains chapters discussing all the basic
components of XPath with suitable examples.
Audience
This tutorials has been designed for beginners to help them understand the basic concepts
related to XPath. This tutorial will give you enough understanding on XPath from where
you can take yourself to higher levels of expertise.
Prerequisites
Before proceeding with this tutorial, you should have basic knowledge of XML, HTML, and
JavaScript.
Disclaimer&Copyright
 Copyright 2016 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com.
XPath
ii
TableofContents
About the Tutorial....................................................................................................................................i
Audience..................................................................................................................................................i
Prerequisites............................................................................................................................................i
Disclaimer & Copyright.............................................................................................................................i
1. XPATH – OVERVIEW................................................................................................................ 1
XSL ..........................................................................................................................................................1
Need for XSL............................................................................................................................................1
What is XPath..........................................................................................................................................1
Features..................................................................................................................................................2
2. XPATH — EXPRESSION........................................................................................................... 3
Verify the output.....................................................................................................................................6
3. XPATH — NODES...................................................................................................................... 7
XPath Root Node.....................................................................................................................................7
Verify the output...................................................................................................................................10
XPath Element Node .............................................................................................................................10
Verify the output...................................................................................................................................14
XPath Text Node ...................................................................................................................................14
Verify the output...................................................................................................................................17
XPath Attribute Node............................................................................................................................18
Verify the output...................................................................................................................................20
XPath Comment Node...........................................................................................................................21
Verify the output...................................................................................................................................23
XPath
iii
4. XPATH — ABSOLUTE PATH................................................................................................24
Verify the output...................................................................................................................................26
5. XPATH — RELATIVE PATH .................................................................................................27
Verify the output...................................................................................................................................29
6. XPATH — AXES........................................................................................................................30
Verify the output...................................................................................................................................32
7. XPATH — OPERATORS.........................................................................................................33
XPath Comparison Operators................................................................................................................33
Verify the output...................................................................................................................................36
XPath Boolean Operators......................................................................................................................36
Verify the output...................................................................................................................................38
XPath Number Operators / Functions ...................................................................................................39
Verify the output...................................................................................................................................42
XPath String Functions ..........................................................................................................................42
Verify the output...................................................................................................................................45
XPath Node Functions...........................................................................................................................45
Verify the output...................................................................................................................................47
8. XPATH — WILDCARD ...........................................................................................................49
Verify the output...................................................................................................................................52
9. XPATH — PREDICATE...........................................................................................................53
Verify the output...................................................................................................................................56
XPath
1
Before learning XPath, we should first understand XSL which stands for
Extensible Stylesheet Language. It is similar to XML as CSS is to HTML.
NeedforXSL
In case of HTML documents, tags are predefined such as table, div, span, etc. The browser
knows how to add style to them and display them using CSS styles. But in case of XML
documents, tags are not predefined. In order to understand and style an XML document,
World Wide Web Consortium (W3C) developed XSL which can act as an XML-based
Stylesheet Language. An XSL document specifies how a browser should render an XML
document.
Following are the main parts of XSL:
 XSLT — used to transform XML documents into various other types of document.
 XPath — used to navigate XML documents.
 XSL-FO — used to format XML documents.
WhatisXPath?
XPath is an official recommendation of the World Wide Web Consortium (W3C). It defines
a language to find information in an XML file. It is used to traverse elements and attributes
of an XML document. XPath provides various types of expressions which can be used to
enquire relevant information from the XML document.
 Structure Definitions — XPath defines the parts of an XML document like
element, attribute, text, namespace, processing-instruction, comment, and
document nodes
 Path Expressions — XPath provides powerful path expressions select nodes or
list of nodes in XML documents.
 Standard Functions — XPath provides a rich library of standard functions for
manipulation of string values, numeric values, date and time comparison, node and
QName manipulation, sequence manipulation, Boolean values etc.
 Major part of XSLT — XPath is one of the major elements in XSLT standard and
is must have knowledge in order to work with XSLT documents.
 W3C recommendation — XPath is an official recommendation of World Wide Web
Consortium (W3C).
1. XPath – Overview
XPath
2
One should keep the following points in mind, while working with XPath:
 XPath is core component of XSLT standard.
 XSLT cannot work without XPath.
 XPath is basis of XQuery and XPointer.
XPath
3
An XPath expression generally defines a pattern in order to select a set of nodes. These
patterns are used by XSLT to perform transformations or by XPointer for addressing
purpose.
XPath specification specifies seven types of nodes which can be the output of execution of
the XPath expression.
 Root
 Element
 Text
 Attribute
 Comment
 Processing Instruction
 Namespace
XPath uses a path expression to select node or a list of nodes from an XML document.
Following is the list of useful paths and expression to select any node/ list of nodes from
an XML document.
Expression Description
node-name Select all nodes with the given name "nodename"
/ Selection starts from the root node
// Selection starts from the current node that match the selection
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
student Example: Selects all nodes with the name "student"
2. XPath — Expression
XPath
4
class/student Example: Selects all student elements that are children of class
//student Selects all student elements no matter where they are in the document
Example
In this example, we've created a sample XML document, students.xml and its stylesheet
document students.xsl which uses the XPath expressions under select attribute of
various XSL tags to get the values of roll no, firstname, lastname, nickname and marks of
each student node.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
XPath
5
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
6
Verify the output
XPath
7
In this chapter, we'll see the XPath expression in details covering common types of Nodes,
XPath defines and handles.
S.N. Node Type & Description
1
Root
Root element node of an XML Document.
2
Element
Element node.
3
Text
Text of an element node.
4
Attribute
Attribute of an element node.
5
Comment
Comment
Let us now understand the nodes in detail.
XPathRootNode
Following are the ways to get root element and do the processing afterwards.
Use Wildcard
Use /*, wild card expression to select the root node.
<p><xsl:value-of select="name(/*)"/></p>
Use Name
Use /class, to select root node by name.
<p><xsl:value-of select="name(/class)"/></p>
3. XPath — Nodes
XPath
8
Use Name with wild card
Use /class/*, select all element under root node.
<p><xsl:value-of select="name(/class/*)"/></p>
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
XPath
9
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>Root Element. Xpath expression = "/*"</h3>
<p><xsl:value-of select="name(/*)"/></p>
<h3>Root Element. Xpath expression = "/class"</h3>
<p> <xsl:value-of select="name(/class)"/></p>
<h3>Details of each Students. Xpath expression = "/class/*"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/*">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
10
Verify the output
XPathElementNode
There are multiple ways to get and handle elements.
/class/* — select all element under root node.
<xsl:for-each select="/class/*">
/class/student — select all student element under root node.
<xsl:for-each select="/class/student">
//student — select all student elements in the document.
<xsl:for-each select="//student">
XPath
11
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>Details of each Students. Xpath expression = "/class/*"</h3>
XPath
12
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/*">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
<h3>Details of each Students. Xpath expression = "/class/student"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
XPath
13
</tr>
</xsl:for-each>
</table>
<h3>Details of each Students. Xpath expression = "//student"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="//student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
14
Verify the output
XPathTextNode
Text can be easily retrieved and checked by using the name of the element.
name — get the text value of node "name".
<td><xsl:value-of select="firstname"/></td>
Text can be used to compared using operators.
XPath
15
marks > 85 — get the text value of node "marks" and compare with a value.
<xsl:if test="marks > 85">
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XPath
16
<xsl:template match="/">
<html>
<body>
<h3>Details of each Students. Xpath expression = "/class/student"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
<h3>Details of each Students whose marks are greater than 85. Xpath
expression = "marks > 85"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="//student">
<xsl:if test="marks > 85">
<tr>
<td>
XPath
17
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
18
XPathAttributeNode
This attribute can be easily retrieved and checked by using the @attribute-name of the
element.
@name - get the value of attribute "name".
<td><xsl:value-of select="@rollno"/></td>
Attribute can be used to compared using operators.
@rollno = 493 - get the text value of attribute "rollno" and compare with a value.
<xsl:if test="@rollno = 493">
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
XPath
19
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>Details of each Students. Xpath expression = "/class/student"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
<h3>Details of Student whose roll no is 493. Xpath expression = "@rollno =
493"</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
XPath
20
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="//student">
<xsl:if test="@rollno = 493">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
21
XPathCommentNode
Comment can be easily retrieved of the element.
comment() - get the value of attribute "name".
<xsl:value-of select="/class/student/preceding-sibling::comment()"/>
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
XPath
22
<!-- Comment: This is a list of student -->
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>Comment(). Xpath expression = "/class/student/preceding-
sibling::comment()"</h3>
<xsl:value-of select="/class/student/preceding-sibling::comment()"/>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
XPath
23
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
24
Location path specifies the location of node in XML document. This path can be absolute
or relative. If location path starts with root node or with '/' then it is an absolute path.
Following are few of the example locating the elements using absolute path.
/class/student - select student nodes within class root node.
<xsl:for-each select="/class/student">
/class/student/firstname - select firstname of a student node within class root node.
<p><xsl:value-of select="/class/student/firstname"/></p>
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
4. XPath — Absolute Path
XPath
25
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/" >
<html>
<body>
<h3>Details of each Students. </h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<tr>
<td>
<xsl:value-of select="/class/student[1]/@rollno"/>
</td>
<td><xsl:value-of select="/class/student[1]/firstname"/></td>
<td><xsl:value-of select="/class/student[1]/lastname"/></td>
<td><xsl:value-of select="/class/student[1]/nickname"/></td>
<td><xsl:value-of select="/class/student[1]/marks"/></td>
</tr>
<tr>
<td>
<xsl:value-of select="/class/student/@rollno"/>
</td>
<td><xsl:value-of select="/class/student[2]/firstname"/></td>
<td><xsl:value-of select="/class/student[2]/lastname"/></td>
XPath
26
<td><xsl:value-of select="/class/student[2]/nickname"/></td>
<td><xsl:value-of select="/class/student[2]/marks"/></td>
</tr>
<tr>
<td>
<xsl:value-of select="/class/student[3]/@rollno"/>
</td>
<td><xsl:value-of select="/class/student[3]/firstname"/></td>
<td><xsl:value-of select="/class/student[3]/lastname"/></td>
<td><xsl:value-of select="/class/student[3]/nickname"/></td>
<td><xsl:value-of select="/class/student[3]/marks"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
27
Location path specifies the location of node in XML document. This path can be absolute
or relative. If location path starts with the node that we've selected, then it is a relative
path.
Following are a few examples locating the elements using relative path.
firstname — select firstname related to student nodes .
<p><xsl:value-of select="firstname"/></p>
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
5. XPath — Relative Path
XPath
28
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/" >
<html>
<body>
<h3>Details of each Students. </h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
29
Verify the output
XPath
30
As location path defines the location of a node using absolute or relative path, axes are
used to identify elements by their relationship like parent, child, sibling, etc. Axes are
named so because they refer to axis on which elements are lying relative to an element.
Following is the list of various Axis values.
Axis Description
ancestor
Represents the ancestors of the current node which include the
parents up to the root node.
ancestor-or-
self
Represents the current node and it's ancestors.
attribute Represents the attributes of the current node.
child Represents the children of the current node.
descendant
Represents the descendants of the current node. Descendants include
the node's children upto the leaf node(no more children).
descendant-or-
self
Represents the current node and it's descendants.
following Represents all nodes that come after the current node.
following-
sibling
Represents the following siblings of the context node. Siblings are at
the same level as the current node and share it's parent.
namespace Represents the namespace of the current node.
parent Represents the parent of the current node.
preceding
Represents all nodes that come before the current node (i.e. before
it's opening tag).
self Represents the current node.
6. XPath — Axes
XPath
31
Following are a few examples on the use of axes.
firstname — select firstname related to student nodes.
<p><xsl:value-of select="firstname"/></p>
<xsl:value-of select="/class/student/preceding-sibling::comment()"/>
Example
In this example, we've created a sample XML document students.xml and its stylesheet
document students.xsl which uses the XPath expressions.
Following is the sample XML used.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<!-- Comment: This is a list of student -->
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
XPath
32
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/" >
<html>
<body>
<xsl:value-of select="/class/student/preceding-sibling::comment()"/>
<br/>
<xsl:text>First Student: </xsl:text>
<xsl:value-of select="/class/student/child::firstname" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verifytheoutput
XPath
33
In this chapter, we'll see XPath operators and functions in details covering commonly used
XPath defines and handles. XPath defines Operators and functions on Nodes, String,
Number and Boolean types.
Following is the list we are going to discuss about.
S.N. Operators/Functions & Description
1
Comparison Operators
Comparison operators to compare values.
2
Boolean Operators
Boolean operators to check 'and', 'or' & 'not' functionalities.
3
Number Functions/Operators
Operators/Functions on numbers.
4
String Functions
Various string functions.
5
Node Functions/Operators
Various functions and operators acting on nodes.
XPathComparisonOperators
XPath defines following comparison operators to be used with the XPath expressions.
Operator Description
= is equals to
!= is not equals to
< is less than
> is greater than
<= is less than or equals to
7. XPath — Operators
XPath
34
>= is greater than or equals to
Example
This example creates a table of <student> element with its attribute roll no and its child
<firstname>,<lastname><nickname> and <marks> by iterating over each student. It
checks marks to be greater than 90 and then prints the student(s) details.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
XPath
35
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<xsl:if test="marks > 90">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
36
Verify the output
XPathBooleanOperators
XPath defines the following Boolean operators to be used with the XPath expressions.
Operator Description
and both conditions to be satisfied
or any one of the condition to be satisfied
not() function to check condition not to be satisfied.
Example
This example creates a table of <student> element with its attribute roll no and its child
<firstname>,<lastname><nickname> and <marks> by iterating over each student. It
checks rollno to be either 393 or 493 and then prints the student(s) details.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
XPath
37
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student[(@rollno = 393) or ((@rollno = 493))]">
XPath
38
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
39
XPathNumberOperators/Functions
XPath defines the following operators on numbers to be used with the XPath expressions.
Operator Description
+ used for addition operation
- used for subtraction operation
* used for multiplication operation
div used for division operation
mod used for modulo operation
XPath defines the following functions on numbers to be used with the XPath expressions.
Function Description
ceiling() returns the smallest integer larger than the value provided.
floor() returns the largest integer smaller than the value provided.
round() returns the rounded value to nearest integer.
sum() returns the sum of two numbers.
Example
This example creates a table of <student> element with its attribute roll no and its child
<firstname>,<lastname><nickname> and <marks> by iterating over each student. It
calculates grades of the student and then prints the student(s) details.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
XPath
40
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
<th>Grade</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
XPath
41
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
<td>
<xsl:choose>
<xsl:when test="marks div 90 > 1">
High
</xsl:when>
<xsl:when test="marks div 80 > 1">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
42
Verify the output
XPathStringFunctions
The following is a list of XPath String functions:
S.N. Function & Description
1
starts-with(string1, string2)
Returns true when first string starts with the second string.
2
contains(string1, string2)
Returns true when the first string contains the second string.
3
substring(string, offset, length?)
Returns a section of the string. The section starts at offset up to the length
provided.
4
substring-before(string1, string2)
Returns the part of string1 up before the first occurrence of string2.
5
substring-after(string1, string2)
Returns the part of string1 after the first occurrence of string2.
6
string-length(string)
Returns the length of string in terms of characters.
XPath
43
7
normalize-space(string)
Trims the leading and trailing space from string.
8
translate(string1, string2, string3)
Returns string1 after any matching characters in string2 have been replaced
by the characters in string3.
9
concat(string1, string2, ...)
Concatenates all strings.
10
format-number(number1, string1, string2)
Returns a formatted version of number1 after applying string1 as a format
string. string2 is an optional locale string.
Example
This example creates a table of <student> element with their names and length of names,
by iterating over each student. It calculates length of the student name after concatenating
firstname and lastname and then prints the student(s) details.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
XPath
44
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Length of Name</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td><xsl:value-of select="concat(firstname,' ',lastname)"/></td>
<td><xsl:value-of select="string-length(concat(firstname,' ',lastname))"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
45
Verify the output
XPathNodeFunctions
XPath defines the following operators on nodes to be used with the XPath expressions.
Operator Description
/ used to select node under a specific node.
// used to select node from root node
[...] used to check node value
| used for union of two node sets
XPath defines the following functions on nodes to be used with the XPath expressions.
Function Description
comment() selects nodes which are comments.
node() selects all kinds of nodes.
processing-instruction() selects nodes which are processing instruction.
XPath
46
text() selects a text node.
name() provides the name of the node.
position() provides the position of the node.
last() selects the last node relative to current node;
Example
This example creates a table of <student> element with their details, by iterating over
each student. It calculates the position of the student node then prints the student(s)
details along with serial no.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
XPath
47
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Serial No</th>
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="@rollno"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Verify the output
XPath
48
XPath
49
XPath defines the following wildcards on nodes to be used with the XPath expressions.
Wildcard Description
* used to match any node.
. used to match the current node in context.
@* used to match any attribute
node() used to match node of any type
Example
This example creates a table of <student> element with their details, by iterating over
each student.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
8. XPath — Wildcard
XPath
50
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<xsl:apply-templates select="class/*" />
</body>
</html>
</xsl:template>
<xsl:template match="class/*">
<xsl:apply-templates select="@rollno" />
<xsl:apply-templates select="firstname" />
<xsl:apply-templates select="lastname" />
<xsl:apply-templates select="nickname" />
<xsl:apply-templates select="marks" />
<br />
</xsl:template>
<xsl:template match="@rollno">
<span style="font-size=22px;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="firstname">
First Name:<span style="color:blue;">
XPath
51
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="lastname">
Last Name:<span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="nickname">
Nick Name:<span style="color:red;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="marks">
Marks:<span style="color:gray;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
XPath
52
Verify the output
XPath
53
Predicate refers to the XPath expression written in square brackets. It refers to restrict the
selected nodes in a node set for some condition. For example,
Predicate Description
/class/student[1]
Select first student element which is child of the class
element.
/class/student[last()]
Select last student element which is child of the class
element.
/class/student[@rolllno=493] Select student element with roll no 493.
/class/student[marks>85] Select student element with marks > 85.
Example
This example creates a table of <student> element with their details, by iterating over
each student. It calculates the position of the student node and then prints the student(s)
details along with serial no.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
9. XPath — Predicate
XPath
54
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="/class/student[1]">
<tr>
<td><xsl:value-of select="@rollno"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
<xsl:for-each select="/class/student[last()]">
<tr>
<td><xsl:value-of select="@rollno"/></td>
XPath
55
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
<xsl:for-each select="/class/student[@rollno=493]">
<tr>
<td><xsl:value-of select="@rollno"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
<xsl:for-each select="/class/student[marks > 85]">
<tr>
<td><xsl:value-of select="@rollno"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPath
56
Verify the output

More Related Content

What's hot

XML
XMLXML
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
Mukesh Tilokani
 
Dom parser
Dom parserDom parser
Dom parser
sana mateen
 
Xml schema
Xml schemaXml schema
Xml schema
Dr.Saranya K.G
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
Swati Parmar
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
Russell Ward
 
Xml dom
Xml domXml dom
Xml dom
sana mateen
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
Ameer Khan
 
Intro to XML in libraries
Intro to XML in librariesIntro to XML in libraries
Intro to XML in libraries
Kyle Banerjee
 
The Lumber Mill Xslt For Your Templates
The Lumber Mill   Xslt For Your TemplatesThe Lumber Mill   Xslt For Your Templates
The Lumber Mill Xslt For Your TemplatesThomas Weinert
 
Json processing
Json processingJson processing
Json processing
Ahmed Gamil
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16
Smita B Kumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Kumar
 
Search A Database Via NLB
Search A Database Via NLBSearch A Database Via NLB
Search A Database Via NLB
katieday
 
Xml databases
Xml databasesXml databases
Xml databases
Srinivasan R
 
Data exchange over internet (XML vs JSON)
Data exchange over internet (XML vs JSON)Data exchange over internet (XML vs JSON)
Data exchange over internet (XML vs JSON)
Wajahat Shahid
 

What's hot (20)

XML
XMLXML
XML
 
XML
XMLXML
XML
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
 
Dom parser
Dom parserDom parser
Dom parser
 
Xml processors
Xml processorsXml processors
Xml processors
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
XMl
XMlXMl
XMl
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
 
XML
XMLXML
XML
 
Xml dom
Xml domXml dom
Xml dom
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
Intro to XML in libraries
Intro to XML in librariesIntro to XML in libraries
Intro to XML in libraries
 
The Lumber Mill Xslt For Your Templates
The Lumber Mill   Xslt For Your TemplatesThe Lumber Mill   Xslt For Your Templates
The Lumber Mill Xslt For Your Templates
 
Json processing
Json processingJson processing
Json processing
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Search A Database Via NLB
Search A Database Via NLBSearch A Database Via NLB
Search A Database Via NLB
 
Xml databases
Xml databasesXml databases
Xml databases
 
Data exchange over internet (XML vs JSON)
Data exchange over internet (XML vs JSON)Data exchange over internet (XML vs JSON)
Data exchange over internet (XML vs JSON)
 

Similar to Xpath tutorial

Xslt tutorial
Xslt tutorialXslt tutorial
Xslt tutorial
Ashoka Vanjare
 
Java xml tutorial
Java xml tutorialJava xml tutorial
Java xml tutorial
HarikaReddy115
 
Xaml Guidelines Draft0
Xaml Guidelines Draft0Xaml Guidelines Draft0
Xaml Guidelines Draft0guest27165
 
Xml tutorial
Xml tutorialXml tutorial
Xml tutorial
Ashoka Vanjare
 
Session 4
Session 4Session 4
Xml tutorial
Xml tutorialXml tutorial
Xml tutorial
IT
 
07 Advanced RTF Template Techniques.doc
07 Advanced RTF Template Techniques.doc07 Advanced RTF Template Techniques.doc
07 Advanced RTF Template Techniques.doc
Maqsood Joyo
 
Xsd tutorial
Xsd tutorialXsd tutorial
Xsd tutorial
Ashoka Vanjare
 
X query language reference
X query language referenceX query language reference
X query language referenceSteve Xu
 
XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7
Deniz Kılınç
 
03 x files
03 x files03 x files
03 x files
Baskarkncet
 
Xml programming language myassignmenthelp.net
Xml programming  language myassignmenthelp.netXml programming  language myassignmenthelp.net
Xml programming language myassignmenthelp.net
www.myassignmenthelp.net
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
gauravashq
 
Markup For Dummies (Russ Ward)
Markup For Dummies (Russ Ward)Markup For Dummies (Russ Ward)
Markup For Dummies (Russ Ward)
STC-Philadelphia Metro Chapter
 
E05412327
E05412327E05412327
E05412327
IOSR-JEN
 
Tutor Xml Gxs
Tutor Xml GxsTutor Xml Gxs
Tutor Xml Gxs
nit Allahabad
 
Xquery xpath
Xquery xpathXquery xpath
Xquery xpath
Ashoka Vanjare
 
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriyaIntegrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
VijiPriya Jeyamani
 
XSLT 3.0 Improvements - XML Prague 2018
XSLT 3.0 Improvements - XML Prague 2018XSLT 3.0 Improvements - XML Prague 2018
XSLT 3.0 Improvements - XML Prague 2018
Octavian Nadolu
 

Similar to Xpath tutorial (20)

Xslt tutorial
Xslt tutorialXslt tutorial
Xslt tutorial
 
Java xml tutorial
Java xml tutorialJava xml tutorial
Java xml tutorial
 
Xaml Guidelines Draft0
Xaml Guidelines Draft0Xaml Guidelines Draft0
Xaml Guidelines Draft0
 
Xml tutorial
Xml tutorialXml tutorial
Xml tutorial
 
Session 4
Session 4Session 4
Session 4
 
Xml toc 1 day
Xml toc   1 dayXml toc   1 day
Xml toc 1 day
 
Xml tutorial
Xml tutorialXml tutorial
Xml tutorial
 
07 Advanced RTF Template Techniques.doc
07 Advanced RTF Template Techniques.doc07 Advanced RTF Template Techniques.doc
07 Advanced RTF Template Techniques.doc
 
Xsd tutorial
Xsd tutorialXsd tutorial
Xsd tutorial
 
X query language reference
X query language referenceX query language reference
X query language reference
 
XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7
 
03 x files
03 x files03 x files
03 x files
 
Xml programming language myassignmenthelp.net
Xml programming  language myassignmenthelp.netXml programming  language myassignmenthelp.net
Xml programming language myassignmenthelp.net
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
 
Markup For Dummies (Russ Ward)
Markup For Dummies (Russ Ward)Markup For Dummies (Russ Ward)
Markup For Dummies (Russ Ward)
 
E05412327
E05412327E05412327
E05412327
 
Tutor Xml Gxs
Tutor Xml GxsTutor Xml Gxs
Tutor Xml Gxs
 
Xquery xpath
Xquery xpathXquery xpath
Xquery xpath
 
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriyaIntegrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
 
XSLT 3.0 Improvements - XML Prague 2018
XSLT 3.0 Improvements - XML Prague 2018XSLT 3.0 Improvements - XML Prague 2018
XSLT 3.0 Improvements - XML Prague 2018
 

More from Ashoka Vanjare

Tika tutorial
Tika tutorialTika tutorial
Tika tutorial
Ashoka Vanjare
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
Ashoka Vanjare
 
Sqoop tutorial
Sqoop tutorialSqoop tutorial
Sqoop tutorial
Ashoka Vanjare
 
Postgresql tutorial
Postgresql tutorialPostgresql tutorial
Postgresql tutorial
Ashoka Vanjare
 
Postgresql quick guide
Postgresql quick guidePostgresql quick guide
Postgresql quick guide
Ashoka Vanjare
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
Ashoka Vanjare
 
Perltut
PerltutPerltut
Php7 tutorial
Php7 tutorialPhp7 tutorial
Php7 tutorial
Ashoka Vanjare
 
Mongodb tutorial
Mongodb tutorialMongodb tutorial
Mongodb tutorial
Ashoka Vanjare
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
Ashoka Vanjare
 
Mahout tutorial
Mahout tutorialMahout tutorial
Mahout tutorial
Ashoka Vanjare
 
Learn embedded systems tutorial
Learn embedded systems tutorialLearn embedded systems tutorial
Learn embedded systems tutorial
Ashoka Vanjare
 
Learn data structures algorithms tutorial
Learn data structures algorithms tutorialLearn data structures algorithms tutorial
Learn data structures algorithms tutorial
Ashoka Vanjare
 
Learn c standard library
Learn c standard libraryLearn c standard library
Learn c standard library
Ashoka Vanjare
 
Learn c programming
Learn c programmingLearn c programming
Learn c programming
Ashoka Vanjare
 
Json tutorial
Json tutorialJson tutorial
Json tutorial
Ashoka Vanjare
 
Json perl example
Json perl exampleJson perl example
Json perl example
Ashoka Vanjare
 
Java tutorial
Java tutorialJava tutorial
Java tutorial
Ashoka Vanjare
 
Java spring tutorial
Java spring tutorialJava spring tutorial
Java spring tutorial
Ashoka Vanjare
 
Java maven tutorial
Java maven tutorialJava maven tutorial
Java maven tutorial
Ashoka Vanjare
 

More from Ashoka Vanjare (20)

Tika tutorial
Tika tutorialTika tutorial
Tika tutorial
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
 
Sqoop tutorial
Sqoop tutorialSqoop tutorial
Sqoop tutorial
 
Postgresql tutorial
Postgresql tutorialPostgresql tutorial
Postgresql tutorial
 
Postgresql quick guide
Postgresql quick guidePostgresql quick guide
Postgresql quick guide
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
 
Perltut
PerltutPerltut
Perltut
 
Php7 tutorial
Php7 tutorialPhp7 tutorial
Php7 tutorial
 
Mongodb tutorial
Mongodb tutorialMongodb tutorial
Mongodb tutorial
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
Mahout tutorial
Mahout tutorialMahout tutorial
Mahout tutorial
 
Learn embedded systems tutorial
Learn embedded systems tutorialLearn embedded systems tutorial
Learn embedded systems tutorial
 
Learn data structures algorithms tutorial
Learn data structures algorithms tutorialLearn data structures algorithms tutorial
Learn data structures algorithms tutorial
 
Learn c standard library
Learn c standard libraryLearn c standard library
Learn c standard library
 
Learn c programming
Learn c programmingLearn c programming
Learn c programming
 
Json tutorial
Json tutorialJson tutorial
Json tutorial
 
Json perl example
Json perl exampleJson perl example
Json perl example
 
Java tutorial
Java tutorialJava tutorial
Java tutorial
 
Java spring tutorial
Java spring tutorialJava spring tutorial
Java spring tutorial
 
Java maven tutorial
Java maven tutorialJava maven tutorial
Java maven tutorial
 

Recently uploaded

Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 

Recently uploaded (20)

Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 

Xpath tutorial

  • 1.
  • 2. XPath i AbouttheTutorial XPath is a query language that is used for traversing through an XML document. It is used commonly to search particular elements or attributes with matching patterns. This tutorial explains the basics of XPath. It contains chapters discussing all the basic components of XPath with suitable examples. Audience This tutorials has been designed for beginners to help them understand the basic concepts related to XPath. This tutorial will give you enough understanding on XPath from where you can take yourself to higher levels of expertise. Prerequisites Before proceeding with this tutorial, you should have basic knowledge of XML, HTML, and JavaScript. Disclaimer&Copyright  Copyright 2016 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com.
  • 3. XPath ii TableofContents About the Tutorial....................................................................................................................................i Audience..................................................................................................................................................i Prerequisites............................................................................................................................................i Disclaimer & Copyright.............................................................................................................................i 1. XPATH – OVERVIEW................................................................................................................ 1 XSL ..........................................................................................................................................................1 Need for XSL............................................................................................................................................1 What is XPath..........................................................................................................................................1 Features..................................................................................................................................................2 2. XPATH — EXPRESSION........................................................................................................... 3 Verify the output.....................................................................................................................................6 3. XPATH — NODES...................................................................................................................... 7 XPath Root Node.....................................................................................................................................7 Verify the output...................................................................................................................................10 XPath Element Node .............................................................................................................................10 Verify the output...................................................................................................................................14 XPath Text Node ...................................................................................................................................14 Verify the output...................................................................................................................................17 XPath Attribute Node............................................................................................................................18 Verify the output...................................................................................................................................20 XPath Comment Node...........................................................................................................................21 Verify the output...................................................................................................................................23
  • 4. XPath iii 4. XPATH — ABSOLUTE PATH................................................................................................24 Verify the output...................................................................................................................................26 5. XPATH — RELATIVE PATH .................................................................................................27 Verify the output...................................................................................................................................29 6. XPATH — AXES........................................................................................................................30 Verify the output...................................................................................................................................32 7. XPATH — OPERATORS.........................................................................................................33 XPath Comparison Operators................................................................................................................33 Verify the output...................................................................................................................................36 XPath Boolean Operators......................................................................................................................36 Verify the output...................................................................................................................................38 XPath Number Operators / Functions ...................................................................................................39 Verify the output...................................................................................................................................42 XPath String Functions ..........................................................................................................................42 Verify the output...................................................................................................................................45 XPath Node Functions...........................................................................................................................45 Verify the output...................................................................................................................................47 8. XPATH — WILDCARD ...........................................................................................................49 Verify the output...................................................................................................................................52 9. XPATH — PREDICATE...........................................................................................................53 Verify the output...................................................................................................................................56
  • 5. XPath 1 Before learning XPath, we should first understand XSL which stands for Extensible Stylesheet Language. It is similar to XML as CSS is to HTML. NeedforXSL In case of HTML documents, tags are predefined such as table, div, span, etc. The browser knows how to add style to them and display them using CSS styles. But in case of XML documents, tags are not predefined. In order to understand and style an XML document, World Wide Web Consortium (W3C) developed XSL which can act as an XML-based Stylesheet Language. An XSL document specifies how a browser should render an XML document. Following are the main parts of XSL:  XSLT — used to transform XML documents into various other types of document.  XPath — used to navigate XML documents.  XSL-FO — used to format XML documents. WhatisXPath? XPath is an official recommendation of the World Wide Web Consortium (W3C). It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document. XPath provides various types of expressions which can be used to enquire relevant information from the XML document.  Structure Definitions — XPath defines the parts of an XML document like element, attribute, text, namespace, processing-instruction, comment, and document nodes  Path Expressions — XPath provides powerful path expressions select nodes or list of nodes in XML documents.  Standard Functions — XPath provides a rich library of standard functions for manipulation of string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values etc.  Major part of XSLT — XPath is one of the major elements in XSLT standard and is must have knowledge in order to work with XSLT documents.  W3C recommendation — XPath is an official recommendation of World Wide Web Consortium (W3C). 1. XPath – Overview
  • 6. XPath 2 One should keep the following points in mind, while working with XPath:  XPath is core component of XSLT standard.  XSLT cannot work without XPath.  XPath is basis of XQuery and XPointer.
  • 7. XPath 3 An XPath expression generally defines a pattern in order to select a set of nodes. These patterns are used by XSLT to perform transformations or by XPointer for addressing purpose. XPath specification specifies seven types of nodes which can be the output of execution of the XPath expression.  Root  Element  Text  Attribute  Comment  Processing Instruction  Namespace XPath uses a path expression to select node or a list of nodes from an XML document. Following is the list of useful paths and expression to select any node/ list of nodes from an XML document. Expression Description node-name Select all nodes with the given name "nodename" / Selection starts from the root node // Selection starts from the current node that match the selection . Selects the current node .. Selects the parent of the current node @ Selects attributes student Example: Selects all nodes with the name "student" 2. XPath — Expression
  • 8. XPath 4 class/student Example: Selects all student elements that are children of class //student Selects all student elements no matter where they are in the document Example In this example, we've created a sample XML document, students.xml and its stylesheet document students.xsl which uses the XPath expressions under select attribute of various XSL tags to get the values of roll no, firstname, lastname, nickname and marks of each student node. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class>
  • 9. XPath 5 students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 11. XPath 7 In this chapter, we'll see the XPath expression in details covering common types of Nodes, XPath defines and handles. S.N. Node Type & Description 1 Root Root element node of an XML Document. 2 Element Element node. 3 Text Text of an element node. 4 Attribute Attribute of an element node. 5 Comment Comment Let us now understand the nodes in detail. XPathRootNode Following are the ways to get root element and do the processing afterwards. Use Wildcard Use /*, wild card expression to select the root node. <p><xsl:value-of select="name(/*)"/></p> Use Name Use /class, to select root node by name. <p><xsl:value-of select="name(/class)"/></p> 3. XPath — Nodes
  • 12. XPath 8 Use Name with wild card Use /class/*, select all element under root node. <p><xsl:value-of select="name(/class/*)"/></p> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class>
  • 13. XPath 9 students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>Root Element. Xpath expression = "/*"</h3> <p><xsl:value-of select="name(/*)"/></p> <h3>Root Element. Xpath expression = "/class"</h3> <p> <xsl:value-of select="name(/class)"/></p> <h3>Details of each Students. Xpath expression = "/class/*"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/*"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 14. XPath 10 Verify the output XPathElementNode There are multiple ways to get and handle elements. /class/* — select all element under root node. <xsl:for-each select="/class/*"> /class/student — select all student element under root node. <xsl:for-each select="/class/student"> //student — select all student elements in the document. <xsl:for-each select="//student">
  • 15. XPath 11 Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>Details of each Students. Xpath expression = "/class/*"</h3>
  • 16. XPath 12 <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/*"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> <h3>Details of each Students. Xpath expression = "/class/student"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td>
  • 17. XPath 13 </tr> </xsl:for-each> </table> <h3>Details of each Students. Xpath expression = "//student"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="//student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 18. XPath 14 Verify the output XPathTextNode Text can be easily retrieved and checked by using the name of the element. name — get the text value of node "name". <td><xsl:value-of select="firstname"/></td> Text can be used to compared using operators.
  • 19. XPath 15 marks > 85 — get the text value of node "marks" and compare with a value. <xsl:if test="marks > 85"> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  • 20. XPath 16 <xsl:template match="/"> <html> <body> <h3>Details of each Students. Xpath expression = "/class/student"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> <h3>Details of each Students whose marks are greater than 85. Xpath expression = "marks > 85"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="//student"> <xsl:if test="marks > 85"> <tr> <td>
  • 21. XPath 17 <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 22. XPath 18 XPathAttributeNode This attribute can be easily retrieved and checked by using the @attribute-name of the element. @name - get the value of attribute "name". <td><xsl:value-of select="@rollno"/></td> Attribute can be used to compared using operators. @rollno = 493 - get the text value of attribute "rollno" and compare with a value. <xsl:if test="@rollno = 493"> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname>
  • 23. XPath 19 <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>Details of each Students. Xpath expression = "/class/student"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> <h3>Details of Student whose roll no is 493. Xpath expression = "@rollno = 493"</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th>
  • 24. XPath 20 <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="//student"> <xsl:if test="@rollno = 493"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 25. XPath 21 XPathCommentNode Comment can be easily retrieved of the element. comment() - get the value of attribute "name". <xsl:value-of select="/class/student/preceding-sibling::comment()"/> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class>
  • 26. XPath 22 <!-- Comment: This is a list of student --> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>Comment(). Xpath expression = "/class/student/preceding- sibling::comment()"</h3> <xsl:value-of select="/class/student/preceding-sibling::comment()"/> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th>
  • 27. XPath 23 <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 28. XPath 24 Location path specifies the location of node in XML document. This path can be absolute or relative. If location path starts with root node or with '/' then it is an absolute path. Following are few of the example locating the elements using absolute path. /class/student - select student nodes within class root node. <xsl:for-each select="/class/student"> /class/student/firstname - select firstname of a student node within class root node. <p><xsl:value-of select="/class/student/firstname"/></p> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> 4. XPath — Absolute Path
  • 29. XPath 25 <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/" > <html> <body> <h3>Details of each Students. </h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <tr> <td> <xsl:value-of select="/class/student[1]/@rollno"/> </td> <td><xsl:value-of select="/class/student[1]/firstname"/></td> <td><xsl:value-of select="/class/student[1]/lastname"/></td> <td><xsl:value-of select="/class/student[1]/nickname"/></td> <td><xsl:value-of select="/class/student[1]/marks"/></td> </tr> <tr> <td> <xsl:value-of select="/class/student/@rollno"/> </td> <td><xsl:value-of select="/class/student[2]/firstname"/></td> <td><xsl:value-of select="/class/student[2]/lastname"/></td>
  • 30. XPath 26 <td><xsl:value-of select="/class/student[2]/nickname"/></td> <td><xsl:value-of select="/class/student[2]/marks"/></td> </tr> <tr> <td> <xsl:value-of select="/class/student[3]/@rollno"/> </td> <td><xsl:value-of select="/class/student[3]/firstname"/></td> <td><xsl:value-of select="/class/student[3]/lastname"/></td> <td><xsl:value-of select="/class/student[3]/nickname"/></td> <td><xsl:value-of select="/class/student[3]/marks"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 31. XPath 27 Location path specifies the location of node in XML document. This path can be absolute or relative. If location path starts with the node that we've selected, then it is a relative path. Following are a few examples locating the elements using relative path. firstname — select firstname related to student nodes . <p><xsl:value-of select="firstname"/></p> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.XML <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> 5. XPath — Relative Path
  • 32. XPath 28 </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/" > <html> <body> <h3>Details of each Students. </h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 34. XPath 30 As location path defines the location of a node using absolute or relative path, axes are used to identify elements by their relationship like parent, child, sibling, etc. Axes are named so because they refer to axis on which elements are lying relative to an element. Following is the list of various Axis values. Axis Description ancestor Represents the ancestors of the current node which include the parents up to the root node. ancestor-or- self Represents the current node and it's ancestors. attribute Represents the attributes of the current node. child Represents the children of the current node. descendant Represents the descendants of the current node. Descendants include the node's children upto the leaf node(no more children). descendant-or- self Represents the current node and it's descendants. following Represents all nodes that come after the current node. following- sibling Represents the following siblings of the context node. Siblings are at the same level as the current node and share it's parent. namespace Represents the namespace of the current node. parent Represents the parent of the current node. preceding Represents all nodes that come before the current node (i.e. before it's opening tag). self Represents the current node. 6. XPath — Axes
  • 35. XPath 31 Following are a few examples on the use of axes. firstname — select firstname related to student nodes. <p><xsl:value-of select="firstname"/></p> <xsl:value-of select="/class/student/preceding-sibling::comment()"/> Example In this example, we've created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <!-- Comment: This is a list of student --> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl
  • 36. XPath 32 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/" > <html> <body> <xsl:value-of select="/class/student/preceding-sibling::comment()"/> <br/> <xsl:text>First Student: </xsl:text> <xsl:value-of select="/class/student/child::firstname" /> </body> </html> </xsl:template> </xsl:stylesheet> Verifytheoutput
  • 37. XPath 33 In this chapter, we'll see XPath operators and functions in details covering commonly used XPath defines and handles. XPath defines Operators and functions on Nodes, String, Number and Boolean types. Following is the list we are going to discuss about. S.N. Operators/Functions & Description 1 Comparison Operators Comparison operators to compare values. 2 Boolean Operators Boolean operators to check 'and', 'or' & 'not' functionalities. 3 Number Functions/Operators Operators/Functions on numbers. 4 String Functions Various string functions. 5 Node Functions/Operators Various functions and operators acting on nodes. XPathComparisonOperators XPath defines following comparison operators to be used with the XPath expressions. Operator Description = is equals to != is not equals to < is less than > is greater than <= is less than or equals to 7. XPath — Operators
  • 38. XPath 34 >= is greater than or equals to Example This example creates a table of <student> element with its attribute roll no and its child <firstname>,<lastname><nickname> and <marks> by iterating over each student. It checks marks to be greater than 90 and then prints the student(s) details. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
  • 39. XPath 35 <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="class/student"> <xsl:if test="marks > 90"> <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 40. XPath 36 Verify the output XPathBooleanOperators XPath defines the following Boolean operators to be used with the XPath expressions. Operator Description and both conditions to be satisfied or any one of the condition to be satisfied not() function to check condition not to be satisfied. Example This example creates a table of <student> element with its attribute roll no and its child <firstname>,<lastname><nickname> and <marks> by iterating over each student. It checks rollno to be either 393 or 493 and then prints the student(s) details. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname>
  • 41. XPath 37 <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="class/student[(@rollno = 393) or ((@rollno = 493))]">
  • 42. XPath 38 <tr> <td> <xsl:value-of select="@rollno"/> </td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 43. XPath 39 XPathNumberOperators/Functions XPath defines the following operators on numbers to be used with the XPath expressions. Operator Description + used for addition operation - used for subtraction operation * used for multiplication operation div used for division operation mod used for modulo operation XPath defines the following functions on numbers to be used with the XPath expressions. Function Description ceiling() returns the smallest integer larger than the value provided. floor() returns the largest integer smaller than the value provided. round() returns the rounded value to nearest integer. sum() returns the sum of two numbers. Example This example creates a table of <student> element with its attribute roll no and its child <firstname>,<lastname><nickname> and <marks> by iterating over each student. It calculates grades of the student and then prints the student(s) details. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student>
  • 44. XPath 40 <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> <th>Grade</th> </tr> <xsl:for-each select="class/student"> <tr> <td> <xsl:value-of select="@rollno"/> </td>
  • 45. XPath 41 <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> <td> <xsl:choose> <xsl:when test="marks div 90 > 1"> High </xsl:when> <xsl:when test="marks div 80 > 1"> Medium </xsl:when> <xsl:otherwise> Low </xsl:otherwise> </xsl:choose> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 46. XPath 42 Verify the output XPathStringFunctions The following is a list of XPath String functions: S.N. Function & Description 1 starts-with(string1, string2) Returns true when first string starts with the second string. 2 contains(string1, string2) Returns true when the first string contains the second string. 3 substring(string, offset, length?) Returns a section of the string. The section starts at offset up to the length provided. 4 substring-before(string1, string2) Returns the part of string1 up before the first occurrence of string2. 5 substring-after(string1, string2) Returns the part of string1 after the first occurrence of string2. 6 string-length(string) Returns the length of string in terms of characters.
  • 47. XPath 43 7 normalize-space(string) Trims the leading and trailing space from string. 8 translate(string1, string2, string3) Returns string1 after any matching characters in string2 have been replaced by the characters in string3. 9 concat(string1, string2, ...) Concatenates all strings. 10 format-number(number1, string1, string2) Returns a formatted version of number1 after applying string1 as a format string. string2 is an optional locale string. Example This example creates a table of <student> element with their names and length of names, by iterating over each student. It calculates length of the student name after concatenating firstname and lastname and then prints the student(s) details. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname>
  • 48. XPath 44 <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Name</th> <th>Length of Name</th> </tr> <xsl:for-each select="class/student"> <tr> <td><xsl:value-of select="concat(firstname,' ',lastname)"/></td> <td><xsl:value-of select="string-length(concat(firstname,' ',lastname))"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 49. XPath 45 Verify the output XPathNodeFunctions XPath defines the following operators on nodes to be used with the XPath expressions. Operator Description / used to select node under a specific node. // used to select node from root node [...] used to check node value | used for union of two node sets XPath defines the following functions on nodes to be used with the XPath expressions. Function Description comment() selects nodes which are comments. node() selects all kinds of nodes. processing-instruction() selects nodes which are processing instruction.
  • 50. XPath 46 text() selects a text node. name() provides the name of the node. position() provides the position of the node. last() selects the last node relative to current node; Example This example creates a table of <student> element with their details, by iterating over each student. It calculates the position of the student node then prints the student(s) details along with serial no. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl
  • 51. XPath 47 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Serial No</th> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="class/student"> <tr> <td><xsl:value-of select="position()"/></td> <td><xsl:value-of select="@rollno"/></td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Verify the output
  • 53. XPath 49 XPath defines the following wildcards on nodes to be used with the XPath expressions. Wildcard Description * used to match any node. . used to match the current node in context. @* used to match any attribute node() used to match node of any type Example This example creates a table of <student> element with their details, by iterating over each student. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> 8. XPath — Wildcard
  • 54. XPath 50 <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <xsl:apply-templates select="class/*" /> </body> </html> </xsl:template> <xsl:template match="class/*"> <xsl:apply-templates select="@rollno" /> <xsl:apply-templates select="firstname" /> <xsl:apply-templates select="lastname" /> <xsl:apply-templates select="nickname" /> <xsl:apply-templates select="marks" /> <br /> </xsl:template> <xsl:template match="@rollno"> <span style="font-size=22px;"> <xsl:value-of select="." /> </span> <br /> </xsl:template> <xsl:template match="firstname"> First Name:<span style="color:blue;">
  • 55. XPath 51 <xsl:value-of select="." /> </span> <br /> </xsl:template> <xsl:template match="lastname"> Last Name:<span style="color:green;"> <xsl:value-of select="." /> </span> <br /> </xsl:template> <xsl:template match="nickname"> Nick Name:<span style="color:red;"> <xsl:value-of select="." /> </span> <br /> </xsl:template> <xsl:template match="marks"> Marks:<span style="color:gray;"> <xsl:value-of select="." /> </span> <br /> </xsl:template> </xsl:stylesheet>
  • 57. XPath 53 Predicate refers to the XPath expression written in square brackets. It refers to restrict the selected nodes in a node set for some condition. For example, Predicate Description /class/student[1] Select first student element which is child of the class element. /class/student[last()] Select last student element which is child of the class element. /class/student[@rolllno=493] Select student element with roll no 493. /class/student[marks>85] Select student element with marks > 85. Example This example creates a table of <student> element with their details, by iterating over each student. It calculates the position of the student node and then prints the student(s) details along with serial no. students.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <class> <student rollno="393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> 9. XPath — Predicate
  • 58. XPath 54 <student rollno="593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class> students.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Students</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select="/class/student[1]"> <tr> <td><xsl:value-of select="@rollno"/></td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> <xsl:for-each select="/class/student[last()]"> <tr> <td><xsl:value-of select="@rollno"/></td>
  • 59. XPath 55 <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> <xsl:for-each select="/class/student[@rollno=493]"> <tr> <td><xsl:value-of select="@rollno"/></td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> <xsl:for-each select="/class/student[marks > 85]"> <tr> <td><xsl:value-of select="@rollno"/></td> <td><xsl:value-of select="firstname"/></td> <td><xsl:value-of select="lastname"/></td> <td><xsl:value-of select="nickname"/></td> <td><xsl:value-of select="marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>