XML: include và import
Phần tử include
include làm việc giống như câu lệnh include trong một số ngôn ngữ lập trình (C, PHP, ...), tức là phần tử này có chức năng chèn đoạn của file xsl được chỉ ra trong thuộc tính href của phần tử include vào ngay phần tử include, có nghĩa là nó thực hiện phép thế.
Phần tử import
Phần tử này làm việc cũng giống như phần tử include, nhưng chúng ta cần lưu ý là phần tử import phải là phần tử con đầu tiên của phần tử stylesheet.
Ví dụ:
Tài liệu XSL lưu với tên test.xsl:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:import href = "xslt33.xslt" />
<xsl:output method = "text" />
<xsl:template match = "/" >
<xsl:apply-templates select = "//BBB" />
</xsl:template>
</xsl:stylesheet>
Tập tin xslt33.xslt:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:output method = "text" />
<xsl:template match = "BBB" >
<xsl:text > BBB[</xsl:text>
<xsl:value-of select = "position()" />
<xsl:text >]: </xsl:text>
<xsl:value-of select = "." />
</xsl:template>
</xsl:stylesheet>
Phần XML:
<?xml version=”1.0”?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<AAA >
<BBB>cc </BBB>
<BBB>ff </BBB>
<BBB>aa </BBB>
<BBB>fff </BBB>
<BBB>FFF </BBB>
<BBB>Aa </BBB>
<BBB>ccCCC </BBB>
</AAA>
Kết quả hiển thị trên trình duyệt:
BBB[1]: cc
BBB[2]: ff
BBB[3]: aa
BBB[4]: fff
BBB[5]: FFF
BBB[6]: Aa
BBB[7]: ccCCC