Open In App

XSLT <key> Element

XSLT <key> Element is used to declare the key which is later used to identify the node with the help of the key function. The <xsl: key> tag assigns a specific key to an XML element And the key() function used to access this element of XML

Syntax:



<xsl:key 
   name = "name"
   match = "pattern" 
   use = "expression" /> 

Parameters:

Example 1: In this example, we will make a key to a branch and use it to select the node with specified key-value pairs. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:






<!--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>ME</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>CE</branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch>IT</branch>
      <age> 25</age>
      <city> Indore</city>
   </s>
</student>




<!--Rule.xsl-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:key name = "branch-search" match = "s" use = "branch"/>
   <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="key('branch-search','CSE')">
                  <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:

key element

Example 2: In this example, we will make a key to a name and use it to select all node with name Dhananjay. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:




<!--test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name>Dhananjay</name>
      <branch>CSE</branch>
      <age>18</age>
   </s>
   <s>
      <name>Aniket</name>
      <branch> CSE</branch>
      <age> 20</age>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
   </s>
   <s>
      <name>Abhay</name>
      <branch>CSE</branch>
      <age> 17</age>
   </s>
   <s>
      <name>Dhananjay</name>
      <branch> IT</branch>
      <age>25</age>
   </s>
   <s>
      <name>Abhay</name>
      <branch>CSE</branch>
      <age> 17</age>
   </s>
   <s>
      <name>Dhananjay</name>
      <branch>XE</branch>
      <age>21</age>
   </s>
</student>




<!--Rule.xsl-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:key name = "name-search" match = "s" use = "name"/>
   <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="key('name-search', 'Dhananjay')">
                  <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:

key element


Article Tags :