Open In App

XPath Expression

Last Updated : 12 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The XPath Expression is an expression that is used to select nodes in the XML element. These expressions are similar to the path expression in our system files path. XSLT performs transformation based on this expression.

Following is the table of some useful expressions to select any node in the XML file:

S.No.

Expression

Description

1

[ . ]

Select the current node.

2

[ .. ]

Select the parent of the current node.

3

@attribute_name

Select attribute

4

/

Select starts from the root node.

5

//

Select starts from the current node with a specified pattern.

6

node_name

Select all the nodes with the name specified.

7

parent/child

Select all the children of a parent.

8

//child

Select all child matches without the consent of a parent.

Syntax:

"XPath_Expression"

Attributes: It is an XPath expression that is written inside the Double inverted commas.

Example 1: In this, we will select all the children of the student node using the expression parent/child and select the attribute of a node using @ Expression. Save the file as mentioned and view the XSL file in the web browser.

XML




<!--Test.xml-->
  
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s studentId="1001">
      <name> Divyank Singh Sikarwar </name>
      <branch>CSE</branch>
      <age>18</age>
   </s>
   <s studentId="1005">
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age> 20</age>
   </s>
   <s studentId="1010">
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
   </s>
   <s studentId="1015">
      <name> Abhay Chauhan</name>
      <branch> CSE</branch>
      <age> 17</age>
   </s>
   <s studentId="1012">
      <name> Himanshu Bhatia</name>
      <branch> IT</branch>
      <age>25</age>
   </s>
</student>


XML




<!--Rule.xsl-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
<html>
<body>
<h1 align="center">Students' Basic Details</h1>
<table border="3" align="center" >
<tr>
        <th>Stud.Id</th>
    <th>Name</th>
    <th>Branch</th>
    <th>Age</th>
</tr>
    <xsl:for-each select="student/s">
<tr
        <td>
        <span style="color:#00f000">
        <xsl:value-of select="@studentId"/></span>
        </td
    <td>
        <span style="color:#ff0000">
        <xsl:value-of select="name"/></span>
        </td>
    <td>
        <span style="color:#00ff00">
        <xsl:value-of select="branch"/></span>
          
</td>
          
        <td>
        <span style="color:#0000ff">
        <xsl:value-of select="age"/></span>
        </td>
          
</tr>
    </xsl:for-each>
    </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Output:

expression1

expression

Example 2: In this example, we will select the current node using [. ] and select start from the current node using / expression. Save the file as mentioned and view it in a web browser.

XML




<!--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> CSE</branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age> 20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch> CSE</branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch> IT</branch>
      <age> 25</age>
      <city> Indore</city>
   </s>
</student>


XML




<!--Rule.xsl-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <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>
                     <td>
                        <xsl:apply-templates select="name"/>
                        <xsl:apply-templates select="branch"/>
                        <xsl:apply-templates select="age"/>
                     </td>
                  </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="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="branch">
      Branch:
      <span style="font-family:serif;color:#0ff000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="age">
      Age: 
      <span style="font-family:fantsy;color:#0000ff">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
</xsl:stylesheet>


Output:

expression2

expression



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads