1 / 39

行政院研究發展考核委員會 政府網站共用元件推廣課程

行政院研究發展考核委員會 政府網站共用元件推廣課程. 政府網站共用元件客製化進階教育訓練. 凌網科技 廖宜哲 99 年 9 月 3 日. 課程大綱. XSLT 介紹 相關名詞解釋 用途 XSLT 範例 XSLT 語法與應用 XSL & XML XSL 各種重要元素 XPath 語法. 課程大綱. 政府網站共用元件與 XSLT 新增套版應用在不同頁面 範例演練. XSLT 介紹. 名詞解釋. XML = EXtensible Markup Language XSL = EXtensible Stylesheet Language

shiela
Download Presentation

行政院研究發展考核委員會 政府網站共用元件推廣課程

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. 行政院研究發展考核委員會政府網站共用元件推廣課程行政院研究發展考核委員會政府網站共用元件推廣課程 政府網站共用元件客製化進階教育訓練 凌網科技 廖宜哲 99年9月3日

  2. 課程大綱 • XSLT介紹 • 相關名詞解釋 • 用途 • XSLT範例 • XSLT語法與應用 • XSL & XML • XSL 各種重要元素 • XPath語法

  3. 課程大綱 • 政府網站共用元件與XSLT • 新增套版應用在不同頁面 • 範例演練

  4. XSLT介紹

  5. 名詞解釋 • XML=EXtensible Markup Language • XSL = EXtensible Stylesheet Language • CSS = Style Sheets for HTML • XSL = Style Sheets for XML

  6. XML範例 <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> </catalog>

  7. XSL範例 <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  8. 組合的結果

  9. 什麼是XSLT • XSLT =XSL Transformations • XSLT是XSL最重要的功能 • XSLT可以把XML轉換成另一種格式的XML(HTML) • XSLT使用XPATH來查詢XML文件 • XSLT是W3C建議的功能規格

  10. 支援XSLT的瀏覽器 • Mozilla Firefox • Firefox supports XML, XSLT, and XPath from version 3. • Internet Explorer • Internet Explorer supports XML, XSLT, and XPath from version 6. • Google Chrome • Chrome supports XML, XSLT, and XPath from version 1. • Opera • Opera supports XML, XSLT, and XPath from version 9. Opera 8 supports only XML + CSS. • Apple Safari • Safari supports XML and XSLT from version 3.

  11. XSLT語法與應用

  12. XSL文件的宣告 • XSL也是一種XML也需要XML規定的宣告 • <?xml version="1.0" encoding="ISO-8859-1"?> • 宣告文件使用W3C定義的XSL格式 • <xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • 要加上結束標籤</xsl:stylesheet>

  13. XML&XSL • 在XML中宣告使用的XSL文件 • <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> • http://www.w3schools.com/xsl/cdcatalog.xml • http://www.w3schools.com/xsl/cdcatalog.xsl

  14. <xsl:template> • <xsl:template match="/"> • 此元素用來建立模板 • Match屬性用來關連XML文件 • Match屬性使用XPATH語法

  15. <xsl:template> <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <tr>      <td>.</td>      <td>.</td>    </tr>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

  16. <xsl:value-of> • <xsl:value-of select="catalog/cd/title"/> • 此元素用來提取XML中的資料 • 可以提取XML的標籤內容,也可以提取屬性 • select屬性使用XPATH語法 • NOTE:XPath • 把XML想成檔案總管的樹狀結構,用檔案路徑的方式來找到你要的資料。

  17. <xsl:value-of> <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <tr>      <td><xsl:value-of select="catalog/cd/title"/></td>      <td><xsl:value-of select="catalog/cd/artist"/></td>     </tr>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

  18. <xsl:for-each> • <xsl:for-each select="catalog/cd"> • 此元素用迴圈的方式來提取XML中的資料 • select屬性使用XPATH語法 • 要加上結束標籤</xsl:for-each>

  19. <xsl:for-each> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0”xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  20. XPath過濾資料 • NOTE:XPATH(可以過濾資料) • <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> • 找出演唱者是Bob Dylan的所有的CD • 運算子 • =  (equal等於) • != (not equal不等於) • &lt; less than(小於) • &gt; greater than(大於)

  21. <xsl:sort> • <xsl:sort select="artist"/> • 排序資料 • 放在迴圈裡面 • 空標籤語法,所以不需要結束標籤

  22. <xsl:sort> <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0”xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd"><xsl:sort select="artist"/>      <tr>        <td><xsl:value-of select="title"/></td>        <td><xsl:value-of select="artist"/></td>      </tr>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

  23. <xsl:if> • <xsl:if test=“判斷句”>符合判斷句要顯示的資料</xsl:if> • 用來判斷資料 • 判斷句:price &gt; 10(價格大於10) • 空標籤語法,所以不需要結束標籤

  24. <xsl:if> <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0”xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10">        <tr>          <td><xsl:value-of select="title"/></td>          <td><xsl:value-of select="artist"/></td>        </tr> </xsl:if>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

  25. <xsl:choose> • <xsl:choose> <xsl:when test=“判斷句"> 輸出 </xsl:when> <xsl:otherwise> 輸出 </xsl:otherwise> </xsl:choose> • 多重條件判斷,類似if…Else…if • <xsl:when test=“判斷句”>可以多個 • 空標籤語法,所以不需要結束標籤

  26. <xsl:choose>     <xsl:for-each select="catalog/cd">    <tr>      <td><xsl:value-of select="title"/></td><xsl:choose><xsl:when test="price &gt; 10">          <td bgcolor="#ff00ff">          <xsl:value-of select="artist"/></td></xsl:when>        <xsl:when test="price &gt; 9">          <td bgcolor="#cccccc">          <xsl:value-of select="artist"/></td>        </xsl:when>        <xsl:otherwise>          <td><xsl:value-of select="artist"/></td>        </xsl:otherwise>      </xsl:choose>    </tr>  </xsl:for-each>

  27. <xsl:apply-templates> • <xsl:apply-templates/> • 套用其他模板 • 使模板可以被多重使用 • 空標籤語法,所以不需要結束標籤 • http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_apply

  28. XPath

  29. 什麼是XPath? • 一種定義XML的某部份資料的語言 • 用類似檔案路徑的方式瀏覽XML • 有基本的函數可以使用 • 在XSLT中是非常重要的一部份

  30. 名詞解釋 • 父節點 • 子節點 • 平輩節點 • 祖先節點 • 後裔節點 <?xml version="1.0" encoding="ISO-8859-1"?><bookstore>  <book>    <title lang="en">Harry Potter</title>    <author>J K. Rowling</author>    <year>2005</year>    <price>29.99</price>  </book></bookstore>

  31. 選取節點

  32. 範例XML <?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book>  <title lang="eng">Harry Potter</title>  <price>29.99</price></book><book>  <title lang="eng">Learning XML</title>  <price>39.95</price></book></bookstore>

  33. 選取節點範例

  34. 謂詞(過濾函式)

  35. 萬用字元 • NOTE:萬用字元用來代替節點或屬性名稱

  36. 運算子

  37. XPath練習 • 範例下載:http://www.w3schools.com/xpath/books.xml • 找出所有的書名 • 找出分類是Web的書作者 • 找出出版年份在2004年以後的書名

  38. 政府網站共用元件應用

  39. 簡報完畢敬請指教

More Related