XSLT <xsl:key>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์
ความหมายและการใช้งาน
<xsl:key> องค์ประกอบเป็นองค์ประกอบระดับบนสุดซึ่งประกาศชื่อคีย์ที่สามารถใช้ในสไตล์ชีตด้วยฟังก์ชัน key()
หมายเหตุ:คีย์ไม่จำเป็นต้องไม่ซ้ำกัน!
ไวยากรณ์
<xsl:key
name="name"
match="pattern"
use="expression"/>
คุณลักษณะ
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 |
ตัวอย่าง 1
สมมติว่าคุณมีไฟล์ XML ชื่อ "persons.xml":
<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>
คุณสามารถกำหนดคีย์ในไฟล์ XSL ได้ดังนี้:
<xsl:key name="preg" match="person" use="@id"/>
หากต้องการค้นหาบุคคลที่มี id="050676" ให้เขียน (ในไฟล์ 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>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์