DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
HTML 轉格式化物件(FO)實作指南(下):Doug Tidwell
編輯:XML詳解     

本文承接上一篇,逐一示範如何把常見的 HTML 元素對應成 XSL-FO 元素,並附上可直接取用的 XSLT 樣板。所有範例都以「最終輸出 PDF」為前提。

<h1>~<h6> 標題

標題的轉換相對單純:把每個標題放進一個 <fo:block>,再依層級調整字體、字級與間距。範例中的版面配置還在頂層標題之前插入換頁與一條水平線,讓章與章之間有清楚的區隔。各層級的格式設定如下:

HTML 標記字級行高標題後間距其他
<h1>28pt32pt22pt文字前加分頁與水平線
<h2>24pt28pt18pt無
<h3>21pt24pt14pt無
<h4>18pt21pt12pt無
<h5>16pt19pt12pt文字加粗
<h6>14pt17pt12pt文字加粗、加下劃線且為斜體

以下是標題元素的範例:

<h1>Sample text from Henry FIElding's <cite>Tom Jones</cite></h1>
<h2><b>Book I.</b> Containing as Much of the Birth of the Foundling
   as Is Necessary or Proper to Acquaint the Reader with in the
   Beginning of This History</h2>
<h3><b>Chapter VII.</b> Containing Such Grave Matter, That the Reader
   Cannot Laugh Once Through the Whole Chapter, Unless Peradventure He
   Should Laugh at the Author</h3>

轉換後的格式化物件如下:

<fo:block break-before="page">
  <fo:leader leader-pattern="rule"/>
</fo:block>
<fo:block font-family="serif" space-after="22pt" keep-with-next="always"
          line-height="32pt" font-size="28pt" id="tomjones">
  Sample text from Henry FIElding's
  <fo:inline font-style="italic">Tom Jones</fo:inline>
</fo:block>
<fo:block font-family="serif" space-after="18pt" keep-with-next="always"
          line-height="28pt" font-size="24pt" id="N10017">
  <fo:inline font-weight="bold">Book I.</fo:inline>
  Containing as Much of the Birth of the Foundling
  as Is Necessary or Proper to Acquaint the Reader with in the
  Beginning of This History
</fo:block>
<fo:block font-family="serif" space-after="14pt" keep-with-next="always"
          line-height="24pt" font-size="21pt" id="N1001C">
  <fo:inline font-weight="bold">Chapter VII.</fo:inline>
  Containing Such Grave Matter, That the Reader
  Cannot Laugh Once Through the Whole Chapter, Unless Peradventure He
  Should Laugh at the Author
</fo:block>

在 <h1> 文字前插入分頁,會讓「命名錨點 + 標題」的組合變得棘手,因此 <h1> 的樣板會先檢查前一個兄弟元素是不是命名錨點。以下是 <h1> 與 <h6> 的樣板:

<xsl:template match="h1">
  <fo:block break-before="page">
    <fo:leader leader-pattern="rule"/>
  </fo:block>
  <fo:block font-size="28pt" line-height="32pt"
            keep-with-next="always"
            space-after="22pt" font-family="serif">
    <xsl:attribute name="id">
      <xsl:choose>
        <xsl:when test="@id">
          <xsl:value-of select="@id"/>
        </xsl:when>
        <xsl:when test="name(preceding-sibling::*[1]) = 'a' and
                        preceding-sibling::*[1][@name]">
          <xsl:value-of select="preceding-sibling::*[1]/@name"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="generate-id()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:apply-templates select="*|text()"/>
  </fo:block>
</xsl:template>

<xsl:template match="h6">
  <fo:block font-size="14pt" line-height="17pt"
            keep-with-next="always" space-after="12pt"
            font-family="serif"
            font-style="italic"
            text-decoration="underline">
    <xsl:attribute name="id">
      <xsl:choose>
        <xsl:when test="@id">
          <xsl:value-of select="@id"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="generate-id()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:apply-templates select="*|text()"/>
  </fo:block>
