<xsl:key> |
Syntax: |
<xsl:key match=”pattern” name=”qname” use=”expression” > </xsl:key> |
The xsl:key element is used to declare a named key that can be used by the key function in expressions and patterns. The key function has two arguments: the key name and the value (consider this to be a key-value pair). The appropriate use of these key-value pairs can permit easy access to information in complex XML documents. |
A key does not have to be unique. Further, a key can refer to more than one node and a node can have more than one key. |
There is no limit to the number of the xsl:key elements that can occur. However, each xsl:key element can only be a child of the xsl:stylesheet or the xsl:transform elements. It cannot contain any elements as content, nor can it appear inside a template. |
The xsl:key element alerts the XSLT processor to create an indexed data structure for the key expressions ahead of time. This indexed data structure will be used by the key function. If there are no xsl:key elements, then the time-consuming indexing is not performed. |
This is not a self-closing element. The separate closing element is mandatory.
match=”pattern” |
The mandatory match attribute defines the nodes to which the key will be applied.
name=”qname” |
The mandatory name attribute is a qname that identifies the key. A qname is a qualified name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode).
use=”expression” |
The mandatory use attribute is an expression or string that is used to determine the value of the key for each node. |
Example below searches for members of Java Department and display the records. |
Source Code for xsl_key.xsl: |
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE xsl:stylesheet[ <!ENTITY nbsp " "> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="iso-8859-1" /> <xsl:variable name="tbl"> <tr> <td><b>Name</b></td> <td><b>Address</b></td> <td><b>Department</b></td> <td><b>Designation</b></td> </tr> </xsl:variable> <xsl:attribute-set name="tblAttrib"> <xsl:attribute name="border"> 2 </xsl:attribute> <xsl:attribute name="cellspacing"> 3 </xsl:attribute> <xsl:attribute name="bordercolor"> "#3344dd" </xsl:attribute> </xsl:attribute-set> |
<xsl:key name="emplist" match="employee_details" use="department-sub" /> <xsl:template match="/"> <csitquestion> <head> <title>XSLT xsl:choose Example</title> </head> <body> <span style="width 600px;border: 2px outset blue solidl;display:block"> </span><br /> <table xsl:use-attribute-sets="tblAttrib"> <xsl:copy-of select="$tbl" /> <xsl:for-each select="csitquestion/employee_details"> <xsl:sort data-type="number" order="descending" select="age" /> <tr> <td><xsl:value-of select="fname" /> < xsl:value-of select="lname" /></td> <td><xsl:value-of select="address" /></td> <xsl:choose> <xsl:when test="department-sub ='Java'"> <td style="background-color:pink"> <xsl:value-of select="department" /><xsl:text> [</xsl:text><xsl:value-of select="department-sub" /> <xsl:text>]</xsl:text></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="department" /> </td> |
</xsl:otherwise> </xsl:choose> <td><xsl:value-of select="designation" /></td> </tr> </xsl:for-each> </table> <hr /> <br /> <span style="background-color:pink; width:100%;margin: 0px 0px 0px 0px"> <xsl:text>Using Key-Value pair to access Data of Java Team members </xsl:text> </span> <br /> <div style="background-color: #FFCC99; border: solid 2px groove #663333;width:60%; padding-top: 10px;padding-left:5px"> <table xsl:use-attribute-sets="tblAttrib"> <xsl:copy-of select="$tbl" /> <xsl:for-each select="key('emplist','Java')" > <tr> <td><xsl:value-of select="fname" /> <xsl:value-of select="lname" /></td> <td><xsl:value-of select="address" /></td> <td><xsl:value-of select="department" /></td> <td><xsl:value-of select="designation" /></td> </tr> </xsl:for-each> </table> </div> </body> </csitquestion> </xsl:template> </xsl:stylesheet> |
<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl" href="xsl_key.xsl"?> <ebiz> <employee_details> <emp_id>csitquestion</emp_id> <fname>Mr Abhishek kr</fname> <lname>Agrahari</lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Sr. Developer</designation> <phone> 32942</phone> <address>Sector 44</address> <age>27</age> </employee_details> <employee_details> <emp_id>CSIT1</emp_id> <fname>Shalu</fname> <lname>Jain</lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Jr. Programmer</designation> |
<phone> 32942</phone> <address>Sector 66, Noida</address> <age>23</age> </employee_details> <employee_details> <emp_id>CSIT2</emp_id> <fname>Mr Alok</fname> <lname>Kumar</lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Sr. Developer</designation> <phone>(+91)99990</phone> <address>Sector 56, Rohni</address> <age>23</age> </employee_details> <employee_details> <emp_id>CSIT3</emp_id> <fname>Mrs Sunita</fname> <lname>Singhania</lname> <department>Custome r Support[Web Assist]</department> |
<department-sub>NA</department-sub> <designation>CCE</designation> <phone> 000000</phone> <address>Sector 66, Gurgaon</address> <age>20</age> </employee_details> <employee_details> <emp_id>CSIT11</emp_id> <fname>Miss Amisha</fname> <lname>Mishra</lname> <department>Accounts</department> <department-sub>NA</department-sub> <designation>Jr. Accountant</designation> <phone> 32942</phone> <address>Sector 66, Rohni</address> <age>26</age> </employee_details> <employee_details> <emp_id>CSITC002</emp_id> <fname>ANITA</fname> <lname>Mishra</lname> <department>Accounts</department> |
<department-sub>NA</department-sub> <designation>Jr. Accountant</designation> <phone> 09999 </phone> <address>Sector 66, Noida</address> <age>23</age> </employee_details> <employee_details> <emp_id>CSIT03</emp_id> <fname>ANKITA</fname> <lname>DUBEY</lname> <department>Accounts</department> <department-sub>NA</department-sub> <designation>Jr. Accountant</designation> <phone>032942 </phone> <address>Sector 99, Noida</address> <age>21</age> </employee_details> <employee_details> <emp_id>CSIT004</emp_id> <fname>AKSHITA</fname> <lname>Jaiswal </lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Jr. DEVELOPER</designation> <phone> (+91)99990</phone> |
<address>Sector 66, Noida</address> <age>22</age> </employee_details> <employee_details> <emp_id>CSIT302</emp_id> <fname>Shweta</fname> <lname>Agrwal</lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Jr. Programmer</designation> <phone> 32942</phone> <address>Sector 66, Noida</address> <age>24</age> </employee_details> <employee_details> <emp_id>CSIT3003</emp_id> <fname>Shalu</fname> <lname>Jain</lname> <department>TECHNICAL</department> <department-sub>Java</department-sub> <designation>Jr. Programmer</designation> <phone> 32942</phone> <address>Sector 66, Noida</address> <age>23</age> </employee_details> </csitquestion>
|