XSLT <xsl: key>
❮ Toàn bộ tham chiếu phần tử XSLT
Định nghĩa và Cách sử dụng
Phần tử <xsl: key> là phần tử cấp cao nhất khai báo khóa được đặt tên có thể được sử dụng trong biểu định kiểu với hàm key ().
Lưu ý: Một khóa không nhất thiết phải là duy nhất!
Cú pháp
<xsl:key
name="name"
match="pattern"
use="expression"/>
Thuộc tính
Attribute | Value | Description |
---|---|---|
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 |
ví dụ 1
Giả sử bạn có một tệp XML có tên "people.xml":
<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>
Bạn có thể xác định khóa trong tệp XSL như sau:
<xsl:key name="preg" match="person" use="@id"/>
Để tìm người có id = "050676", hãy viết (trong tệp XSL):
<?xml version="1.0" encoding="UTF-8"?>
<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>
❮ Toàn bộ tham chiếu phần tử XSLT