Open In App

XPath Relative Path

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

XPath-Relative Path is the relative path to the destination node. The relative path starts with the node that we select. For the relative path, we don’t have to start with the root node of the XML tree. This approach simplifies navigation within XML documents, making it easier to target specific nodes without specifying the entire path from the root node.

Syntax:

<xsl:TagName select = "node1/node2/...">

Parameters:

  • node1: It is the node of the XML tree from where we want to start.
  • node2: It is successive node to node1 and it continues till our destination node.

Example 1: In this example, we will use the relative path to select the different nodes of students. Save the file as mentioned and view xsl file in we browser.

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>
    </s>
    <s>
        <name> Aniket Chauhan </name>
        <branch> CSE</branch>
        <age> 20</age>
    </s>
    <s>
        <name> Simran Agarwal</name>
        <branch> CSE</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>
</student>


XML




<?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>Name</th>
                        <th>Branch</th>
                        <th>Age</th>
                    </tr>
                    <xsl:for-each select="student/s">
                        <tr>
                            <td>
                                <xsl:apply-templates select="name"/>
                            </td>
                            <td>
                                <xsl:apply-templates select="branch"/>
                            </td>
                            <td>
                                <xsl:apply-templates select="age"/>
                            </td>
                        </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:

relative

relative

Example 2: In this example we will select different node of XML tree and use apply template on nodes. Save the file as mentioned and run the xsl file in web browser.

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>
    </s>
    <s>
        <name> Aniket Chauhan </name>
        <branch> CSE</branch>
        <age> 20</age>
    </s>
    <s>
        <name> Simran Agarwal</name>
        <branch> CSE</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>
</student>


XML




<?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:

relative2

relative2

In the provided examples, we demonstrated how XPath relative paths can be employed to access and manipulate data within XML documents. Whether you are extracting information or applying templates, mastering relative paths is essential for effective XML processing.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads