Element <xsl:attribute> trong XSLT

Định nghĩa và cách sử dụng

Element <xsl:attribute> được sử dụng để thêm thuộc tính cho phần tử.

Chú ý:Element <xsl:attribute> sẽ thay thế thuộc tính có tên trùng khớp.

Cú pháp

<xsl:attribute name="attributename" namespace="uri">
  <!-- Nội dung:template -->
</xsl:attribute>

Thuộc tính

Thuộc tính Giá trị Mô tả
name attributename Bắt buộc. Định nghĩa tên thuộc tính.
namespace URI Tùy chọn. Định nghĩa URI không gian tên cho thuộc tính.

Mô hình

Ví dụ 1

Thêm thuộc tính source cho phần tử picture:

<picture>
  <xsl:attribute name="source"/>
</picture>

Ví dụ 2

Thêm thuộc tính source vào phần tử picture, và gán giá trị từ "images/name":

<picture>
  <xsl:attribute name="source">
    <xsl:value-of select="images/name" />
  </xsl:attribute>
</picture>

Ví dụ 3

Tạo một tập hợp thuộc tính có thể áp dụng cho bất kỳ phần tử đầu ra nào:

<xsl:attribute-set name="font">
  <xsl:attribute name="fname">Arial</xsl:attribute>
  <xsl:attribute name="size">14px</xsl:attribute>
  <xsl:attribute name="color">red</xsl:attribute>
</xsl:attribute-set>