XSLT <xsl: apply-Template>
❮ Toàn bộ tham chiếu phần tử XSLT
Định nghĩa và Cách sử dụng
Phần tử <xsl: apply-Template> áp dụng một khuôn mẫu cho phần tử hiện tại hoặc cho các nút con của phần tử hiện tại.
Nếu chúng ta thêm một thuộc tính select vào phần tử <xsl: apply-templates> thì nó sẽ chỉ xử lý phần tử con khớp với giá trị của thuộc tính. Chúng ta có thể sử dụng thuộc tính select để chỉ định thứ tự xử lý các nút con.
Cú pháp
<xsl:apply-templates select="expression" mode="name">
<!-- Content:(xsl:sort|xsl:with-param)* -->
</xsl:apply-templates>
Thuộc tính
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 |
ví dụ 1
Quấn một phần tử h1 xung quanh mỗi phần tử tiêu đề trong tài liệu:
<?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>
Ví dụ 2
Bao bọc một phần tử h1 xung quanh tất cả các phần tử tiêu đề là phần tử con của thông báo:
<?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>
Ví dụ 3
Bao bọc một phần tử h1 xung quanh tất cả các nút con của thông báo có thuộc tính mode được đặt thành "lớn":
<?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>
❮ Toàn bộ tham chiếu phần tử XSLT