第三章 再生樹與自動化XML文件轉換
3.2 再生樹 R-Tree
在公式(1) 與 圖 3-1 之中,在資料擷取時,必須先給定一個樣本 網頁
h
,作為第一次資料擷取之用。樣本網頁h
將主導 XSLT 文件XS
之輸出樣式,也就是說,XSLT 所轉換出來的文件 H 與 sample 文件 h 類似。例如,在 sample 文件 S 中,商品資料只有一筆,而在結果文件 H 中,商品資料會有十筆,因為資料庫中有十筆紀錄。為了表示 sample 文件 S 如何擴展成結果文件 H,我們的做法是將 sample 文件 S 擴展成 一個再生樹 (Regeneration tree, R-Tree)。R-Tree 的節點中加上了 一些屬性來描述 S 如何擴展成結果網頁。R-tree 是一棵樹,其詳細定義如下:
定義
Regeneration Tree (R-Tree)• R-Tree (Regeneration Tree) 之結構與 sample HTML document 相同。R-Tree 中的節點都稱為 R-Node。
• R-Node 包含以下三個屬性:
– R-Tag (Regeneration Tag) 這個節點的 HTML Tag。
– R-DataSource (Regeneration Data Source)
R-DataSource 是一個 XPath,指向 XML 文件中的節點,也 就是這個節點的資料來源。
– R-Pattern (Regeneration Pattern)
• 以類似 DTD 的語法定義出子節點的結構。
(1) Operands: `0',`1',`2', … 分 別 代 表 child 0, child 1, child 2, …。
(2) Operators 定義如下:
• a,b => node
a
串接 nodeb
。• a^* => node
a
重複直到來源資料用完。• a^n => node
a
重複 n 次或是直到來源資料• a | b => child
a
或是 childb
,依照資料 R-DataSource,也就是 To 的值。因為這個範例是一對一的對應關係,所以 R-Pattern 就是 child 各出現一次,而 R-Tag 就是原本網頁相對應節點的 tag。圖 3-4 就是 這個範例最後建出來的 Regeneration Tree。
Page
Weather Weather Weather Weather
Location
div div div div
label
Weather Weather Weather Weather
Location
div div div div
label
Result HTML Document Source XML Document
Sample HTML Document Extracted XML Data
BODE
div div div div
label
•R-Pattern = (child 0) (child 1)…
•R-DataSource = to-string
•R-Tag = HTML Tag
圖 3-4: 範例一的 Regeneration Tree
範例二:
範例二是一份書籍的例子,這範例是要顯示一列 Title 列後,後面 都照紅色、綠色、紅色、綠色、…的方式去重複樣式,直到資料結束,
HTML
XML Document
Books
Book Book
TitleAuthorPriceYear TitleAuthorPriceYear
Book
Result HTML Document Source XML Document
Sample HTML Document Extracted XML Data
Extraction
Regeneration
<Books>
<Book>
<Title> Keep Simple </Title>
<Author>Bill<Author>
圖 3-7 顯示這範例的 Regeneration Tree,我們可以看到 tbody 的
•R-DataSource = to-string
•R-Tag = HTML Tag tbody
tr tr
td td td td
Title Author Price Year
td td td td
Keep
Smile Bill 277 2004
tr
td td td td
Healthy John 499 2003
S=BooksP=(0,(1,2)*)
S =Book P=(0,1,2,3) S=Book
P=(0,1,2,3) P=(0,1,2,3)S =Book
S =Title; S =Author ; S =Price ; S =Year
圖 3-7: 範例二的 Regeneration Tree
範例三:
這是一份電子技術手冊(ETM - Electronic Technical Manual)的 範例。在 XML 中,一個 Chapter 下有很多個 Section,而每個 Section 底下可能有 Title、Para 及、Caution 這三種 child,個數及出現的順 序都不固定,但呈現時希望照這三種資料出現的順序來做呈現。
圖 3-8 是 ETM 網頁資料與 XML 的對應關係。
Section Title
Para Caution
Chapter Para
圖 3-8: ETM 的 sample 與 XML 對照圖
圖 3-9 是 BODE 系統萃取及 XSLT 再生的示意圖,<H3>對應到 Title,
<p>對應到 Para,而<H4>對應到 Caution。當資料庫的資料為 Title、
Para、Caution、Caution、Para 時,用 XSLT 還原回來也會依<H3>、
<p>、<H4>、<H4>、<p>的順序來做呈現。
Section
Title Para Caution Caution Para
<div>
<H3> <p> <H4> <H4> <p>
Result HTML Document Source XML Document
Section
Title Para Caution
<div>
<H3> <p> <H4>
Sample HTML Document Extracted XML Data
BODE Extraction
XSLT Regeneration
圖 3-9: 範例三中的 From-To 對應圖
圖 3-10 是這個範例的 Regeneration Tree,在 div 這個 R-Node 中 的 R-Pattern 是((0|1|2)^*),表示依照資料來源的型態來選擇對應的 child 0,child 1,或是 child 2,並且重複直到資料來源用完為止。
div
RT-Node R-DataSource: Para
R-Pattern: (0) R-Tag: p RT-Node
圖 3-10: 範例三的 Regeneration Tree