1 / 20

單元 5-1 : XSL XML 的轉換 語言

單元 5-1 : XSL XML 的轉換 語言. 王豐緒 銘傳大學資工系. 單元目標. 了解 XSL 與 XML 的關係 了解 XSL 基本語法與要素. XSL 轉換模板 (Transformation Templates). 什麼是 XSL ? XSL「 可延伸排版樣本語言」( eXtensible Stylesheet Language) 作為 定義 XML 文件 轉換樣式 的一種語言 利用 XSL 轉換格式 的能力可以將 XML 專換成各種開放的格式(如 XML, HTML 等) 在 XML 文件中宣告使用 XSL

clarke
Download Presentation

單元 5-1 : XSL XML 的轉換 語言

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-1:XSL XML的轉換語言 王豐緒 銘傳大學資工系

  2. 單元目標 • 了解XSL與XML的關係 • 了解XSL基本語法與要素

  3. XSL轉換模板(Transformation Templates) • 什麼是XSL? • XSL「可延伸排版樣本語言」(eXtensibleStylesheet Language) • 作為定義XML文件轉換樣式的一種語言 • 利用XSL轉換格式的能力可以將XML專換成各種開放的格式(如XML, HTML等) • 在XML文件中宣告使用XSL • <?xml-stylesheettype="text/xsl"href=“XSL檔案位址"?>

  4. XSL與XML的關係 • XSL是由一組轉換模板(Transformation Template)所組成 <?xml version=“1.0?> <?xml-stylesheet type="text/xsl" href ="test.xsl" ?> <booklist> <book> <title>Old Man</title> <author>F. H. Wang</author> </book> <book> <title>Goodness</title> <author>C. T. Lee</author> </book> </booklist> XML 文件 XSLT 處理器 轉換 結果(HTML) XSL 文件 (只抓到第一組?) <xsl:stylesheet version = “1.0” xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:templatematch="/"> <h1> <xsl:value-ofselect="//title"/> </h1> <h2> <xsl:value-ofselect="//author"/> </h2> </xsl:template> </xsl:stylesheet> <h1>Old Man</h1> <h2>F. H. Wang</h2> 轉換模板

  5. XSL語法基本概念 • XSL程式本身也是XML文件 • 由一堆轉換規則所組成(甚至沒有任何規則也可以) • 每一條規則利用XPATH指令指定規則的啟動節點 • 規則內部則指定轉換的動作, 或針對其他節點(也是用XPATH指令描述)啟動相關規則 • <xsl:templatematch="xpath" > • <h1>Test XSL</h1> • <xsl:apply-templates select=“xpath2“ /> • </xsl:template>

  6. XSL程式執行的基本概念 • XSL程式的執行是資料(節點)導向 XML DOM Tree XSL 程式 Rule 1: Action1 Rule 2: Action2 Rule 3: Action3 … Rule N: ActionN XSL Processor Action 輸出結果

  7. 一個簡單的範例 • <xsl:templatematch=“sports" > • <h1> • <xsl:apply-templates select=“game“ /> • </h1> • </xsl:template> • <xsl:templatematch=“game" > • <p> • <xsl:value-of select=“@title“ /> is • <xsl:value-of select=“.“ /> • </p> • </xsl:template> <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> / / sports sports title=“baseball” <h1>….. </h1> game game XSL Processor A popular game in Taiwan <p>baseball is A popular game in Taiwan </p>

  8. XSL基本指令元素 • xsl:stylesheet • XSL文件的根元素 • <xsl:stylesheet>....</xsl:stylesheet> • xsl:template • <xsl:templatematch=”xpath” > • 利用「match」設定xpath指令設定本規則的啟動節點 • xsl:apply-templates • <xsl:apply-templatesselect=“xpath” /> • 在xpath指定的節點上逐一套用適當的模板 • xsl:value-of • <xsl:value-ofselect=“xpath”/> • 取出xpath定位到的第一個節點的值

  9. 另一個範例 • <xsl:stylesheetversion=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> • <!-- 沒有任何規則 --> • </xsl:stylesheet> <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> / / sports sports title=“baseball” game game A popular game in Taiwan XSL Processor A popular game in Taiwan A popular game in Taiwan

  10. XSL預設的模板(Templates) • <xsl:template match=“/| *”> <xsl:apply-templates /></xsl:template> • 此模板比對document node 以及element node,然後對其孩子節點繼續套用適當的模板 • <xsl:template match=“text() | @*”> <xsl:value-of select=“.” /></xsl:template> • 此模板比對任何的文字節點以及屬性節點,然後顯示該節點的值 • <xsl:template match=“processing-instruction()| comment()”/> • 此模板比對任何的PI節點以及註解節點,然後不作任何處理(也就是忽略這些節點)

  11. 新增元素與屬性的XSL指令 • 新增元素 • 在輸出文件中建立一個元素標籤 • <xsl:element name=”新增元素名稱”>...</xsl:element> • 建立屬性 • 在輸出文件的元素內插入屬性 • 運用在一個元素標籤內或上述的<xsl:element>之內 • <xsl:attribute name=”屬性名稱">屬性的值<xsl:attribute> • 取得屬性 • 括弧 { } 加「@」 • 如<xsl:element name=“{@title}”> … </xsl:element>

  12. 新增元素與屬性的XSL範例 <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> • <xsl:templatematch=“game" > • <xsl:elementname=“{@title}”> • <xsl:attributename=“des“ > • <xsl:value-of select=“.“ /> • </xsl:attribute> • 我是新增的元素 • </xsl:element> • </xsl:template> / / <baseball des=“A popular …”> 我是新增的元素 </baseball> sports sports title=“baseball” game game XSL Processor A popular game in Taiwan

  13. XSL的條件判斷指令 • 唯一條件判斷 • <xsl:if test=”判斷條件”>...</xsl:if> • 多重條件判斷 <xsl:choose> <xsl:whentest=”條件”>...</xsl:when> ..... <xsl:otherwise>...</otherwise> </xsl:choose>

  14. 條件判斷的XSL範例 • <xsl:templatematch=“game" > • <xsl:iftest=“@title=‘baseball’“ > • <font color=“red”> • <xsl:value-of select=“@title“ /> is • <xsl:value-of select=“.“ /> • </font> • </xsl:if> • </xsl:template> <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> / / <font color=“red”> baseball is A popular game in Taiwan </font> sports sports title=“baseball” game game XSL Processor A popular game in Taiwan

  15. XSL的迴圈指令 • xsl:for-each • <xsl:for-eachselect=“xpath” >…(裡面的參考節點是xpath所定位的節點)</xsl:for-each> • <xsl:sortorder= “ascending | descending” select=“xpath2”/> • 將for-each的結果依照某種xpath2順序來依序處理(遞增或遞減)

  16. 迴圈控制的XSL範例 • <xsl:templatematch=“sports" > • <xsl:for-each select=“game” > • <xsl:sortorder= “descending” select=“@title”/> • <p> • <xsl:value-of select=“@title“ /> is • <xsl:value-of select=“.“ /> • </p> • </xsl:for-each> • </xsl:template> <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> <game title = “tennisl”> A less popular game in Taiwan! </game> </sports> <p> tennis is A less popular game in Taiwan </p> <p> baseball is A popular game in Taiwan </p> XSL Processor

  17. 其他XSL基本元素 • xsl:comment • <xsl:comment>comment</xsl:comment> • 輸出備註資料 • xsl:copy • <xsl:copy>…</xsl:copy> • 將目前處理的節點輸出 • (不包括子節點以及屬性節點) • xsl:copy-of • <xsl:copy-of select=“xpath”/> • 將xpath所定位的XML節點輸出 • 包括其子孫節點以及屬性節點

  18. 節點複製的XSL範例一 • <xsl:templatematch=“sports" > • <xsl:copy > • <p> • <xsl:value-of select=“game/@title“ /> is • <xsl:value-of select=“game“ /> • </p> • </xsl:copy> • </xsl:template> <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> <sports> <p> baseballis A popular game in Taiwan </p> </sports> XSL Processor

  19. 節點複製的XSL範例二 <?xml version=“1.0”?> <sports> <game title = “baseball”> A popular game in Taiwan! </game> </sports> • <xsl:templatematch=“sports" > • <xsl:copy-of select=“game”/> • </xsl:template> XSL Processor <game title = “baseball”> A popular game in Taiwan! </game>

  20. 單元複習 • XSL可作為XML文件的格式轉換器 • 介紹XSL基本語法與要素 • XSL程式的架構與執行概念 • XSL轉換規則的撰寫 • 各種轉換動作 • 新增元素,屬性 • 條件控制 • 迴圈控制 本投影片謹簡述若干基本XSL元素,同學仍須參考課本或講義更多細節

More Related