</xsl:template>

最後一點:標題同時是書籤、目錄與連結的落點,所以務必保證每個標題都有 id。若元素本身已帶 id 就用它,否則用 XSLT 的 generate-id() 補一個:

<xsl:attribute name="id">
  <xsl:choose>
    <xsl:when test="@id">
      <xsl:value-of select="@id"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="generate-id()"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:attribute>

<hr> 水平線

<fo:leader> 是 XSL-FO 專門用來畫水平線的元素。以下是 HTML 範例:

<p>Here's a short paragraph.</p>
<hr/>
<p>Here's another paragraph, following a horizontal rule.</p>

對應的 XSL-FO 標記:

<fo:block>
  Here's a short paragraph.
</fo:block>
<fo:block>
  <fo:leader leader-pattern="rule"/>
</fo:block>
<fo:block>
  Here's another paragraph, following a horizontal rule.
</fo:block>

樣板本身非常短:

<xsl:template match="hr">
  <fo:block>
    <fo:leader leader-pattern="rule"/>
  </fo:block>
</xsl:template>

補充一點:leader-pattern 除了 rule,還接受 dots(常用於目錄的點狀引導線)與 space(製造一段空白)。

<i> 斜體文字

把 HTML 的 <i> 轉成 XSL-FO 很直覺:將內容包進帶 font-style="italic" 的 <fo:inline> 即可。範例如下,寫法與稍後的 <b> 幾乎一致:

<p>Jackdaws <i>love</i> my big sphinx of quartz.</p>

用最基本的 <fo:block> 與 <fo:inline> 來表達:

<fo:block>
  Jackdaws <fo:inline font-style="italic">love</fo:inline>
  my big sphinx of quartz.
</fo:block>

之所以用 <fo:inline> 而非 <fo:block>,是因為後者一定會造成換行。對應的樣板:

<xsl:template match="i">
  <fo:inline font-style="italic">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<img> 嵌入圖片

<img> 直接對應到 <fo:external-graphic>。複雜之處在於 FOP 不接受 width="200" 這種 HTML 寫法,width 與 height 必須帶單位(例如 200px)。因此樣板會在輸出時檢查 HTML 值並補上單位:

<xsl:template match="img">
  <fo:block space-after="12pt">
    <fo:external-graphic src="{@src}">
      <xsl:if test="@width">
        <xsl:attribute name="width">
          <xsl:choose>
            <xsl:when test="contains(@width, 'px')">
              <xsl:value-of select="@width"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat(@width, 'px')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@height">
        <xsl:attribute name="height">
          <xsl:choose>
            <xsl:when test="contains(@height, 'px')">
              <xsl:value-of select="@height"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat(@height, 'px')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
      </xsl:if>
    </fo:external-graphic>
  </fo:block>
</xsl:template>

注意:src 屬性原樣沿用,只有 width 與 height 會被補上長度單位。

<kbd> 鍵盤輸入

鍵盤輸入慣例上以稍大的等寬字體呈現。HTML 範例:

<p>
  An easy way to delete a directory and all its contents (including
  all subdirectorIEs) is with the <kbd>rd /s</kbd> command.
</p>

對應的格式化物件:

<fo:block>
  An easy way to delete a directory and all its contents (including
  all subdirectorIEs) is with the
  <fo:inline font-family="monospace" font-size="110%">rd /s</fo:inline>
  command.
</fo:block>

樣板:

<xsl:template match="kbd">
  <fo:inline font-family="monospace" font-size="110%">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<li> 列表項目

處理列表項目幾乎與處理段落相同,只差在幾個細節,而這些差異取決於父元素是 <ol> 還是 <ul>,因此請見後面兩節的說明。

<nobr> 不換行文字

HTML 的 <nobr> 要求瀏覽器無論內容多長都不得換行。例如:

<p>
  On the Windows 2000 command line, you can use the ampersand (&) to
  combine several commands into one statement.
  <nobr>
    pushd d:\projects\xslfo & del *.fo & rebuild.bat & popd
  </nobr>
  is an example of this technique.
</p>

改用帶 wrap-option="no-wrap" 的 <fo:inline> 表達:

<fo:block>
  On the Windows 2000 command line, you can use the ampersand (&)
  to combine several commands into one statement.
  <fo:inline wrap-option="no-wrap">pushd d:\projects\xslfo &
  del *.fo & rebuild.bat & popd</fo:inline> is an
  example of this technique.
</fo:block>

樣板:

<xsl:template match="nobr">
  <fo:inline wrap-option="no-wrap">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<ol> 有序列表

XSL-FO 詞彙中有專門處理列表的元素,這點比 HTML 更明確。以下是 HTML 範例:

<p>A few of my favorite albums</p>
<ol>
  <li>A Love Supreme</li>
  <li>Remain in Light</li>
  <li>London Calling</li>
  <li>The Indestructible Beat of Soweto</li>
  <li>The Joshua Tree</li>
</ol>

需要用到四個元素:<fo:list-block> 包住整個列表;每個 <fo:list-item> 對應單一項目,內部再分成 <fo:list-item-label>(項目旁的編號,例如「10. 」)與 <fo:list-item-body>(項目實際內容)。轉換結果:

<fo:block>A few of my favorite albums</fo:block>
<fo:list-block provisional-distance-between-starts="0.75cm"
               provisional-label-separation="0.5cm" space-after="12pt"
               start-indent="1cm">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>1. </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        A Love Supreme
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
  ...
  <!-- other list items appear here -->
  ...
</fo:list-block>

樣板必須處理四種情況:

  • 若列表本身嵌在其他列表中,之後不留任何空白;否則在列表後加上 12 點的垂直間距。
  • 若列表不在任何其他列表中,縮排 1 cm;否則縮排 1 cm 再加上每層列表各 1.25 cm。也就是說第三層列表會縮排 3.5 cm。
  • 若 <ol> 帶有 start 屬性,就從該數字開始編號;否則直接用 XSLT 的 position() 編號。
  • 若 <ol> 帶有 type 屬性,就用它決定編號樣式(羅馬數字、阿拉伯數字或字母),做法是設定 <xsl:number> 的 format 屬性。

完整的 XSLT 樣板如下(沿用項目符號列表的規則):

<xsl:template match="ol">
  <fo:list-block provisional-distance-between-starts="1cm"
                 provisional-label-separation="0.5cm">
    <xsl:attribute name="space-after">
      <xsl:choose>
        <xsl:when test="ancestor::ul or ancestor::ol">
          <xsl:text>0pt</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>12pt</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:attribute name="start-indent">
      <xsl:variable name="ancestors">
        <xsl:choose>
          <xsl:when test="count(ancestor::ol) or count(ancestor::ul)">
            <xsl:value-of select="1 +
              (count(ancestor::ol) +
               count(ancestor::ul)) *
              1.25"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>1</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="concat($ancestors, 'cm')"/>
    </xsl:attribute>
    <xsl:apply-templates select="*"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="ol/li">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>
        <xsl:variable name="value-attr">
          <xsl:choose>
            <xsl:when test="../@start">
              <xsl:number value="position() + ../@start - 1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:number value="position()"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="../@type='i'">
            <xsl:number value="$value-attr" format="i. "/>
          </xsl:when>
          <xsl:when test="../@type='I'">
            <xsl:number value="$value-attr" format="I. "/>
          </xsl:when>
          <xsl:when test="../@type='a'">
            <xsl:number value="$value-attr" format="a. "/>
          </xsl:when>
          <xsl:when test="../@type='A'">
            <xsl:number value="$value-attr" format="A. "/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:number value="$value-attr" format="1. "/>
          </xsl:otherwise>
        </xsl:choose>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        <xsl:apply-templates select="*|text()"/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<p> 段落

段落轉成 <fo:block> 相當直接。例如:

<p>When in the Course of human events, it becomes necessary
   for one people to dissolve the political bonds which have connected
   them with another, and to assume among the powers of the earth,
   the separate and equal station to which the Laws of Nature and
   of Nature's God entitle them, a decent respect to the opinions
   of mankind requires that they should declare the causes which
   impel them to the separation.</p>

轉換後:

<fo:block font-size="12pt" line-height="15pt
           space-after="12pt">When in the Course of human events, it becomes
necessary for one people to dissolve the political bonds which
...
</fo:block>

樣板:

<xsl:template match="p">
  <fo:block font-size="12pt" line-height="15pt"
            space-after="12pt">
    <xsl:apply-templates select="*|text()"/>
  </fo:block>
</xsl:template>

這裡的 font-size、line-height 與 space-after 都只是預設值,可依需要自行調整。

<pre> 預先格式化的文字

<pre> 有幾個棘手之處:必須完整保留其中的空白、必須關閉 XSL-FO 引擎的自動換行,慣例上還要用等寬字體。範例:

<pre>
public static void main(String args[])
{
    System.out.println("Hello, world!");
}
</pre>

正確的對應寫法:

<fo:block font-family="monospace"
          white-space-collapse="false"
          wrap-option="no-wrap">
public static void main(String args[])
{
    System.out.println("Hello, world!");
}
</fo:block>

樣板:

<xsl:template match="pre">
  <fo:block font-family="monospace"
            white-space-collapse="false"
            wrap-option="no-wrap">
    <xsl:apply-templates select="*|text()"/>
  </fo:block>
</xsl:template>

<samp> 範例文字

同樣以稍大的等寬字體呈現。雖然 <samp> 不常用,轉換起來毫不費力:

<p>The <samp>DOCTYPE</samp> keyWord lets you
   refer to a DTD from your XML source document.</p>

樣板:

<xsl:template match="samp">
  <fo:inline font-family="monospace" font-size="110%">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<small> 較小字體

既然 <big> 的樣板用「比正常字體大 20%」來實作,那麼 <small> 對稱地定義為小 20% 就很合理:

<xsl:template match="small">
  <fo:inline font-size="80%">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

遇到 <p>The Lakers' chances for a fourth straight title are <small>slim</small>.</p> 這樣的段落,輸出為:

<fo:block font-size="12pt" line-height="15pt" space-after="12pt">
  The Lakers' chances for a fourth straight title are
  <fo:inline font-size="80%">slim</fo:inline>.
</fo:block>

與 <big> 一樣,多層嵌套 <small> 會讓文字逐層變小。

<strike> 刪除線文字

建立帶 text-decoration="line-through" 的 <fo:inline> 即可。刪除線常用來標示文件中被移除的內容:

<p>The underline property
   <strike>is not currently supported by FOP.</strike>
   is now supported by FOP.</p>

樣板:

<xsl:template match="strike">
  <fo:inline text-decoration="line-through">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

補充:text-decoration 的關鍵字可正可負。若想在一段已套用 line-through 的長文字中關掉其中一小段,可以用 text-decoration="no-line-through";也可以同時給多個值,例如 text-decoration="line-through underline" 會同時加上刪除線與下劃線。

<strong> 強調文字

通常以粗體呈現,樣板很簡單:

<xsl:template match="strong">
  <fo:inline font-weight="bold">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<sub> 下標文字

下標靠 XSL-FO 的 vertical-align 屬性移動基線,通常還要順便縮小字級。HTML 範例:

<p>When I'm thirsty, nothing beats a cold
   glass of H<sub>2</sub>O.</p>

樣板:

<xsl:template match="sub">
  <fo:inline vertical-align="sub" font-size="75%">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<sup> 上標文字

做法與下標相同。HTML 範例:

<p>Einstein's famous e=mc<sup>2</sup>
   is an equation that changed the world.</p>

樣板:

<xsl:template match="sup">
  <fo:inline vertical-align="super" font-size="75%">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<table> 表格

處理 HTML 表格最大的難題是:XSL-FO 表格究竟該有幾欄、每欄多寬?FOP 要求每一欄都要有一個 <fo:table-column>;欄位定義搞定之後,剩下的就是把表格內各元素的樣板跑一遍。這個樣板處理的是類似下面的 cols 寫法:

<table cols="200 100pt" border="1">

必須轉成:

<fo:table-column column-width="200pt"/>
<fo:table-column column-width="100pt"/>

這裡要用到 XSLT 常見的「尾遞迴」(tail recursion)技巧:寫一個具名樣板,取出屬性值的頭一個單字並轉成 <fo:table-column>,然後呼叫自己處理剩下的部分,直到整串屬性都處理完。<table> 的樣板如下:

<xsl:template match="table">
  <fo:table table-layout="fixed">
    <xsl:choose>
      <xsl:when test="@cols">
        <xsl:call-template name="build-columns">
          <xsl:with-param name="cols"
                          select="concat(@cols, ' ')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <fo:table-column column-width="200pt"/>
      </xsl:otherwise>
    </xsl:choose>
    <fo:table-body>
      <xsl:apply-templates select="*"/>
    </fo:table-body>
  </fo:table>
</xsl:template>

有 cols 屬性時呼叫 build-columns,否則直接補一個 <fo:table-column>。另外別忘了 table-layout="fixed"——少了它,FOP 會跳出警告訊息。

接著看 build-columns:

<xsl:template name="build-columns">
  <xsl:param name="cols"/>
  <xsl:if test="string-length(normalize-space($cols))">
    <xsl:variable name="next-col">
      <xsl:value-of select="substring-before($cols, ' ')"/>
    </xsl:variable>
    <xsl:variable name="remaining-cols">
      <xsl:value-of select="substring-after($cols, ' ')"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($next-col, 'pt')">
        <fo:table-column column-width="{$next-col}"/>
      </xsl:when>
      <xsl:when test="number($next-col) > 0">
        <fo:table-column column-width="{concat($next-col, 'pt')}"/>
      </xsl:when>
      <xsl:otherwise>
        <fo:table-column column-width="50pt"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="build-columns">
      <xsl:with-param name="cols" select="concat($remaining-cols, ' ')"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

邏輯依序是:先用 normalize-space() 去掉首尾空白後檢查 $cols 是否還有內容;再把 $cols 切成「第一個空格之前」與「之後的全部」兩半(呼叫端會在值尾補一個空格,確保至少有一個空格可切)。然後判斷前半段:含 pt 就當成 200pt 這類帶單位的值直接使用;是數字(number() 轉出的結果大於 0)就補上 pt;兩者都不合就用 50pt 作為預設。處理完前半段後,樣板帶著後半段呼叫自己,並在值尾再補一個空格以維持不變式。

<td> 表格儲存格

HTML <td> 直接對應 <fo:table-cell>,預設上下左右各留 3 點內距。需要處理 colspan、rowspan、align 三個屬性,另外以有限的方式處理 border。

colspan 與 rowspan 直接映射為 number-columns-spanned 與 number-rows-spanned,沒有難度。align 則映射為 text-align,但兩邊的=enumeration 不同:HTML 的 left、center、right、justify 分別對應 XSL-FO 的 start、center、end、justify。最後的麻煩是對齊值可能出現在 <td>、<tr>、<thead> 或 <table> 任一層,必須沿著祖先元素往上找(並注意 text-align 要設在 <fo:block> 而非 <fo:table-cell> 上)。此外,只要上述任一元素帶有 border="1",就以 border-style="solid"、border-color="black"、border-width="1pt" 三個屬性替儲存格描邊。

完整樣板如下,主體大多是在決定該用哪一組屬性:

<xsl:template match="td">
  <fo:table-cell padding-start="3pt" padding-end="3pt"
                 padding-before="3pt" padding-after="3pt">
    <xsl:if test="@colspan">
      <xsl:attribute name="number-columns-spanned">
        <xsl:value-of select="@colspan"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="@rowspan">
      <xsl:attribute name="number-rows-spanned">
        <xsl:value-of select="@rowspan"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="@border='1' or
                  ancestor::tr[@border='1'] or
                  ancestor::thead[@border='1'] or
                  ancestor::table[@border='1']">
      <xsl:attribute name="border-style"><xsl:text>solid</xsl:text></xsl:attribute>
      <xsl:attribute name="border-color"><xsl:text>black</xsl:text></xsl:attribute>
      <xsl:attribute name="border-width"><xsl:text>1pt</xsl:text></xsl:attribute>
    </xsl:if>
    <xsl:variable name="align">
      <xsl:choose>
        <xsl:when test="@align">
          <xsl:choose>
            <xsl:when test="@align='center'"><xsl:text>center</xsl:text></xsl:when>
            <xsl:when test="@align='right'"><xsl:text>end</xsl:text></xsl:when>
            <xsl:when test="@align='justify'"><xsl:text>justify</xsl:text></xsl:when>
            <xsl:otherwise><xsl:text>start</xsl:text></xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="ancestor::tr[@align]">
          <xsl:choose>
            <xsl:when test="ancestor::tr/@align='center'"><xsl:text>center</xsl:text></xsl:when>
            <xsl:when test="ancestor::tr/@align='right'"><xsl:text>end</xsl:text></xsl:when>
            <xsl:when test="ancestor::tr/@align='justify'"><xsl:text>justify</xsl:text></xsl:when>
            <xsl:otherwise><xsl:text>start</xsl:text></xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="ancestor::thead"><xsl:text>center</xsl:text></xsl:when>
        <xsl:when test="ancestor::table[@align]">
          <xsl:choose>
            <xsl:when test="ancestor::table/@align='center'"><xsl:text>center</xsl:text></xsl:when>
            <xsl:when test="ancestor::table/@align='right'"><xsl:text>end</xsl:text></xsl:when>
            <xsl:when test="ancestor::table/@align='justify'"><xsl:text>justify</xsl:text></xsl:when>
            <xsl:otherwise><xsl:text>start</xsl:text></xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise><xsl:text>start</xsl:text></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <fo:block text-align="{$align}">
      <xsl:apply-templates select="*|text()"/>
    </fo:block>
  </fo:table-cell>
</xsl:template>

<tfoot> 表格尾

<tfoot> 很少用到,內部包含若干 <tr>。處理方式很簡單,直接把 <tr> 的樣板叫起來就好:

<xsl:template match="tfoot">
  <xsl:apply-templates select="tr"/>
</xsl:template>

<th> 表頭儲存格

<th> 同樣對應 <fo:table-cell>,四邊 3 點內距,內部放一個粗體、置中的 <fo:block>。邊框邏輯與 <td> 類似,只是檢查的祖先層級少一層:

<xsl:template match="th">
  <fo:table-cell padding-start="3pt" padding-end="3pt"
                 padding-before="3pt" padding-after="3pt">
    <xsl:if test="@border='1' or
                  ancestor::tr[@border='1'] or
                  ancestor::table[@border='1']">
      <xsl:attribute name="border-style"><xsl:text>solid</xsl:text></xsl:attribute>
      <xsl:attribute name="border-color"><xsl:text>black</xsl:text></xsl:attribute>
      <xsl:attribute name="border-width"><xsl:text>1pt</xsl:text></xsl:attribute>
    </xsl:if>
    <fo:block font-weight="bold" text-align="center">
      <xsl:apply-templates select="*|text()"/>
    </fo:block>
  </fo:table-cell>
</xsl:template>

<thead> 表格頭

極少使用的 <thead>,處理方式與 <tfoot> 相同:

<xsl:template match="thead">
  <xsl:apply-templates select="tr"/>
</xsl:template>

<title> 文件標題

範例中的 PDF 版面要求把文件標題(<head> 裡的 <title>)以大字級、粗體、置中顯示於頁面頂端:

<xsl:template match="title">
  <fo:block space-after="18pt" line-height="27pt"
            font-size="24pt" font-weight="bold" text-align="center">
    <xsl:apply-templates select="*|text()"/>
  </fo:block>
</xsl:template>

<tr> 表格列

HTML <tr> 直接映射為 <fo:table-row>。既然絕大多數的表格處理邏輯都集中在 <td> 樣板裡,這裡只要建好列元素並把內容跑一遍即可:

<xsl:template match="tr">
  <fo:table-row>
    <xsl:apply-templates select="*|text()"/>
  </fo:table-row>
</xsl:template>

<tt> 打字機體文字

以等寬字體呈現:

<xsl:template match="tt">
  <fo:inline font-family="monospace">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<u> 下劃線文字

用 XSL-FO 的 text-decoration 屬性即可。HTML 範例:

<p>When typewriters ruled the earth,
   <u>underlining</u> was the most
   common way to highlight text.</p>

轉換時使用帶 text-decoration="underline" 的 <fo:inline>:

<xsl:template match="u">
  <fo:inline text-decoration="underline">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

同樣地,text-decoration 的關鍵字可正可負:想在已套用 underline 的長段落中關掉一小段,可用 text-decoration="no-underline";也可以複合指定,例如 text-decoration="underline line-through" 會同時產生下劃線與刪除線。

<ul> 無序列表

無序列表比有序列表單純,因為 <fo:list-item-label> 一律是項目符號。HTML 範例:

<p>A few of my favorite albums</p>
<ul>
  <li>A Love Supreme</li>
  <li>Remain in Light</li>
  <li>London Calling</li>
  <li>The Indestructible Beat of Soweto</li>
  <li>The Joshua Tree</li>
</ul>

一樣用到 <fo:list-block>、<fo:list-item>、<fo:list-item-label> 與 <fo:list-item-body>:

<fo:block>A few of my favorite albums</fo:block>
<fo:list-block provisional-distance-between-starts="0.2cm"
               provisional-label-separation="0.5cm"
               space-after="12pt" start-indent="1cm">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>•</fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      A Love Supreme
    </fo:list-item-body>
  </fo:list-item>
  ...
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>•</fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      The Joshua Tree
    </fo:list-item-body>
  </fo:list-item>
</fo:list-block>

項目符號直接使用 Unicode 實體 &#8226;。<ul> 的樣板遵循兩條規則:若列表嵌在其他列表中,之後不加空白;若不在任何列表中,縮排 1 cm,否則為 1 cm 加上每層各 1.25 cm(第三層即 3.5 cm)。

<xsl:template match="ul">
  <fo:list-block provisional-distance-between-starts="1cm"
                 provisional-label-separation="0.5cm">
    <xsl:attribute name="space-after">
      <xsl:choose>
        <xsl:when test="ancestor::ul or ancestor::ol">
          <xsl:text>0pt</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>12pt</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:attribute name="start-indent">
      <xsl:variable name="ancestors">
        <xsl:choose>
          <xsl:when test="count(ancestor::ol) or count(ancestor::ul)">
            <xsl:value-of select="1 +
              (count(ancestor::ol) +
               count(ancestor::ul)) *
              1.25"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>1</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="concat($ancestors, 'cm')"/>
    </xsl:attribute>
    <xsl:apply-templates select="*"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="ul/li">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>•</fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        <xsl:apply-templates select="*|text()"/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<var> 變數名稱

變數名慣例上以斜體等寬字體呈現,用 font-family 與 font-style 兩個屬性即可。範例:

<p>To run the FOP program, you must make sure
   your <var>classpath</var> variable
   is set correctly.</p>

樣板:

<xsl:template match="var">
  <fo:inline font-style="italic" font-family="monospace">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

把這些樣板組裝起來,就能把一份 HTML 文件相當完整地映射成 XSL-FO,再交給渲染器輸出 PDF。實作時最值得留意的三個地雷是:長度值務必帶單位、表格一定要給欄寬定義並加上 table-layout="fixed",以及別忘了替每個標題補上 id。

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