DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
把 XSLT 當成 HTML 樣式表:擴充、指令與跨文件組合的實務解析
編輯:布局實例     

聽到「樣式表」,多數人第一時間想到的是 CSS。但 XSLT 樣式表其實也能扮演類似角色——它最常被用於 XML 轉換,例如在 Web 服務之間映射資料。正因為太適合這類工作,XSLT 甚至提供了 <xsl:transform> 作為頂層元素 <stylesheet> 的別名(雖然很少有人用)。傳統 XSLT 轉換的輸入與輸出結構差異極大,最明顯的就是命名空間不同。

而當 XSLT 用來處理 HTML 時,情況反過來:輸入與輸出結構相近,只是輸入多了一些額外標記,其餘大部分原樣複製到輸出;兩者的命名空間相同(HTML);輸入文件還可以包含屬於其他命名空間的指令,這些指令不會被帶進輸出。

本文使用的縮寫:
CSS:層疊樣式表
XHTML:可擴充超文本標記語言
XPath:XML 路徑語言
XSLT:可擴充樣式表語言轉換

CSS 做不到的事,正好是 XSLT 的舞臺

XSLT 不會取代 JavaScript 或 CSS。字體、粗細、顏色、間距交給 CSS 就好;但若要組合來自不同位置的資訊——例如生成腳注、插入共用模組、自動產生目錄——CSS 就無能為力了。這是互補而非替代的關係。

基礎樣式表

為求示範清晰,本文各範例分別放在獨立檔案中。清單 1 是所有範例共用的基礎程式碼。

清單 1. 基礎樣式(samples/common.xml)

XML/HTML Code復制內容到剪貼板

  1. <s:stylesheet
  2.   xmlns="https://www.w3.org/1999/xhtml"
  3.   xmlns:h="https://www.w3.org/1999/xhtml"
  4.   xmlns:a="https://sourceforge.net/projects/arbalo/"
  5.   xmlns:s="https://www.w3.org/1999/XSL/Transform"
  6.   exclude-result-prefixes="a h"
  7.   version="1.0">
  8. <s:template match="h:head">
  9.   <s:copy>
  10.     <s:apply-templates select="@*|node()"/>
  11.     <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  12.     <link href="common.css" rel="stylesheet" type="text/css" />
  13.   </s:copy>
  14. </s:template>
  15. <s:template match="*">
  16.   <s:copy>
  17.     <s:copy-of select="@*"/>
  18.     <s:apply-templates/>
  19.   </s:copy>
  20. </s:template>
  21. </s:stylesheet>

XHTML 的命名空間宣告了兩次:一個是預設命名空間,用於輸出時不必寫前綴;另一個是 h:,用在 XPath 表達式中比對節點。

兩個模板的分工要記清楚:match="h:head" 在源文件的 head 中插入 CSS 連結與 content-type 宣告(即使 UTF-8 是 XML 預設編碼,部分瀏覽器仍需 content-type 才肯正確呈現);match="*" 則是預設的「完整複製」模板,把所有內容原封不動搬進目標文件。如果少了後者,輸出將只剩標記裡的文字,處理指令節點也不會被複製。

本文使用 XSLT 1.0。目前多數瀏覽器仍無法解釋 XSLT 2.0,但若轉換發生在伺服器端,2.0 會是相當實用的選擇,它額外提供了 XPath 2.0(含 if…then…else 與大量內建函數)、使用者自訂 XPath 函數,以及分組能力。

擴充:加上源文件未要求的內容

清單 1 裡插入 CSS 連結就是一種擴充。再看一個例子:為每個內部連結加上小箭頭,提示目標在當前位置之前(^)還是之後(v)。

清單 2. 樣式表(samples/linkUpDown.xsl)

XML/HTML Code復制內容到剪貼板

  1. <s:stylesheet
  2.   xmlns="https://www.w3.org/1999/xhtml"
  3.   xmlns:h="https://www.w3.org/1999/xhtml"
  4.   xmlns:s="https://www.w3.org/1999/XSL/Transform"
  5.   version="1.0">
  6.   <s:import href="common.xsl"/>
  7.   <s:template match="h:a[starts-with(@href,'#')]">
  8.     <s:copy>
  9.       <s:copy-of select="@*"/>
  10.       <s:variable name="name" select="substring-after(@href,'#')"/>
  11.       <s:choose>
  12.         <s:when test="preceding::h:a[@name=$name]">
  13.           <s:text>^</s:text>
  14.         </s:when>
  15.         <s:when test="following::h:a[@name=$name]">
  16.           <s:text>v</s:text>
  17.         </s:when>
  18.       </s:choose>
  19.       <s:apply-templates/>
  20.     </s:copy>
  21.   </s:template>
  22. </s:stylesheet>

先匯入共用樣式,接著比對所有以「#」開頭的內部連結。若錨點位於連結之前,補上向上箭頭;反之補上向下箭頭。s:copy-of 與 s:apply-templates 確保屬性與子節點都不會在半路被丟掉。

清單 3. 源文件(samples/linkUpDown.xml)

XML/HTML Code復制內容到剪貼板

  1. <?xml-stylesheet href="linkUpDown.xsl" type="text/xsl"?>
  2. <html xmlns="https://www.w3.org/1999/xhtml">
  3.   <head/>
  4.   <body>
  5.     <a name="a"/>
  6.     <p>This link goes <a href="#b">downward.</a></p>
  7.     <a name="b"/>
  8.     <p>This link goes <a href="#a">upward.</a></p>
  9.   </body>
  10. </html>

清單 4. 輸出文件(samples/linkUpDown.html)

XML/HTML Code復制內容到剪貼板

  1. … <a href="#b">v downward.</a> …
  2. … <a href="#a">^ upward.</a> …

指令:讓源文件指揮樣式表

你可以在源文件中放入屬於另一個命名空間(此例前綴為 a:)的指令,這些元素不會被複製到輸出。清單 5 中,任何位置的 a:ref 都會生成一個腳注。

清單 5. 樣式表(samples/footnote.xsl)

XML/HTML Code復制內容到剪貼板

  1. <s:stylesheet
  2.   xmlns="https://www.w3.org/1999/xhtml"
  3.   xmlns:a="https://sourceforge.net/projects/arbalo/"
  4.   xmlns:h="https://www.w3.org/1999/xhtml"
  5.   xmlns:s="https://www.w3.org/1999/XSL/Transform"
  6.   version="1.0">
  7.   <s:import href="common.xsl"/>
  8.   <s:template match="h:body">
  9.     <s:copy>
  10.       <s:apply-templates select="@*|node()"/>
  11.       <!-- 若無 a:references 指令,就把腳注放在 body 末尾 -->
  12.       <s:if test="not(descendant::a:references)">
  13.         <s:call-template name="references"/>
  14.       </s:if>
  15.     </s:copy>
  16.   </s:template>
  17.   <!-- 生成一個腳注連結 -->
  18.   <s:template match="a:ref">
  19.     <s:variable name="number" select="count(preceding::a:ref) + 1"/>
  20.     <a name="ref-{$number}"></a>
  21.     <a class="footnote" href="#reference-{$number}">
  22.       <s:value-of select="concat('v ',$number)"/>
  23.     </a>
  24.   </s:template>
  25.   <!-- 若缺少 a:references,預設放在 body 結尾 -->
  26.   <s:template match="a:references" name="references">
  27.     <hr/>
  28.     <s:for-each select="//a:ref">
  29.       <s:variable name="number" select="count(preceding::a:ref) + 1"/>
  30.       <p>
  31.         <a name="reference-{$number}"></a>
  32.         <a class="footnote" href="#ref-{$number}">
  33.           <s:value-of select="concat(' ^',$number)"/>
  34.         </a>
  35.         <s:apply-templates/>
  36.       </p>
  37.     </s:for-each>
  38.   </s:template>
  39. </s:stylesheet>

邏輯是這樣的:若源文件寫了 a:references,名為 references 的模板會在該位置列出所有腳注;若沒寫,與 body 匹配的模板會在 body 結尾呼叫同一個模板。兩種情況下都會列出腳注內容,並產生向上箭頭的回跳連結。第二個模板(比對 a:ref)則負責在內文中產生帶編號的向下箭頭連結。

輸出中的 class="footnote" 由 common.xsl 裡連結的 CSS 樣式表接手處理——這正是 XSLT 與 CSS 分工互補的寫照。

清單 6. 源文件(samples/footnote.xml)

XML/HTML Code復制內容到剪貼板

  1. <?xml-stylesheet href="footnote.xsl" type="text/xsl"?>
  2. <html xmlns="https://www.w3.org/1999/xhtml"
  3.       xmlns:a="https://sourceforge.net/projects/arbalo/">
  4.   <head/>
  5.   <body>
  6.     <p>
  7.       This example looks a little scientific
  8.       <a:ref>From Latin <em>scientia</em></a:ref>
  9.       and academic<a:ref>From Greek akademia</a:ref>.
  10.     </p>
  11.     <p>
  12.       Do you know why?
  13.       <a:ref>It uses <em>footnotes</em>.</a:ref>
  14.     </p>
  15.   </body>
  16. </html>

清單 7. 輸出文件(samples/footnote.html,節錄)

XML/HTML Code復制內容到剪貼板

  1. <p>This example looks a little scientific
  2. <a name="ref-1"/><a href="#reference-1" class="footnote">v 1</a>
  3. and academic.
  4. <a name="ref-2"/><a href="#reference-2" class="footnote">v 2</a></p>
  5. <hr/>
  6. <p><a name="reference-1"/><a href="#ref-1" class="footnote"> ^1</a> From Latin <em>scientia</em></p>

跨越源文件邊界

XSLT 也能引用其他文件的片段。a:include 指令會抓取某個 id 對應的元素並就地轉換。

清單 8. 樣式表(samples/include.xsl)

XML/HTML Code復制內容到剪貼板

  1. <s:stylesheet
  2.   xmlns="https://www.w3.org/1999/xhtml"
  3.   xmlns:a="https://sourceforge.net/projects/arbalo/"
  4.   xmlns:s="https://www.w3.org/1999/XSL/Transform"
  5.   version="1.0">
  6.   <s:import href="common.xsl"/>
  7.   <s:template match="a:include">
  8.     <s:choose>
  9.       <s:when test="0!=string-length(@src)">
  10.         <s:apply-templates
  11.           select="document(@src)//*[@id=current()/@refid]"/>
  12.       </s:when>
  13.       <s:when test="not(@src) and //a:default[1]/@src">
  14.         <s:apply-templates
  15.           select="document(//a:default[1]/@src)//*[@id=current()/@refid]"/>
  16.       </s:when>
  17.       <s:when test="0=string-length(@src) or not(//a:default[1]/@src)">
  18.         <s:apply-templates select="//*[@id=current()/@refid]"/>
  19.       </s:when>
  20.     </s:choose>
  21.   </s:template>
  22. </s:stylesheet>

三個分支的優先順序很清楚:先看 a:include 自帶的 src;若無,退回 a:default 的 src;兩者皆無則在當前文件內查找。用 refid 去比對 id,可以避開自我遞迴造成的無限迴圈。

清單 9. 源文件(samples/include.xml,節錄)

XML/HTML Code復制內容到剪貼板

  1. <head><a:default src="includedY.xml"/></head>
  2. <body>
  3.   <p>The following text is included:</p>
  4.   <a:include refid="x" src="includedX.xml"/>
  5.   <a:include refid="y1"/>
  6.   <p id="i">double</p>
  7.   <a:include refid="y2"/>
  8.   <a:include refid="i" src=""/>
  9. </body>

清單 10. 被引用的片段(samples/includeY.xml)

XML/HTML Code復制內容到剪貼板

  1. <h2 id="y2">I'm the <em>included</em> h2</h2>
  2. <h1 id="y1">I'm the <em>included</em> h1</h1>

清單 11. 輸出文件(samples/include.html,節錄)

XML/HTML Code復制內容到剪貼板

  1. <p id="x">I'm the <em>included</em> paragraph.</p>
  2. <h1 id="y1">I'm the <em>included</em> h1</h1>
  3. <p id="i">double</p>
  4. <h2 id="y2">I'm the <em>included</em> h2</h2>

被引入的元素可以具備複雜結構,套用模板後一樣能被完整轉換。

主文件與導航

若你的簡報由多頁組成,可以用一個主文件統籌頁面標題與連結,自動生成完整的頁面導航,包含上一頁、下一頁與任意跳轉。這部分細節超出本文範圍,但概念與上面的 include 機制一脉相承。

解釋與編譯的差別

解釋:頁面以 XML 形式提供(副檔名 .xml,content-type 為 text/xml 或 application/xml),由瀏覽器讀取處理指令並就地執行 XSLT。

編譯:瀏覽器收到的是已轉換好的 HTML(副檔名 .html,content-type 為 text/html),轉換發生在請求之前,於開發環境或伺服器上完成。Xalan 與 Saxon 都是常見的 XSLT 處理器。

解釋式的優點:

  • 修改源文件、CSS 或 XSLT 後,按 F5 就能看到結果,除錯回饋極快。
  • 傳輸到客戶端的資料量較小。
  • 客戶端看到的是乾淨的頁面,擴充內容尚未生成。

缺點同樣要正視:

  • 少數舊瀏覽器不支援 XSLT;封閉環境(內部網路)不受此限。
  • 部分瀏覽器禁止 XSLT 引用另一目錄下的樣式表。
  • 與 SVG、iframe 等技術混用時,某些瀏覽器可能出問題。
  • 瀏覽器普遍只支援 XSLT 1.0,無法使用 XPath 2.0 的條件式與自訂函數。

無論走哪條路,記住順序:XSLT 轉換先發生,之後才轮到 CSS 與 JavaScript 接手。

結語

XSLT 樣式表能把「結構生成」這件事從靜態 HTML 中抽離出來,讓擴充、腳注、跨文件組合成為宣告式的規則。把本文的範例當作起點,你也能打造屬於自己的樣式表。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved