單元 5-2 : 撰寫 xsl 程式

14
單單 5-2 單單 XSL 單單 單單單 單單單單單單單

Upload: linus-rosario

Post on 31-Dec-2015

56 views

Category:

Documents


0 download

DESCRIPTION

單元 5-2 : 撰寫 XSL 程式. 王豐緒 銘傳大學資工系. 單元目標. 了解 XSL 處理器的運作原理 如何撰寫 XSL 程式. 範例 : What is the output ? (1/2). XML 文件. XSL 程式. < ? xml version=“1.0” ? > < xsl:stylesheet version=“1.0” xmlns:xsl =“http:// www.w3.org/1999/XSL/Transform”> - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 單元 5-2 : 撰寫 XSL 程式

單元 5-2:撰寫 XSL程式

王豐緒銘傳大學資工系

Page 2: 單元 5-2 : 撰寫 XSL 程式

單元目標

了解 XSL處理器的運作原理 如何撰寫 XSL程式

Page 3: 單元 5-2 : 撰寫 XSL 程式

範例 : What is the output? (1/2)

<?xml version=“1.0”?><!-- file name: games.xsl --> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>

<xsl:template match = “/”> <xsl:apply-templates /> </xsl:template>

<xsl:template match = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template>

<xsl:template match = “game”> <xsl:element name=“{@title}”> <xsl:attribute name=“id”>

<xsl:value-of select= “id” /> </xsl:attribute>

<comment> <xsl:value-of select= “para” />

</comment>

</xsl:element> </xsl:template>

</xsl:stylesheet>

<?xml version=“1.0”?><!-- file name: welcome.xml--> <?xml-stylesheet type=“text/xsl” href = “games.xsl” ?> <sports>

<game title = “cricket”><id>243</id><para> More popular!</para>

</game> <game title = “baseball”>

<id>433</id><para> Less popular!</para>

</game> <game title = “soccer”>

<id>856</id><para> Not popular!</para>

</game>

</sports>

XSL程式 XML文件

Page 4: 單元 5-2 : 撰寫 XSL 程式

範例 : What is the output? (2/2)

<sports>

<cricket id=“243”><comment> More popular!</comment>

</cricket >

<baseball id= “433”><comment > Less popular!</comment >

</baseball>

<soccer id= “856”><comment > Not popular!</comment>

</soccer>

</sports>

Page 5: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (1/6) 先將 XML檔案轉換成 XML DOM Tree

<?xml version=“1.0”?><?xml-stylesheet type=“text/xsl” href = “games.xsl” ?> <sports>

<game title = “cricket”><id>243</id><para> More popular!</para>

</game> <game title = “baseball”>

<id>433</id><para> Less popular!</para>

</game> <game title = “soccer”>

<id>856</id><para> Not popular!</para>

</game>

</sports>

/

sports

game

title=“cricket”

id

243para

More popular

game

id…

文件節點

Page 6: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (2/6) 載入 XSL檔案 , 建立規則庫

<?xml version=“1.0”?><xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform>

<xsl:template match = “/”> <xsl:apply-templates /> </xsl:template>

<xsl:template match = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template>

<xsl:template match = “game”> <xsl:element name=“{@title}”> <xsl:attribute name=“id”>

<xsl:value-of select= “id” /> </xsl:attribute>

<comment> <xsl:value-of select= “para” />

</comment>

</xsl:element> </xsl:template></xsl:stylesheet>

規則 1: match=“/”

規則 2: match=“sports”

規則 3: match=“game”

Page 7: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (3/6) 從 XML DOM Tree的文件節點 (’/’)開始尋找規則進行轉換 , 找到規則 1,進行轉換/

sports

game

title=“cricket”

id

243para

More popular

game

id…

規則 1: match=“/”

規則 2: match=“sports”

規則 3: match=“game”

Page 8: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (4/6)

規則 1:進行規則套用呼叫 (<xsl:apply-templates />), 對象是目前節點 (“/”)下所有的子節點 (就是“ sports”)› 尋找可以處理<sports>的規則 (規則 2)

<xsl:template match = “/”> <xsl:apply-templates /></xsl:template>

/

sports

規則 1: match=“/”

規則 2: match=“sports”

規則 3: match=“game”

(2)

(1)

Page 9: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (5/6)

規則 2: 先產出<sports>標籤 , 其內容則是繼續套用規則呼叫(<xsl:apply-templates />), 對象是目前節點 (“sports”)下所有的子節點 (3個“ game”),依序尋找可套用規則 (規則 3)…

<xsl:template match = “sports”> <sports> <xsl:apply-templates /> </sports></xsl:template>

sports

game

game

game

(1)

(2)

(3)

規則 3: match=“game”

規則 3: match=“game”

規則 3: match=“game”

XSL 規則XML DOM Tree

Page 10: 單元 5-2 : 撰寫 XSL 程式

XSL運作過程 (6/6) 規則 3: 先建立一個以目前節點 (<game>)的@title屬性值為名的標籤

接著為其建立屬性 id,屬性值則是目前節點 (<game>)的子元素<id>的值

接著建立<comment>標籤 ,並以目前節點 (<game>)的子元素<para>的值作為內容

最後 , 規則被套用 3次後 , done!

<xsl:template match = “game”> <xsl:element name=“{@title}”> <xsl:attribute name=“id”>

<xsl:value-of select= “id” /> </xsl:attribute> <comment> <xsl:value-of select= “para” /> </comment> </xsl:element></xsl:template>

game

id

243para

More popular

title=“cricket”

<cricket id=“243”> <comment> More popular </comment></cricket>XSL 規則 XML DOM Tree

輸出

Page 11: 單元 5-2 : 撰寫 XSL 程式

如何撰寫 XSL程式

務必先了解 XSL的運作原理 了解題目所給 XML文件的結構 了解所欲轉換成的結果 (如 HTML網頁 ) 分析 XML與轉換結果的對應關係 撰寫 XSL的對應轉換規則 測試 -除錯 -修改 -直到完成

Page 12: 單元 5-2 : 撰寫 XSL 程式

範例 :分析對應關係<?xml version=“1.0”?><!-- file name: welcome.xml--><sports> <game title = “cricket”>

<id>243</id><para> More popular!</para>

</game> <game title = “baseball”>

<id>433</id><para> Less popular!</para>

</game> <game title = “soccer”>

<id>856</id><para> Not popular!</para>

</game> </sports>

<sports>

<cricket id=“243”><comment> More popular!</comment>

</cricket >

<baseball id= “433”><comment > Less popular!</comment >

</baseball>

<soccer id= “856”><comment > Not popular!</comment>

</soccer>

</sports>

1. 分析對應關係(注意顏色對應 )2.<comment>是新設標籤

Page 13: 單元 5-2 : 撰寫 XSL 程式

範例 : 撰寫對應規則<?xml version=“1.0”?><!-- file name: games.xsl --> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform>

<xsl:template match = “/”> <xsl:apply-templates /> </xsl:template>

<xsl:template match = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template>

<xsl:template match = “game”> <xsl:element name=“{@title}”> <xsl:attribute name=“id”>

<xsl:value-of select= “id” /> </xsl:attribute>

<comment> <xsl:value-of select= “para” />

</comment>

</xsl:element> </xsl:template>

</xsl:stylesheet>

Page 14: 單元 5-2 : 撰寫 XSL 程式

單元複習 追蹤更複雜的 XSL程式以了解 XSL處理器的運作原理 如何撰寫 XSL程式的一般性原則