1 / 14

單元 5-2 : 撰寫 XSL 程式

單元 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”>

Télécharger la présentation

單元 5-2 : 撰寫 XSL 程式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 單元5-2:撰寫XSL程式 王豐緒 銘傳大學資工系

  2. 單元目標 • 了解XSL處理器的運作原理 • 如何撰寫XSL程式

  3. 範例: 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>

  4. 範例: 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>

  5. / 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>

  6. 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”

  7. 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 …

  8. 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)

  9. 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

  10. 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規則 輸出

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

  12. 範例:分析對應關係 <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> 是新設標籤

  13. 範例: 撰寫對應規則 <?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>

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

More Related