Updated April 1, 2023
Introduction to XPath Operators
XML Path Language (XPath Operators) being an XML standard have few sets of operators can be defined as an expression which is structured using special Symbols and Operators and make use of the attribute type to work with node- sets. It defines operators, string and functions on nodes and follows the order of precedence. XPath Operators take Xpath expressions are dependent on context and implement functions and operators in a node which we select. The XML operators are supported on a small SQL specification with the own meaning. While carrying its operation on arithmetic it is done in floating-point and for comparison nodes are done with strings.
Explanation of all XPath Operators
There are different Xpath Operators available according to the respective property and they are categorized which are listed below:
- Node Operators
- Number/ Arithmetic Operators
- Boolean Operators
- Comparison Operators
1. Node Operators
These Nodes are used with Xpath Expressions which acts on a specific node or a node-set. First, two Operators are meant for Location path expressions.
- [ / ] : This Stepping Operator helps in selecting a specific node ( specific path) from a root node.
- [ // ] : This operator Being Descendant is used to select a node directing from a root node.
- [ … ] : This operator helps in checking a node value from the node-set.
- [ |] : It is used to compute union between two node – sets by this the duplicate values are filtered out and arranged in a sorted manner.
The functions used are here:
- Position(): This function helps in assigning the current position of the context nodes and returns the value starting with 1.
In the below example we have created a chef database with three fields.
.xml file
Code:
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "Hotelmag.xsl"?>
<class>
<chef id = "01">
<firstname>Willaiam</firstname>
<lastname>Hendry</lastname>
<income>35000</income>
</chef>
<chef id = "02">
<firstname>Abey</firstname>
<lastname>Zelic</lastname>
<income>40000</income>
</chef>
<chef id = "03">
<firstname>Brivian</firstname>
<lastname>Kalvin</lastname>
<income>50000</income>
</chef>
</class>
.xslt file
Code:
<?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>Chef</h2>
<table border = "2">
<tr bgcolor = "Red">
<th>SNo</th>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>income</th>
</tr>
<xsl:for-each select = "class/chef">
<tr>
<td><xsl:value-of select = "position()"/></td>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "income"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
2. Arithmetic /Number Operators
Arithmetic operators do mathematical calculations and are applied to Input, condition of XPath expressions.
Different types of Operators are:
- Addition (+): It does Addition Manipulation in the given field.
- Subtraction (-): It does Subtraction Manipulation in the given field.
- Multiplication (*): It does Multiplication Manipulation in the given field.
- Div (-): It does Addition Division in the given field.
- Mod: It does Modulation Manipulation in the given field.
Below Example shows the Income of the Chef is calculated using Arithmetic operators. By modifying the above database so that only income field is selected and the operator ‘div ‘ is assigned to them to achieve a new column status. Similarly we can do all the above operator to the income column. ( example multiplying the salary column into 3 etc..).
.xml file
Code:
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "Hotelmag.xsl"?>
<class>
<chef id = "01">
<firstname>Willaiam</firstname>
<lastname>Hendry</lastname>
<income>35000</income>
</chef>
<chef id = "02">
<firstname>Abey</firstname>
<lastname>Zelic</lastname>
<income>40000</income>
</chef>
<chef id = "03">
<firstname>Brivian</firstname>
<lastname>Kalvin</lastname>
<income>50000</income>
</chef>
</class>
.xslt file
Code:
<?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>Chef</h2>
<table border = "2">
<tr bgcolor = "Yellow">
<th>SNo</th>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>income</th>
<th> Status </th>
</tr>
<xsl:for-each select = "class/chef">
<tr>
<td><xsl:value-of select = "position()"/></td>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "income"/></td>
<td>
<xsl:choose>
<xsl:when test = "income div 35000 > 1">
Senior Level
</xsl:when>
<xsl:when test = "income div 40000 > 1">
Team Leader
</xsl:when>
<xsl:otherwise>
Assistant
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
3. Boolean Operators
This operator gives true or False results and supports Boolean functions like Boolean() , false(), true() , lang().And there are no Boolean constants supported in the Xpath.
- Boolean AND (and) : With And with two conditions, both the conditions should be true to return the statement, it fails if any one condition is false.
- Boolean OR (or ) : With OR expression among the two conditions, one condition must be true or both the conditions may be true.
- Boolean not (not() ) : Returns True if one expression is false and in vice-versa (opposite value). Example : <xsl:for-each match=”//b[not(@location and @location =’India’)]”>
Below Example shows how Boolean operators are done with Xpath queries. In the test example the predicate filters the elements and returns only the results which satisfy the conditions specified. Here Boolean Operator OR is applied for the item type.
.xml file
Code:
<?xml version="1.0"?>
<list>
<item type ="Biscuits">
<name>Gerber</name>
<carb>40</carb>
<fiber>8</fiber>
<fat>0.8</fat>
<kg>1120</kg>
</item>
<item type="Healthy drink">
<name>Smuff</name>
<carb>50</carb>
<fiber>9</fiber>
<fat>0.7</fat>
<kg>2220</kg>
</item>
<item type="veggie bisc">
<name>Gerber</name>
<carb>50</carb>
<fiber>6</fiber>
<fat>0.5</fat>
<kg>720</kg>
</item>
<item type="grains">
<name>Hena Multi Grain</name>
<carb>60</carb>
<fiber>10</fiber>
<fat>0.4</fat>
<kg>1500</kg>
</item>
</list>
.xslt file
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="list">
<table border="2" style="background-color: pink">
<tr>
<th>SNO</th>
<th> Item</th>
<th>Carbohydrates </th>
<th>Fiber</th>
<th>Fat content </th>
<th>Kilo</th>
</tr>
<xsl:for-each select="item[(@type = 'veggie bisc') or (@type = 'grains')]">
<tr>
<td><b><xsl:value-of select="position()"/></b></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="carb"/></td>
<td><xsl:value-of select="fiber"/></td>
<td><xsl:value-of select="fat"/></td>
<td><xsl:value-of select="kg"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output:
4. Comparison Operator
This operates does a comparison between two values. Below shows the different types of the comparison operator.
- Equal to (=): Checks whether A and B attributes are equal.
- Not Equal to (! =): Checks A attribute is not equal to B attribute.
- Lesser than / lesser than equal to (<=) : Specifies A attribute is less or lesser than equal to
- Greater / Greater than equal to (>=): SpecifiesA attribute is greater or greater than equal to B attribute.
Below Example shows Calculating Income >= 40,000 from the database using comparison operator. I have used xml file as chef database from the above example. The modified .xslt file part is given below.
.xslt file
Code:
<?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>Chef</h2>
<table border = "2">
<tr bgcolor = "Yellow">
<th>SNo</th>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>income</th>
<th> Status </th>
</tr>
<xsl:for-each select = "class/chef">
<xsl:if test = "income >= 40000">
<tr>
<td><xsl:value-of select = "position()"/></td>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "income"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Conclusion
In this article, we learned deeply about the XPath Operators. And we gone through their types with the syntax. We have seen the working of the different operators with the help of an example as well. And also, later we have seen how to filter or find an element on a web page using operators.
Recommended Articles
This is a guide to XPath Operators. Here we discuss an introduction to XPath Operators and the explanation to the operators with examples. You can also go through our other related articles to learn more –