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.No. | Operators/Functions & Description |
---|---|
1 | Comparision Operators |
2 | Boolean Operators |
3 | Number Functions/Operators |
4 | String Functions |
5 | Node Functions/Operators |
1. XPath – Comparison Operators
XPath defines following comparison operators to be used with the XPath expressions.
S.No. | Operator & Description |
---|---|
1 | =is equals to |
2 | !=is not equals to |
3 | <is less than |
4 | >is greater than |
5 | <=is less than or equals to |
6 | >=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 = "/"> <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>
Verify the output
2. XPath – Boolean Operators
XPath defines the following Boolean operators to be used with the XPath expressions.
S.No. | Operator & Description |
---|---|
1 | andboth conditions to be satisfied |
2 | orany one of the condition to be satisfied |
3 | 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> <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))]"> <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
3. XPath – Number Operators / Functions
XPath defines the following operators on numbers to be used with the XPath expressions.
S.No. | Operator & Description |
---|---|
1 | +used for addition operation |
2 | –used for subtraction operation |
3 | *used for multiplication operation |
4 | divused for division operation |
5 | modused for modulo operation |
XPath defines the following functions on numbers to be used with the XPath expressions.
S.No. | Function & Description |
---|---|
1 | ceiling()returns the smallest integer larger than the value provided. |
2 | floor()returns the largest integer smaller than the value provided. |
3 | round()returns the rounded value to nearest integer. |
4 | 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> <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> <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>
Verify the output
4. XPath – String Functions
The following is a list of XPath String functions −
S.No. | 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. |
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> <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>
Verify the output
5. XPath – Node Functions
XPath defines the following operators on nodes to be used with the XPath expressions.
S.No. | Operator & Description |
---|---|
1 | /used to select node under a specific node. |
2 | //used to select node from root node |
3 | […]used to check node value |
4 | |used for union of two node sets |
XPath defines the following functions on nodes to be used with the XPath expressions.
S.No. | Function & Description |
---|---|
1 | comment()selects nodes which are comments. |
2 | node()selects all kinds of nodes. |
3 | processing-instruction()selects nodes which are processing instruction. |
4 | text()selects a text node. |
5 | name()provides the name of the node. |
6 | position()provides the position of the node. |
7 | 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
<?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>