140 likes | 247 Views
單元 5-2 : 撰寫 XSL 程式. 王豐緒 銘傳大學資工系. 單元目標. 了解 XSL 處理器的運作原理 如何撰寫 XSL 程式. 範例 : What is the output ? (1/2). XML 文件. XSL 程式. < ? xml version=“1.0” ? > <!-- file name: games.xsl --> < xsl:stylesheet version=“1.0” xmlns:xsl =“http:// www.w3.org/1999/XSL/Transform”>
E N D
單元5-2:撰寫XSL程式 王豐緒 銘傳大學資工系
單元目標 • 了解XSL處理器的運作原理 • 如何撰寫XSL程式
範例: What is the output? (1/2) XML文件 XSL程式 <?xml version=“1.0”?> <!-- file name: games.xsl--> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:templatematch = “/”> <xsl:apply-templates /> </xsl:template> <xsl:templatematch = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template> <xsl:templatematch = “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>
範例: What is the output? (2/2) <sports> <cricketid=“243”> <comment> More popular! </comment> </cricket > <baseballid= “433”> <comment > Less popular! </comment > </baseball> <soccerid= “856”> <comment > Not popular! </comment> </soccer> </sports>
/ title=“cricket” sports game id 243 para More popular game id … 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>
XSL運作過程(2/6) • 載入XSL檔案, 建立規則庫 <?xml version=“1.0”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform> <xsl:templatematch = “/”> <xsl:apply-templates /> </xsl:template> <xsl:templatematch = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template> <xsl:templatematch = “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”
XSL運作過程(3/6) • 從XML DOM Tree的文件節點(’/’)開始尋找規則進行轉換, 找到規則1,進行轉換 / title=“cricket” sports 規則1: match=“/” 規則2: match=“sports” 規則3: match=“game” game id 243 para More popular game id …
XSL運作過程(4/6) • 規則1:進行規則套用呼叫(<xsl:apply-templates />), 對象是目前節點(“/”)下所有的子節點(就是“sports”) • 尋找可以處理<sports>的規則(規則2) / <xsl:templatematch = “/”> <xsl:apply-templates /> </xsl:template> (1) sports 規則1: match=“/” 規則2: match=“sports” 規則3: match=“game” (2)
XSL運作過程(5/6) • 規則2: 先產出<sports>標籤, 其內容則是繼續套用規則呼叫(<xsl:apply-templates />), 對象是目前節點(“sports”)下所有的子節點(3個“game”),依序尋找可套用規則(規則3)… sports (1) <xsl:templatematch = “sports”> <sports> <xsl:apply-templates /> </sports> </xsl:template> game 規則3: match=“game” (2) game 規則3: match=“game” (3) game 規則3: match=“game” XSL規則 XMLDOMTree
XSL運作過程(6/6) • 規則3: 先建立一個以目前節點(<game>)的@title屬性值為名的標籤 • 接著為其建立屬性id,屬性值則是目前節點(<game>)的子元素<id>的值 • 接著建立<comment>標籤,並以目前節點(<game>)的子元素<para>的值作為內容 • 最後, 規則被套用3次後, done! title=“cricket” game <xsl:templatematch = “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> id 243 para <cricket id=“243”> <comment> More popular </comment> </cricket> More popular XMLDOMTree XSL規則 輸出
如何撰寫XSL程式 • 務必先了解XSL的運作原理 • 了解題目所給XML文件的結構 • 了解所欲轉換成的結果(如HTML網頁) • 分析XML與轉換結果的對應關係 • 撰寫XSL的對應轉換規則 • 測試-除錯-修改-直到完成
範例:分析對應關係 <sports> <cricketid=“243”> <comment> More popular! </comment> </cricket > <baseballid= “433”> <comment > Less popular! </comment > </baseball> <soccer id= “856”> <comment > Not popular! </comment> </soccer> </sports> <?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> 1. 分析對應關係 (注意顏色對應) 2.<comment> 是新設標籤
範例: 撰寫對應規則 <?xml version=“1.0”?> <!-- file name: games.xsl--> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform> <xsl:templatematch = “/”> <xsl:apply-templates /> </xsl:template> <xsl:templatematch = “sports”> <sports><xsl:apply-templates /></sports> </xsl:template> <xsl:templatematch = “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>
單元複習 • 追蹤更複雜的XSL程式以了解XSL處理器的運作原理 • 如何撰寫XSL程式的一般性原則