XPath Expression

XPath defines a pattern or path expression to select nodes or node sets in an XML document. These patterns are used by XSLT to perform transformations. The path expressions look like very similar to the general expressions we used in traditional file system.

Xpath Expression 1

XPath specifies seven types of nodes that can be output of the execution of the XPath expression.

  • Root
  • Element
  • Text
  • Attribute
  • Comment
  • Processing Instruction
  • Namespace

We know that XPath uses a path expression to select node or a list of nodes from an XML document.

A list of useful paths and expression to select any node/ list of nodes from an XML document:

Index Expression Description
1) node-name It is used to select all nodes with the given name "nodename"
2) / It specifies that selection starts from the root node.
3) // It specifies that selection starts from the current node that match the selection.
4) . Select the current node.
5) .. Select the parent of the current node.
6) @ Selects attributes.
7) student Example - selects all nodes with the name "student".
8) class/student Example - selects all student elements that are children of class
9) //student Selects all student elements no matter where they are in the document

XPath Expression Example

Let's take an example to see the usage of XPath expression. Here, we use an xml file "employee.xml" and a stylesheet for that xml file named "employee.xsl". The XSL file uses the XPath expressions under select attribute of various XSL tags to fetchvalues of id, firstname, lastname, nickname andsalary of each employee node.

Employee.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>

Employee.xsl

snippet
<?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> Employees</h2> 
            <table border = "1> 
               <tr bgcolor = "pink"> 
                  <th> ID</th>
                  <th> First Name</th> 
                  <th> Last Name</th> 
                  <th> Nick Name</th> 
                  <th> Salary</th> 
               </tr> 
               <xsl:for-each select = "class/employee"> 
                  <tr>
                     <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 = "nickname"/> </td> 
                     <td> <xsl:value-of select = "salary"/> </td> 
                  </tr> 
               </xsl:for-each> 
            </table> 
         </body> 
      </html> 
   </xsl:template> 
</xsl:stylesheet>

Output:

Xpath Expression
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +