XPath Absolute Path

There are two types of location paths used to specify the location of node in XML documents. These paths are absolute or relative path.

An absolute path starts with root node or with '/'.

/company/employee- It will select employee nodes within class root node.

snippet
<xsl:for-each select = "/class/employee">

/company/employee/firstname? It will select firstname of an employee node within class root node.

snippet
<p><xsl:value-of select = "/class/employee/firstname"/></p>

XPath Absolute Path Example

Let's take an example locating the elements using absolute path. Create an XML document employee.xml and its stylesheet document employee.xsl which uses the XPath expressions.

Employee.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
   <employee id = "001">
      <firstname>Abhiram</firstname>
      <lastname>Kushwaha</lastname>
      <nickname>Manoj</nickname>
      <salary>>15000</salary>
   </employee>
   <employee id = "002">
      <firstname>Akash</firstname>
      <lastname>Singh</lastname>
      <nickname>Bunty</nickname>
      <salary>25000</salary>
   </employee>
    <employee id = "003">
      <firstname>Brijesh</firstname>
      <lastname>Kaushik</lastname>
      <nickname>Ballu</nickname>
      <salary>20000</salary>
   </employee>
    <employee id = "004">
      <firstname>Zoya</firstname>
      <lastname>Mansoori</lastname>
      <nickname>Sonam</nickname>
      <salary>30000</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>
            <h3>Details of each Employee. </h3>
            <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>						
               <tr>
                  <td><xsl:value-of select = "/class/employee[1]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/salary"/></td>		 
               </tr>					
               <tr>
                  <td><xsl:value-of select = "/class/employee[2]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/salary"/></td>		 
               </tr>		
                <tr>
                  <td><xsl:value-of select = "/class/employee[3]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/salary"/></td>		 
               </tr>		 
               <tr>
                  <td><xsl:value-of select = "/class/employee[4]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/salary"/></td>		 
               </tr>		
            </table>    
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

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