1.什么是XSL-FO?
XSL-FO 是用于將結(jié)果格式化成XML數(shù)據(jù)的語言,XSL-FO全稱為(Extensible Stylesheet Language Formatting Objects:擴展格式化對象樣式表語言),XSL-FO 是W3C的推薦標準,XSL-FO 現(xiàn)在通常被稱為XSL。
XSL-FO 是用于格式化數(shù)據(jù)的
XSL是一種基于XML的標記語言,它對輸出到屏幕、紙張或其它媒體上的XML數(shù)據(jù)的格式化作了具體的描述。
XSL-FO現(xiàn)在通常叫做XSL
樣式化包括信息的轉(zhuǎn)換和格式化。當萬維網(wǎng)聯(lián)盟(W3C)做出第一份XSL工作草案時,它包括了XML文檔的轉(zhuǎn)換和格式化的語法。后來,W3C的XSL工作組把原草案分成了以下幾塊參考標準:
l XSLT,用于轉(zhuǎn)換XML文檔的語言
l XSL 或 XSL-FO,用于格式化XML文檔的語言
l XPath,用于將XML文檔中的元素和屬性進行定位的語言
該教程剩下的部分是關于格式化XML文檔的語言的:XSL-FO,也稱為XSL。
XSL-FO是一個網(wǎng)絡標準
2001年10月15日,XSL-FO已經(jīng)成為了W3C的推薦標準。它通常被稱為XSL。
2.XSL-FO文檔
XSL-FO文檔是包含輸出信息的XML文件。它們包含了與輸出的布局和輸出的內(nèi)容相關的信息。XSL-FO文檔以擴展名“a .fo” 或 “a .fob” 的文件保存在文件里。XSL-FO文檔也通常以“an .xml” 擴展名來保存。因為這樣做可以使它們能更容易被XML編輯器訪問。
XSL-FO文檔結(jié)構
<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> </fo:root> |
結(jié)構說明
XSL-FO 文檔是XML文檔,所以必須在文檔的起始處包含一份XML聲明:
<?xml version="1.0" encoding="UTF-8"?>
<fo:root> 元素是XSL-FO文檔的根元素。根元素也給XSL-FO聲明了命名空間:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- The full XSL-FO document goes here -->
</fo:root>
<fo:layout-master-set> 元素包含了一個或多個頁面模板:
<fo:layout-master-set>
<!-- All page templates go here -->
</fo:layout-master-set>
每個<fo:simple-page-master>元素包含著一個單獨的頁面模板。每個模板都包含一個獨立的名稱:
<fo:simple-page-master master-name="A4">
<!-- One page template goes here -->
</fo:simple-page-master>
一個或多個<fo:page-sequence>元素描述了頁面內(nèi)容。Master-reference 屬性指的是同名的“simple-page-master” 模板。
<fo:page-sequence master-reference="A4">
<!-- Page content goes here -->
</fo:page-sequence>
注意:事實上,master-reference = "A4" 并未真正地描述預定義頁面格式。它只是一個名稱而已。你可以隨意命名,如:"MyPage"、"MyTemplate" 等等這樣的名稱。
3.XSL-FO 區(qū)域
XSL格式化模型定義了大量的矩形域(塊狀區(qū))來顯示結(jié)果。所有的結(jié)果(文本、圖片,等等)都會被格式化以后放入這些區(qū)域里,并且,它將會在目標媒體上顯示或打印出來。讓我們進一步看看下面的區(qū)域:
l Pages 頁面
l Regions 區(qū)域
l Block areas 塊狀區(qū)域
l Line areas 行區(qū)域
l Inline areas 行內(nèi)區(qū)域
XSL-FO 頁面
XSL-FO 結(jié)果被格式化成頁面形式。打印出來的結(jié)果通常會被寫進很多獨立的頁面。瀏覽器的輸出的結(jié)果通常會顯示長長的一頁。XSL-FO頁面包含區(qū)域。
XSL-FO 區(qū)域
每張XSL-FO頁面都包含大量的區(qū)域。
l region-body 區(qū)域 主體(頁面的主體)
l region-before 區(qū)域頭部(頁面頁首)
l region-after 區(qū)域尾部(頁面的頁腳)
l region-start 區(qū)域左端(左工具條)
l region-end 區(qū)域右端(右工具條)
XSL-FO 區(qū)域包含塊狀區(qū)域。
XSL-FO 塊狀區(qū)域
XSL-FO 塊狀區(qū)域定義了小塊狀元素(通常是以新的一行作為起始的元素),例如圖表、表格和列表。XSL-FO 塊狀區(qū)域可以包含其它的塊狀區(qū)域,但是大多數(shù)情況下,它們通常包含行區(qū)域。
XSL-FO 行區(qū)域
XSL-FO 行區(qū)域定義了塊狀區(qū)域內(nèi)部的文本行。XSL-FO 行區(qū)域包含行內(nèi)區(qū)域。
XSL-FO Inline 行內(nèi)區(qū)域
XSL-FO 行內(nèi)區(qū)域定義了行內(nèi)的文本內(nèi)容(段首粗體圓點、單字符、圖形,等等)。
4.XSL-FO 輸出
XSL-FO 在<fo:flow> 元素中定義輸出結(jié)果。
XSL-FO 頁面、流程和區(qū)域
“塊狀區(qū)域”的內(nèi)容首先進入“頁面”,然后再由指定的媒體輸出。XSL-FO 的輸出結(jié)果通常被嵌套在<fo:block>元素、<fo:flow>元素、以及<fo:page-sequence>元素中,具體如下:
<fo:page-sequence> |
XSL-FO 案例
下面列舉一個關于XSL-FO的實際案例:
<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> </fo:root> |
上述代碼應該輸出下述結(jié)果:
Hello w3pop.com |
5.XSL-FO 流程
XSL-FO 將被來自<fo:flow>元素中的數(shù)據(jù)所填充。XSL-FO 使用<fo:page-sequence>元素來定義結(jié)果頁面。每個結(jié)果頁面都包含了定義頁面布局的頁面主體元素。每個結(jié)果頁面都包含一個定義輸出結(jié)果的<fo:flow>元素。每個結(jié)果頁面都會按照一定順序打印(或顯示)。
XSL-FO 流程
XSL-FO 將被來自<fo:flow>元素中的內(nèi)容所填充。<fo:flow> 元素包含了需要在頁面上打印的所有元素。當頁面被印滿時,相同的頁面主體元素會被反復地使用,直到所有的文本內(nèi)容都被打印出來為止。
流程“流向”哪里?
<fo:flow> 元素包含“flow-name(流程名稱)”屬性。“flow-name(流程名稱)”的屬性值定義了<fo:flow>元素的內(nèi)容將流向何處。所有合法值如下:
l xsl-region-body :流向區(qū)域主體
l xsl-region-before :流向區(qū)域最上端
l xsl-region-after :流向區(qū)域最下端
l xsl-region-start :流向區(qū)域左端
l xsl-region-end :流向區(qū)域右端
6.XSL-FO 頁面
XSL-FO 使用名為 “Page Masters [ 頁面主體 ] ” 的頁面模板來定義頁面的布局。
XSL-FO 頁面模板
XSL-FO 使用名為“Page Masters [ 頁面主體 ] ” 的頁面模板來定義頁面的布局。每塊模板必須包含一個獨立的名稱:
<fo:simple-page-master master-name="intro"> <fo:simple-page-master master-name="left"> <fo:simple-page-master master-name="right"> |
在上述案例中,三個<fo:simple-page-master>元素,定義了三塊不同的模板。每塊模板都包含不同的名稱。第一塊模板稱為“intro”,它是用于介紹頁面的模板。第二和第三塊模板稱作"left" 和 "right"。它們是用于定義奇數(shù)頁面和偶數(shù)頁面。
XSL-FO 頁面大小
XSL-FO 使用下述屬性來定義頁面的尺寸:
a) page-width 定義了頁面的寬度
b) page-height 定義了頁面的高度
XSL-FO 頁面邊界
XSL-FO 用下述屬性來定義頁面邊界:
a) margin-top 定義頂邊界
b) margin-bottom 定義底邊界
c) margin-left 定義左邊界
d) margin-right 定義右邊界
e) margin 定義所有四個邊界
XSL-FO 頁面區(qū)域
XSL-FO 使用以下元素來定義頁面的區(qū)域:
a) region-body 定義主體區(qū)域
b) region-before 定義頂端區(qū)域(頁眉)
c) region-after 定義底端區(qū)域(頁腳)
d) region-start 定義左端區(qū)域(左端工具條)
e) region-end 定義右端區(qū)域(右端工具條)
前端區(qū)域(region-before),后端區(qū)域(region-after),起始區(qū)域(region-start),終止區(qū)域(region-end)是主體區(qū)域的一部分。為了避免文本在主體區(qū)域內(nèi)溢出,主體區(qū)域的邊界尺寸至少要和上述這些區(qū)域一樣。
|
XSL-FO例子
下面列舉了一份XSL-FO文檔的部分內(nèi)容:
<fo:simple-page-master master-name="A4" |
上述代碼定義了名稱為"A4"的“Simple Page Master”模板。這張頁面寬297毫米,高210毫米。頁面的上下左右邊界都是1厘米。主體部分的四條邊上包含了寬為3厘米的邊界。主體的上下左右部分都是2厘米。在上述案例中,主體寬度 = 頁面自身寬度 - 左右邊界寬度 - 區(qū)域主體邊界寬度,具體如下:
297mm - (2 x 1cm) - (2 x 3cm) = 297mm - 20mm - 60mm = 217mm.
注意:區(qū)域(左端區(qū)域和右端區(qū)域)并不在計算范圍之內(nèi)。如同前面所說的那樣,這些區(qū)域只是主體的一部分。
7.XSL-FO 塊狀區(qū)域
XSL-FO 結(jié)果將輸入塊狀區(qū)域。
XSL-FO 頁面、流程 以及 塊狀區(qū)域
“塊狀區(qū)域”的內(nèi)容首先進入“頁面”,然后再由指定的媒體輸出。XSL-FO 的輸出結(jié)果通常被嵌套在<fo:block>元素、<fo:flow>元素、以及<fo:page-sequence>元素中,具體如下:
<fo:page-sequence> |
Block 區(qū)域?qū)傩?/span>
塊狀區(qū)域包含矩形框內(nèi)的結(jié)果序列:
<fo:block |
因為塊狀區(qū)域的是矩形框,它們共享很多共同的區(qū)域性質(zhì):
l space before and space after 最上端邊界和最下端邊界
l margin 邊界
l border 邊框
l padding 補白
“Space before [頂端空間]” 和 “space after [底端空間]” 是用來分隔其它塊狀區(qū)域的空白空間。“margin [邊界]” 是塊狀區(qū)域外部的空白區(qū)域。“border [邊框]”是矩形框的四條邊;它們可以有不同的寬度;它們也可以填充不同的顏色和背景圖片,padding [補白]”是位于邊框和內(nèi)容區(qū)域之間的地方。“content [內(nèi)容]” 區(qū)域包含了類似于文本、圖片、圖表等實際存在的內(nèi)容。
塊狀區(qū)域邊界
l margin四邊邊界
l margin-top頂部邊界
l margin-bottom底部邊界
l margin-left左邊界
l margin-right右邊界
塊狀區(qū)域邊框
邊框樣式屬性:
l border-style邊框樣式
l border-before-style頂端邊框樣式
l border-after-style底端邊框樣式
l border-start-style左端邊框樣式
l border-end-style右端邊框樣式
l border-top-style (same as border-before) 頂端邊框樣式(和 border-before 相同)
l border-bottom-style (same as border-after) 底端邊框樣式(邊 border-after 相同)
l border-left-style (same as border-start) 左端邊框樣式(和 border-start 相同)
l border-right-style (same as border-end) 右端邊框樣式(和 border-end 相同)
邊框顏色屬性:
l border-color邊框顏色
l border-before-color頂端邊框顏色
l border-after-color底端邊框顏色
l border-start-color左端邊框顏色
l border-end-color右端邊框顏色
l border-top-color (same as border-before) 頂端邊框顏色(和 border-before 相同)
l border-bottom-color (same as border-after) 底端邊框顏色(和 border-after 相同)
l border-left-color (same as border-start) 左端邊框顏色(和 border-start 相同)
l border-right-color (same as border-end) 右端邊框顏色(和 border-end 相同)
邊框?qū)挾葘傩裕?/span>
l border-width邊框?qū)挾?/span>
l border-before-width頂端邊框?qū)挾?/span>
l border-after-width底端邊框?qū)挾?/span>
l border-start-width左端邊框?qū)挾?/span>
l border-end-width右端邊框?qū)挾?/span>
l border-top-width (same as border-before) 頂端邊框?qū)挾龋ê?/span> border-before 相同)
l border-bottom-width (same as border-after) 底端邊框?qū)挾龋ê?/span> border-after 相同)
l border-left-width (same as border-start) 左端邊框?qū)挾龋ê?/span> border-start 相同)
l border-right-width (same as border-end) 右端邊框?qū)挾龋ê?/span> border-end 相同)
塊狀區(qū)域補白:
l padding補白
l padding-before頂端補白
l padding-after底端補白
l padding-start左端補白
l padding-end右端補白
l padding-top (same as padding-before) 頂端補白(和 padding-before 相同)
l padding-bottom (same as padding-after) 底端補白(和 padding-after 相同)
l padding-left (same as padding-start) 左端補白(和 padding-start 相同)
l padding-right (same as padding-end) 右端補白(和 padding-end 相同)
塊狀區(qū)域背景
l background-color背景顏色
l background-image背景圖形
l background-repeat背景重復
l background-attachment (scroll or fixed) 背景滾動設置(scroll:隨頁面滾動而滾動;fixed:不隨頁面滾動而滾動)
塊狀區(qū)域樣式屬性
塊狀區(qū)域包含了可以單獨定義樣式的結(jié)果序列:
<fo:block |
字體屬性:
l font-family字體
l font-weight字體粗細
l font-style字形
l font-size字體尺寸
l font-variant字體變量
文本屬性:
l text-align文本排列
l text-align-last文本排列持續(xù)
l text-indent文本縮進
l start-indent頂端縮進
l end-indent底端縮進
l wrap-option (defines word wrap) 嵌套選項(定義自動換行)
l break-before (defines page breaks) 頁面左端換行(定義頁面換行)
l break-after (defines page breaks) 頁面右端換行(定義頁面換行)
l reference-orientation (defines text rotation in 90" increments) 參考定位(定義90度內(nèi)的文本旋轉(zhuǎn))
舉例
<fo:block <fo:block |
Result:
結(jié)果
W3Schools At w3pop.com you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. |
從上述案例中,你會發(fā)現(xiàn),要創(chuàng)建一份包含多個標題和段落的文檔需要使用大量的代碼。一般情況下,XSL-FO文檔不需要將格式化信息和內(nèi)容結(jié)合起來。只需要使用XSLT的一小部分功能,我們就可以把格式化信息放入模板中,書寫一份整潔的內(nèi)容了。當你學完這份教程之后,你就會掌握更多關于把XSL-FO和XSLT模板結(jié)合起來使用的方法。
8.XSL-FO 列表
XSL-FO用列表塊區(qū)定義列表
XSL-FO列表區(qū)
用于創(chuàng)建列表的XSL-FO對象有以下四個:
l fo:list-block (含有整個列表)
l fo:list-item (含有列表里的每一項)
l fo:list-item-label (含有列表項的標簽,典型的例子如:一個<fo:block>元素含有一個數(shù)字,字符,等等.)
l fo:list-item-body (含有列表項的內(nèi)容和主體,典型的例子如:一個或多個<fo:block>對象)
一份XSL-FO列表的例子:
<fo:list-block> <fo:list-item> <fo:list-item> </fo:list-block> |
The output from this code would be:
這份代碼應該輸出如下結(jié)果:
* Volvo |
9.XSL-FO 表格
XSL-FO 使用<fo:table-and-caption>元素定義表格。
XSL-FO 表格
XSL-FO 的表格模式和HTML表格模式相差不大。下面列舉9個可以用于創(chuàng)建表格的XSL-FO對象:
l fo:table-and-caption
l fo:table
l fo:table-caption
l fo:table-column
l fo:table-header
l fo:table-footer
l fo:table-body
l fo:table-row
l fo:table-cell
XSL-FO使用<fo:table-and-caption>元素來定義一張表格。它包含了一個<fo:table>元素和一個可選擇的<fo:caption>元素。<fo:table>元素包含了幾個可選擇的<fo:table-column>元素,一個可選擇的<fo:table-footer>元素。這些元素都包含一個或多個的<fo:table-row>元素以及一個或多個的<fo:table-cell>元素:
<fo:table-and-caption> |
The output from this code would something like this:
上述代碼將輸出如下結(jié)果:
Car |
Price |
Volvo |
$50000 |
SAAB |
$48000 |
10.XSL-FO 和 XSLT
XSL-FO 和 XSLT 可以進行功能互補。
還記得下面這個案例嗎?
<fo:block <fo:block |
Result:
結(jié)果
W3Schools At w3pop.com you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. |
從XSLT中得到一些幫助
從文檔中刪除XSL-FO信息的方法:
<header> <paragraph> |
Add an XSLT transformation:
添加一條 XSLT 轉(zhuǎn)換信息的方法:
<xsl:template match="header"> |
And the result will be the same:
結(jié)果仍然相同:
W3Schools At W3pop.com you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. |
11.XSL-FO 軟件
XSL-FO 需要格式化軟件來輸出結(jié)果。
XSL-FO 處理器
XSL-FO 處理器是用于格式化XML文檔輸出結(jié)果的軟件程序。大多數(shù)XSL-FO 處理器可以輸出PDF文檔,并進行高質(zhì)量的打印。當然,它還可以HTML等其它形式輸出。下面的部分介紹一些知名的XSL-FO處理器。
1) XSL Formatter:XSL Formatter 是一種用于格式化XML文檔的軟件,從而可以打印輸出高質(zhì)量的PDF文件。在2002年的一月,全球市場上推出了相同產(chǎn)品的V2版本。在歐洲舉辦的XML 2002, XML 2003 峰會上,XSL Formatter被公認為品質(zhì)最佳的產(chǎn)品。在擁有4年XSL-FO軟件開發(fā)經(jīng)驗的基礎之上,, Antenna House又從頭開發(fā)全新的Formatter。這種Formatter將增加一些更顯著實用的功能,并為將來的繼續(xù)發(fā)展提供了堅實的基礎。
2) Xinc:Xinc 是 Lunasil 公司推出的XSL-FO處理器。Xinc的特點是:運行速度快,多路處理,并有記憶效果。包含擺動裝置的XSL-FO閱讀器可以瀏覽和打印XSL-FO文件,只需要點擊鼠標就可以操做PDF文件;通過Xinc的Java API(Java應用程序接口),Xinc可以作為服務器組件使用;通過Xinc的COM(串行通訊端口)分界面,它也可在Microsoft服務器環(huán)境下使用。它的新特點如下:連字符號連接、基礎連接、PDF輸出、內(nèi)存 / 速度最優(yōu)化和簡單的COM界面。
3) Scriptura:Inventive Designers Scriptura 是基于XSLT 和 XSL-FO 跨平臺文檔設計器和問題處理器。Scriptura 包含一個 WYSIWYG(即見及所得)的工具和引擎。在該引擎中使用的 XSL-FO formatter 已經(jīng)不再以 Apache FOP 為基礎了,而是設計者們重新開始書寫。這個版本的新特點是:支持圓點、編號列表、“break-before [ 頂部換行 ]” 和 “break-after [ 底部換行 ]” 特性;擴展的條形碼選項、改進的數(shù)字、貨幣格式。免費的體驗版本可以通過下載獲取。
XSL格式化對象參考資料
將具體描述轉(zhuǎn)化為表達式的過程稱為格式化:
Object |
Description |
Represents the start resource of a link |
|
Overrides the default Unicode BIDI direction |
|
Defines a block of output (e.g. paragraphs and titles) |
|
Defines a block-level reference-area |
|
Specifies a character that will be mapped to a glyph for presentation |
|
Defines a color-profile for a stylesheet |
|
Specifies a page-master to be used when the conditions defined are true |
|
Groups global declarations for a stylesheet |
|
Used for a graphic where the graphics data resides outside of the XML result tree |
|
Typically used to position an image in a separate area at the beginning of a page OR to position an image to one side, with the content flowing along-side of the image |
|
Contains all elements to be printed to a page |
|
Defines a footnote within the region-body of a page |
|
Defines the content of the footnote |
|
Formats the first line of an <fo:block> |
|
Formats a part of a text with a background or enclosing it in a border |
|
Defines an inline reference-area |
|
Used for inline graphics or for "generic" objects where the object's data resides as descendants of <fo:instream-foreign-object> |
|
Holds all masters used in a document |
|
Used to generate "." to separate titles from page numbers in table of contents, or to create input fields in forms, or to create horizontal rules |
|
Defines a list |
|
Contains each item in the list |
|
Contains the content/body of the list-item |
|
Contains the label for the list-item (typically a number, character, etc.) |
|
Used with <fo:retrieve-marker> to create running headers or footers |
|
Contains (within an <fo:multi-switch>) each alternative sub-tree of XSL-FO objects. The parent <fo:multi-switch> will choose which alternative to show and hide the rest |
|
Used to switch between two or more property-sets |
|
Specifies an alternative property-set that will be applied depending on the state of the user agent |
|
Holds one or more <fo:multi-case> objects and controls the switching between them (activated by <fo:multi-toggle>) |
|
Used to switch to another <fo:multi-case> |
|
Represents the current page-number |
|
References the page-number for the page that contains the first normal area returned by the cited object |
|
A container for page output elements. There will be one <fo:page-sequence> object for each page layout |
|
Specifies which simple-page-masters are to be used and in which order |
|
Defines a page footer |
|
Defines a page header |
|
Defines a page body |
|
Defines the right sidebar of a page |
|
Defines the left sidebar of a page |
|
Specifies repetition of a set of simple-page-masters |
|
Specifies repetition of a single simple-page-master |
|
Used with <fo:marker> to create running headers or footers |
|
The root (top) node for XSL-FO documents |
|
Defines the size and shape of a page |
|
Specifies a page-master to be used at a given point in the sequence of pages |
|
Contains static content (e.g. headers and footers) that will be repeated on many pages |
|
Formats the tabular material of a table |
|
Formats a table and its caption |
|
Container for table rows and table cells |
|
Contains the caption for a table |
|
Defines a table cell |
|
Formats the columns of a table |
|
Defines a table footer |
|
Defines a table header |
|
Defines a table row |
|
Defines a title for a page-sequence |
|
Specifies inherited properties for a group of XSL-FO objects |