XPath Operators
XPath Operators are the operators which are used in XPath expression. The Operators enhance our operating ability to the XML element. Such as we can check conditions using conditional operators and check multiple conditions using Boolean operators.
Types of Operators:
Serial No. |
Operator |
Representation. |
---|---|---|
1 |
Arithmetic Operator |
Such as +, -, *…etc |
2 |
Node Operator |
Operate on node |
3 |
Boolean Operator |
Such as and, or, not. |
4 |
Comparison Operator |
Compare operands |
There are lots of operators available such as Boolean and, or, not Arithematic +, -, *, Node operator /, //, “..”, |.
Operators available are:
S.No. |
Operator |
Description |
---|---|---|
1 |
/ |
Node operator which is used to select a node under the specific node. |
2 |
// |
Node operator which is used to select a node from the root node. |
3 |
| |
Node operator which is used to form a union of two node sets. |
4 |
+ |
Addition Operator. |
5 |
– |
Subtraction Operator. |
6 |
* |
Multiplication Operator. |
7 |
div |
Division Operator. |
8 |
mod |
Modulo Operator. |
9 |
= |
Equality Operator. |
10 |
!= |
Unequality Operator. |
11 |
< |
Less than Operator. |
12 |
> |
Greater than Operator. |
13 |
<= |
Less than equal to Operator. |
14 |
>= |
Greater than equal to Operator. |
Different types of Operators have different syntax. But most of the Arithmetic Operator Syntax is shown below:
Syntax:
Operand1 Arithematic_Operator Operand2
Parameters:
- Operand1: It is the first element on which operation is performed.
- Operand 2the: It is the second element on which operation is performed.
Example 1: In this example, we will see how we select the current node using the ‘.’ operator and div operator. Save the below file as mentioned and view the xsl file in the web browser.
XML
<!--File: Test.xml--> <? xml version = "1.0" encoding = "UTF-8" ?> <? xml-stylesheet type = "text/xsl " href = "Rule.xsl" ?> < student > < s > < name >Divyank Singh Sikarwar </ name > < class >12</ class > < Total_marks >86</ Total_marks > </ s > < s > < name >Aniket Chauhan </ name > < class >11</ class > < Total_marks >84</ Total_marks > </ s > < s > < name >Simran Agarwal</ name > < class >12</ class > < Total_marks >54</ Total_marks > </ s > < s > < name >Abhay Chauhan</ name > < class >11</ class > < Total_marks >75</ Total_marks > </ s > < s > < name >Himanshu Bhatia</ name > < class >12</ class > < Total_marks >55</ Total_marks > </ s > < s > < name >Suresh Sharma</ name > < class >10</ class > < Total_marks >85</ Total_marks > </ s > < s > < name >Dharmesh Snehil</ name > < class >11</ class > < Total_marks >68</ Total_marks > </ s > < s > < name >Ramesh Argrwal</ name > < class >10</ class > < Total_marks >75</ Total_marks > </ s > </ student > |
XML
<!--File: Rule. 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 > < h1 align = "center" >Students' Basic Details</ h1 > < table border = "3" align = "center" > < tr > < th >Details</ th > </ tr > < xsl:for-each select = "student/s" > < tr > < xsl:if test = "class != '12'" > < td > < xsl:apply-templates select = "name" /> < xsl:apply-templates select = "class" /> < xsl:choose > < xsl:when test="Total_marks div 65 >= 1"> Grade: A </ xsl:when > < xsl:otherwise > Grade: B </ xsl:otherwise > </ xsl:choose > </ td > </ xsl:if > </ tr > </ xsl:for-each > </ table > </ body > </ html > </ xsl:template > < xsl:template match = "name" > Name: < span style = "font-family:cursive;color:#ff0000" > < xsl:value-of select = "substring-before(.,' ')" /> </ span > < br /> </ xsl:template > < xsl:template match = "class" > class: < span style = "font-family:serif;color:#0ff000" > < xsl:value-of select = "." /> </ span > < br /> </ xsl:template > </ xsl:stylesheet > |
Output:
Example 2: In this example, we will see the ‘And’ and ‘Or’ operators. Save the file as mentioned and view the xsl file in the web browser to view the output.
XML
<!--File: Test.xml--> <? xml version = "1.0" encoding = "UTF-8" ?> <? xml-stylesheet type = "text/xsl " href = "Rule.xsl" ?> < student > < s > < name > Divyank Singh Sikarwar </ name > < branch >ME</ branch > < age >18</ age > </ s > < s > < name > Aniket Chauhan </ name > < branch >CE</ branch > < age >20</ age > </ s > < s > < name > Simran Agarwal</ name > < branch >CE</ branch > < age >23</ age > </ s > < s > < name >Abhay Chauhan</ name > < branch >CSE</ branch > < age >17</ age > </ s > < s > < name > Himanshu Bhatia</ name > < branch > IT</ branch > < age >25</ age > </ s > < s > < name >Sam Toppo</ name > < branch >CSE</ branch > < age >25</ age > </ s > < s > < name >Rahul Sahu</ name > < branch >XE</ branch > < age >25</ age > </ s > </ student > |
XML
<!--Rule.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 > < h1 align = "center" >Students' Basic Details</ h1 > < table border = "3" align = "center" > < tr > < th >Serial NO.</ th > < th >Name</ th > < th >Branch</ th > < th >Age</ th > </ tr > < xsl:for-each select = "student/s" > < tr > < xsl:if test="(age >= '18') and (branch='CE' or branch='ME')"> < td > < xsl:value-of select = "position()" /> </ td > < td > < xsl:apply-templates select = "name" /> </ td > < td > < xsl:apply-templates select = "branch" /> </ td > < td > < xsl:apply-templates select = "age" /> </ td > </ xsl:if > </ tr > </ xsl:for-each > </ table > </ body > </ html > </ xsl:template > < xsl:template match = "name" > < span style = "color:#ff0000" > < xsl:value-of select = "." /> </ span > < br /> </ xsl:template > < xsl:template match = "branch" > < span style = "color:#0ff000" > < xsl:value-of select = "." /> </ span > < br /> </ xsl:template > < xsl:template match = "age" > < span style = "color:#0000ff" > < xsl:value-of select = "." /> </ span > < br /> </ xsl:template > </ xsl:stylesheet > |
Output:
Contact Us