Xslt 简明教程

XSLT <key>

<xsl:key> 标记元素指定分配给 XML 文档中特定元素的已命名名称-值对。此键与 XPath 表达式中的 key() 函数一起使用,以便访问 XML 文档中的已分配元素。

<xsl:key> tag element specifies a named name-value pair assigned to a specific element in an XML document. This key is used with the key() function in XPath expressions to access the assigned elements in an XML document.

Declaration

以下是 <xsl:key> 元素的语法声明。

Following is the syntax declaration of <xsl:key> element.

<xsl:key
   name = QName
   match = Pattern
   use = Expression >
</xsl:key>

Attributes

S.No

Name & Description

1

Name Name of the key to be used.

2

Match Patterns used to identify a node that holds this key.

3

Use XPath expression to identify the value of the nodes of xml document.

Elements

Number of Occurrences

Unlimited

Parent elements

xsl:stylesheet

Child elements

None

Demo Example

此示例通过遍历每个学生来创建一个 <student> 元素的表格,其中包含其属性 rollno 及其子项 <firstname>、<lastname>、<nickname> 和 <marks>。它检查键作为学生的姓名之一,然后打印学生详细信息。

This example creates a table of <student> element with its attribute rollno and its child <firstname>, <lastname>, <nickname>, and <marks> by iterating over each student. It checks key as firstname to be one of the student’s name and then prints the student details.

students.xml

students.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

students.xsl

students.xsl

<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   <xsl:key name = "firstname-search" match = "student" use = "firstname"/>
   <xsl:template match = "/">
      <html>
         <body>
            <h2>Students</h2>
            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>Roll No</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Marks</th>
               </tr>

               <xsl:for-each select = "key('firstname-search', 'Dinkar')">

                  <tr>
                     <td><xsl:value-of select = "@rollno"/></td>
                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "nickname"/></td>
                     <td><xsl:value-of select = "marks"/></td>
                  </tr>

               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output

xlst students key