提到「樣式表」,你可能第一時間想到的是 CSS。但 XSLT 樣式表同樣能做不少有趣的事。它通常用於 XML 轉換,例如在 Web 服務之間映射資料;正因為它在這類任務上表現出色,官方甚至為頂層元素 <stylesheet> 提供了 <xsl:transform> 這個別名。
與一般的 XSLT 轉換不同,這裡的輸入結構與輸出結構相當相似,只是更為簡單——大部分標記會原樣複製到輸出,輸入與輸出的命名空間也相同(都是 HTML)。本文將示範如何用 XSLT 樣式表來擴充 XHTML 文件。
先列出幾個常見縮寫:
XSLT 並不會取代 CSS 或 JavaScript。CSS 擅長處理字型、粗體、顏色、間距等外觀,卻不適合把分散在不同位置的信息組合起來——例如生成腳註、模組或目錄。這正是 XSLT 的用武之地:它補充而非替代 CSS。
為簡便起見,本文各範例位於獨立的 XSLT 檔中,並共用一份必要程式碼(位於 samples/common.XML):
<s:stylesheet
xmlns="https://www.w3.org/1999/xhtml"
xmlns:h="https://www.w3.org/1999/xhtml"
xmlns:a="https://sourceforge.net/projects/arbalo/"
xmlns:s="https://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="a h"
version="1.0">
<s:template match="h:head">
<s:copy>
<s:apply-templates select="@*|node()"/>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link href="common.css" rel="stylesheet" type="text/css" />
</s:copy>
</s:template>
<s:template match="*">
<s:copy>
<s:copy-of select="@*"/>
<s:apply-templates/>
<s:copy>
</s:template>
</s:stylesheet>
這裡 XHTML 命名空間定義了兩次:默認定義與 h:。默認命名空間用於撰寫輸出的 XHTML 標記(應避免使用前綴),h: 則用於 XPath 表達式。其中 s:template match="head" 擴充了源文件的 head,加入一份 CSS 連結;s:template match="*" 則是默認的「完整複製」模板,原則上會把內容原封不動地搬到目標文件。
本文採用 XSLT 1.0,因為目前大多數瀏覽器還無法解釋 XSLT 2.0。若 XSLT 運行於伺服器端,則可考慮 2.0,它額外提供了 XPath 2.0(含 if…then…else 與眾多內建函式)、使用者自訂的 XPath 函式,以及分組功能。
所謂「擴充」,是指加入源文件未明確要求的特性。例如向每個內部連結加上一個小箭頭(^ 或 v),提示目標在連結之前還是之後:
<s:stylesheet
xmlns="https://www.w3.org/1999/xhtml"
xmlns:h="https://www.w3.org/1999/xhtml"
xmlns:s="https://www.w3.org/1999/XSL/Transform"
version="1.0">
<s:import href="common.xsl"/>
<s:template match="h:a[starts-with(@href,'#')]">
<s:copy>
<s:copy-of select="@*"/>
<s:variable name="name" select="substring-after(@href,'#')"/>
<s:choose>
<s:when test="preceding::h:a[@name=$name]">
<s:text>^</s:text>
</s:when>
<s:when test="following::h:a[@name=$name]">
<s:text>v</s:text>
</s:when>
</s:choose>
<s:apply-templates/>
<s:copy>
</s:template>
</s:stylesheet>
這個樣式表先導入通用樣式,接著讓模板匹配以 # 開頭的內部連結:若連結指向的錨點位於它之前,就用向上箭頭擴充;反之則用向下箭頭。s:copy-of 與 s:apply-templates 確保內容不會在中途遺失。
你也可以在源文件中加入指令,告訴樣式表執行特定操作。這些指令屬於另一個命名空間(此處前綴為 a:),不會被複製到目標文件。例如,源文件中任何位置的 a:ref 指令都會生成一個腳註:
<s:stylesheet
xmlns="https://www.w3.org/1999/xhtml"
xmlns:a="https://sourceforge.net/projects/arbalo/"
xmlns:h="https://www.w3.org/1999/xhtml"
xmlns:s="https://www.w3.org/1999/XSL/Transform"
version="1.0">
<s:import href="common.xsl"/>
<s:template match="h:body">
<s:copy>
<s:apply-templates select="@*|node()"/>
<!-- 若無 a:references 指令,則把腳註放在末尾 -->
<s:if test="not(descendant::a:references)">
<s:call-template name="references"/>
</s:if>
</s:copy>
</s:template>
<!-- 建立一個腳註 -->
<s:template match="a:ref">
<s:variable name="number" select="count(preceding::a:ref) + 1"/>
<a name="ref-{$number}"></a>
<a class="footnote" href="#reference-{$number}">
<s:value-of select="concat('v ',$number)"/>
</a>
</s:template>
<!-- 若缺少 a:references,則預設放在 body 末尾 -->
<s:template match="a:references" name="references">
<hr/>
<s:for-each select="//a:ref">
<s:variable name="number" select="count(preceding::a:ref) + 1"/>
<p>
<a name="reference-{$number}"></a>
<a class="footnote" href="#ref-{$number}">
<s:value-of select="concat(' ^',$number)"/>
</a>
<s:apply-templates/>
</p>
</s:for-each>
</s:template>
</s:stylesheet>
使用源文件中的 a:references 指令時,名為 references 的模板會在該指令處配置腳註;若缺少該指令,第一個與 body 匹配的模板則會在 body 末尾調用同樣的模板來配置。兩種情況下,都會列出腳註內容並生成向上連結,而匹配 a:ref 的第二個模板則以向下箭頭建立腳註連結。
XSLT 還能引用其他源文件的部分內容。a:include 指令會包含一個屬於另一個源文件的元素並加以轉換:
<s:stylesheet
xmlns="https://www.w3.org/1999/xhtml"
xmlns:a="https://sourceforge.net/projects/arbalo/"
xmlns:s="https://www.w3.org/1999/XSL/Transform"
version="1.0">
<s:import href="common.xsl"/>
<s:template match="a:include">
<s:choose>
<s:when test="0!=string-length(@src)">
<s:apply-templates
select="document(@src)//*[@id=current()/@refid]"/>
</s:when>
<s:when test="not(@src) and //a:default[1]/@src">
<s:apply-templates
select="document(//a:default[1]/@src)//*[@id=current()/@refid]"/>
</s:when>
<s:when test="0=string-length(@src) or not(//a:default[1]/@src)">
<s:apply-templates
select="//*[@id=current()/@refid]"/>
</s:when>
</s:choose>
</s:template>
</s:stylesheet>
源文件中的 a:include 指令會引用某個元素的 id。被包含元素所在的文件可在 src 屬性中指定;若缺少該屬性,則改用 a:default 指令的 src;若任何地方都沒有 src,便使用同一份源文件。refid 引用 id 的做法,則是為了避免無限遞迴。
「解釋」是指頁面為 XML 格式(副檔名 .xml),並由瀏覽器執行處理指令所引用的 XSLT 樣式表;「編譯」則是指瀏覽器看到的是在請求前就轉換好的 HTML(副檔名 .html)。Xalan 與 Saxon 都是知名的 XSLT 處理器。
解釋被視為未來的方向,因為所有現代瀏覽器都支援 XSLT,且具備幾項優點:測試時按下 F5 即可立即反映源文件、CSS 與 XSLT 的更動;傳遞給客戶端的資訊量更少;客戶端看到的是乾淨、尚未擴充的網頁。但也需注意其缺點:部分舊瀏覽器不支援 XSLT;某些瀏覽器禁止樣式表跨目錄引用;與 SVG 或 iframe 等功能搭配時可能出問題;由於多數瀏覽器尚不支援 XSLT 2.0/3.0,你無法使用 if() then…else 等新功能。
無論選擇編譯還是解釋,頁面的其他轉換(CSS、JavaScript)都會在 XSLT 轉換之後執行。本文的範例可作為你打造自有 XSLT 樣式表的起點。