XPath Absolute Path
XPath-Absolute Path is the exact path specifying to reach the XML node. The absolute path starts with the root node or with the ‘/’ symbol. For the absolute path, we have to specify every parent node of the node we want to reach.
Syntax:
<xsl:TagName select = "/rootNode/node1/...">
Parameters:
- rootNode: It is the root node of the XML tree.
- node1: It is the child node of the root node. and this path goes on till we reach our desired node.
Example 1: In this example, we will use the absolute path to select the different nodes of students. Save the file as mentioned and view xsl file in we browser.
XML
<!-- File name- Text.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
<!-- File name - Rule.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 > < 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 > < tr > < td > < xsl:value-of select = "/student/s[1]/@studentId" /> </ td > < td > < xsl:value-of select = "/student/s[1]/name" /> </ td > < td > < xsl:value-of select = "/student/s[1]/branch" /> </ td > < td > < xsl:value-of select = "/student/s[1]/age" /> </ td > </ tr > < tr > < td > < xsl:value-of select = "/student/s[2]/@studentId" /> </ td > < td > < xsl:value-of select = "/student/s[2]/name" /> </ td > < td > < xsl:value-of select = "/student/s[2]/branch" /> </ td > < td > < xsl:value-of select = "/student/s[2]/age" /> </ td > </ tr > < tr > < td > < xsl:value-of select = "/student/s[3]/@studentId" /> </ td > < td > < xsl:value-of select = "/student/s[3]/name" /> </ td > < td > < xsl:value-of select = "/student/s[3]/branch" /> </ td > < td > < xsl:value-of select = "/student/s[3]/age" /> </ td > </ tr > </ table > </ body > </ html > </ xsl:template > </ xsl:stylesheet > |
Output:
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
<!-- File name Text.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
<!-- Rule.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 > < h1 align = "center" >Students' Basic Details</ h1 > < table border = "3" align = "center" > < tr > < th >Details</ th > </ tr > < tr > < td > < xsl:apply-templates select = "/student/s[1]/name" /> < xsl:apply-templates select = "/student/s[1]/branch" /> < xsl:apply-templates select = "/student/s[1]/age" /> </ td > </ tr > < tr > < td > < xsl:apply-templates select = "/student/s[2]/name" /> < xsl:apply-templates select = "/student/s[2]/branch" /> < xsl:apply-templates select = "/student/s[2]/age" /> </ td > </ tr > < tr > < td > < xsl:apply-templates select = "/student/s[3]/name" /> < xsl:apply-templates select = "/student/s[3]/branch" /> < xsl:apply-templates select = "/student/s[3]/age" /> </ td > </ tr > </ 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:
Contact Us