ตัวเลือก <xsl:apply-templates> ใน XSLT
การใช้งานและการประกาศ
element <xsl:apply-templates> สามารถประมวล template ให้กับ element ปัจจุบันหรือ child element ของ element ปัจจุบัน
ถ้าเราเพิ่ม attribute select ใน element <xsl:apply-templates> มันจะประมวลเฉพาะ child element ที่ตรงกับค่าของ attribute นี้ เราสามารถใช้ attribute select มากกว่านี้เพื่อกำหนดลำดับที่ประมวล child element
รูปแบบการใช้
<xsl:apply-templates select="expression" mode="name"> <!-- Content:(xsl:sort|xsl:with-param)* --> </xsl:apply-templates>
attribute
attribute | ค่า | รายละเอียด |
---|---|---|
select | การแสดง | ตัวเลือกตามความต้องการ กำหนด node ที่ต้องการประมวล ดาวดาบ (*) คือ node ทั้งหมด ถ้าละเลย attribute นี้ จะเลือกทุก child node ของ node ปัจจุบัน |
mode | ชื่อ | ตัวเลือกตามความต้องการ ถ้ามีหลายวิธีการการประมวลเดียวกันสำหรับ element เดียวกัน ใช้ mode ที่จะแยกต่างกัน |
ตัวอย่าง
ตัวอย่าง 1
ใช้ element h1 ล้อม element title ใน document
<?xml version="1.0" encoding="ISO-8859-1"?> <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
ใช้ element h1 ล้อม element title ทั้งหมดใน document ที่เป็น child element ของ message
<?xml version="1.0" encoding="ISO-8859-1"?> <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
ใช้ element h1 ล้อมรายการทั้งหมดที่มี attribute mode ใน message ทั้งหมด
<?xml version="1.0" encoding="ISO-8859-1"?> <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>