当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT <xsl:key> 元素
The <xsl:key> element is a top-level element which declares a named key that can be used in the style sheet with the key() function.
<xsl:key> 元素是一个顶级元素,它是用来声明一个指定的键[key]的。该键所对应的函数是key()函数,它可以被用在样式表中。
Note: A key does not have to be unique!
注意:键[key]不具有唯一性。
<xsl:key name="name" match="pattern" use="expression"/> |
属性 | 值 | 描述 |
---|---|---|
name | name | Required. Specifies the name of the key 必要参数。指定键名称 |
match | pattern | Required. Defines the nodes to which the key will be applied 必要参数。定义键所应用的节点(即:该键对哪个节点起作用) |
use | expression | Required. The value of the key for each of the nodes 必要参数。为每个节点指定键值 |
Suppose you have an XML file called "persons.xml":
假设你创建了一个名为 "persons.xml"的XML文件:
<persons> <person name="Tarzan" id="050676"/> <person name="Donald" id="070754"/> <person name="Dolly" id="231256"/> </persons> |
You can define a key in an XSL file like this:
你可以像下面一样给XSL文件定义一个键:
<xsl:key name="preg" match="person" use="@id"/> |
To find the person with id="050676", write (in the XSL file):
查找XSL文件中id=="050676"的人:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="preg" match="person" use="@id"/> <xsl:template match="/"> <html> <body> <xsl:for-each select="key('preg','050676')"> <p> Id: <xsl:value-of select="@id"/><br /> Name: <xsl:value-of select="@name"/> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> |