XSLT <xsl:apply-templates>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์
ความหมายและการใช้งาน
องค์ประกอบ <xsl:apply-templates> ใช้เทมเพลตกับองค์ประกอบปัจจุบันหรือโหนดย่อยขององค์ประกอบปัจจุบัน
หากเราเพิ่มแอตทริบิวต์ select ให้กับองค์ประกอบ <xsl:apply-templates> จะประมวลผลเฉพาะองค์ประกอบย่อยที่ตรงกับค่าของแอตทริบิวต์ เราสามารถใช้แอตทริบิวต์ select เพื่อระบุลำดับที่โหนดย่อยจะถูกประมวลผล
ไวยากรณ์
<xsl:apply-templates select="expression" mode="name">
<!-- Content:(xsl:sort|xsl:with-param)* -->
</xsl:apply-templates>
คุณลักษณะ
Attribute | Value | Description |
---|---|---|
select | expression | Optional. Specifies the nodes to be processed. An asterisk selects the entire node-set. If this attribute is omitted, all child nodes of the current node will be selected |
mode | name | Optional. If there are multiple ways of processing defined for the same element, distinguishes among them |
ตัวอย่าง 1
ล้อมองค์ประกอบ h1 เดียวรอบๆ องค์ประกอบชื่อแต่ละองค์ประกอบในเอกสาร:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="title">
<h1><xsl:apply-templates/></h1>
</xsl:template>
</xsl:stylesheet>
ตัวอย่าง 2
ล้อมองค์ประกอบ h1 เดียวรอบองค์ประกอบชื่อทั้งหมดซึ่งเป็นลูกของข้อความ:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="message">
<h1><xsl:apply-templates select="title"/></h1>
</xsl:template>
</xsl:stylesheet>
ตัวอย่างที่ 3
ล้อมองค์ประกอบ h1 เดียวรอบโหนดย่อยของข้อความที่มีแอตทริบิวต์โหมดตั้งค่าเป็น "ใหญ่":
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="message">
<h1><xsl:apply-templates select="*" mode="big"/></h1>
</xsl:template>
</xsl:stylesheet>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์