1 / 50

經濟部工業局電子化人才培訓課程 清華大學工業工程與工程管理學系 課程名稱:電子商務管理與技術

經濟部工業局電子化人才培訓課程 清華大學工業工程與工程管理學系 課程名稱:電子商務管理與技術. eXtensible Markup Language (XML). 主題. 1.XML 基礎 2. 建立 XML 文件 3.XML 語法檢查 (DTD/XML Schema) 4.XSLT 、 XPath 轉換 XML 文件 5.XML Document Object Model. 1.XML 基礎. 文件資料的物件化.

Télécharger la présentation

經濟部工業局電子化人才培訓課程 清華大學工業工程與工程管理學系 課程名稱:電子商務管理與技術

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. 經濟部工業局電子化人才培訓課程清華大學工業工程與工程管理學系課程名稱:電子商務管理與技術經濟部工業局電子化人才培訓課程清華大學工業工程與工程管理學系課程名稱:電子商務管理與技術 eXtensible Markup Language (XML)

  2. 主題 • 1.XML基礎 • 2.建立XML文件 • 3.XML語法檢查(DTD/XML Schema) • 4.XSLT、XPath轉換XML文件 • 5.XML Document Object Model

  3. 1.XML基礎

  4. 文件資料的物件化 • 一份電腦文件如Word檔案,資料內容本身是一份沒有結構性的文件。一份文件若能夠轉換成一個樹狀結構資料,我們就可以如同資料庫一般撰寫程式進行資料的排序、 搜尋。(如圖書產品型錄) • 如果所有的文件都可以使用相同的樹狀結構進行分析,如此文件的格式就可以統一,電腦間的檔案資料交換也更加的容易。

  5. Root element -> Nested elements -> Hierarchical structure

  6. What is XML? XML: eXtensible Markup Language defines an universal standard for electronically exchanging data for the Internet and Web applications and is supported by major IT vendors. XML在書寫上非常類似HTML,注重資料的內容而不是如何顯示,沒有如HTML的預設標籤,並非取代HTML。 使用者必須自行定義描述資料所需的各種標籤以傳輸有結構性的資料,即須注意良好格式(Well-Formed)和資料驗證(Validity)。

  7. XML用途 • XML儲存HTML顯示文件內容:使用XML技術動態產生所需的HTML網頁。 • XML可以作為資料交換的格式 • XML應用在電子商務:在企業間(B2B) 電子商務交易,XML將是資料交換企業整合的重要技術與標準。

  8. XML can take large chunks of information and consolidate them into an XML document - meaningful pieces that providestructure and organization to the information.

  9. XML相關技術關聯圖

  10. 文件資料驗證 • 所謂的驗證即是定義一組規則,這套規則可以檢查XML文件的架構和標籤是否合乎規則。 • DTD(Document Type Definition):為SGML之語法檢查,能幫助XML剖析器來解譯XML文件內容。 • XML Schema的架構就是一份XML文件,所以在使用上和撰寫一份XML文件一般,不用像DTD需要學習另一種語法。

  11. 顯示輸出和文件轉換 • XSL(Extensible Stylesheet Language)的功能有兩項:一為顯示XML的內容,另一就是文件轉換,也就是XSLT。使用XSLT可以將XML的樹狀結構轉換成另一架構,如純文字文件或HTML文件。 • XSL屬於一種樣式程式語言,其提供比CSS(Cascading Style sheets)更加強大的XML文件顯示的功能。CSS主要用來支援簡單的XML架構,而XSL則是使用於複雜的XML架構。

  12. XML軟體需求 • XML剖析器(Parser):所謂剖析為依照標籤解讀文件。電腦本身雖看不懂XML文件內容,但是透過標籤幫助,剖析器就可以分析文件的內容取出所需的資訊。MSXML Parser 為微軟附在IE的剖析器,只要5.x版就擁有XML剖析器。 • XML驗證工具:通常XML編輯和瀏覽亦會提供驗證。 • XML編輯工具:UltraEdit、XML Notepad、XML Spy。 • XML瀏覽工具:IE 5.x可以如同瀏覽HTML文件一般瀏覽XML文件。

  13. 2.建立XML文件

  14. XML文件組成因子 • 元素(Element):XML元素為整個文件的主要架構,為一完整的項目,包含了標籤、屬性、開始標籤、結尾標籤和其中的文字。 • 屬性(Attribute):開始標籤名稱後可為標籤加上屬性名稱和屬性值,且值要用雙引號括起來。 • 實體參考(Entity Reference):由於標籤語言本身有一些保留符號,此時會用到實體參考。 • 註解(Comment):由<!-- -->表示 • CDATA Section和PCDATA:在Tag中包含的文字為PCDATA;如果XML有CDATA Section,則告訴Parser不要處理此區塊的內容。CDATA Section位於<![CDATA[ ]]>之間,通常是用來儲存程式碼的資料。 • PI(Processing Instructions):PI允許文件包含應用程式的指令,這些資訊告訴XML剖析器如何處理XML文件,並將這些資訊送給應用程式。其基本格式以<? name ?>表示。

  15. 標準XML注意事項 • 根標籤:所有XML文件都需要一對標籤來定義文件的根元素,其它元素都屬於此根元素之子元素。 • XML標籤可以使用中文,但英文大小寫不同 • 所有元素需要有結尾標籤 • 巢狀XML元素不可重疊 • 屬性值必須要有雙引號

  16. Rules for Well-Formed XML • Mandatory Closing Tag • The set of tags are unlimited but all container tags must have end tag, e.g., <tag>…</tag>. • Example: • <number>kj9876_34</number> • <due>1/12/98</due> • <bill_to>MTB1</bill_to> • <ship_to>MTO1</ship_to> • <selling_party>AC987</selling_party>

  17. Rules for Well-Formed XML (cont.) • Proper Element Nesting • All tags must be nested correctly. Like HTML, XML can intermix tag, but tags may not overlap each other. • Legal XML <PRODUCT> <ID>721</ID> <NAME>PENTIUM 11 300</NAME> <PRICE>5000</PRICE> </PRODUCT> Illegal XML <PRODUCT> <ID> <NAME>PENTIUM 11 300<NAME> <PRICE>5000</PRICE> </PRODUCT> </ID>

  18. Rules for Well-Formed XML (cont. 2) • Double-quote value delimiters • All attribute values must be enclosed in single or double quotation marks. • Legal: • <tag attribute=“value”> • Illegal: • <font size = 6> <XML> <xml> are different

  19. Rules for Well-Formed XML (cont. 3) • Single tag element • Singleton tag (called empty element or tags without content) must be written in an abbreviated form using special XML syntax. • Legal: • <BR/> • <TITLE></TITLE> is equivalent to <TITLE/>

  20. XML範例 (product.xml) <?xml version="1.0" encoding="Big5"?> <!--product.xml edited by jack--> <PRODUCTLIST> <PRODUCT> <ID>721</ID> <NAME>PENTIUM 100</NAME> <PRICE>1000</PRICE> </PRODUCT> <PRODUCT> <ID>722</ID> <NAME>PENTIUM 11 300</NAME> <PRICE>5000</PRICE> </PRODUCT> </PRODUCTLIST>

  21. Exercise 1.booklist含有子元素book兩個 2.book含有子元素code title author price 3.Tag中包含的文字自訂 Hint:<?xml version=“1.0” encoding=“Big5”?>

  22. XML文件的名稱空間(Namespace) • 如果XML文件需要引用其它XML架構或元素,或是與其它XML文件進行資料交換,我們必須要在XML文件宣告名稱空間,以避免標籤名稱重複的問題。 • 為了取得唯一的標籤名稱,名稱空間使用網址的名稱和路徑,我們稱為 URI (Universal Resource Identifier) 。例<PRODUCT xmlns = http://www.company.com/ > • 標籤的完整名稱: <名稱空間字頭:標籤名稱> 例如: <bk:title> <jb:title> 亦即前面的名稱空間不同,就屬於不同的標籤。

  23. 名稱空間範圍 <?xml version="1.0" encoding="Big5" ?> <booklist> <book xmlns="http://www.company.com/"> <title>abc123</title> <authorlist> <author>jack</author> <jobxmlns:jb="http://www.ebec.com.tw/"> < companyname > ebec </companyname> <title>student</title> </job> </authorlist> </book> </booklist>

  24. 3.XML語法檢查(DTD/XML Schema)

  25. DTD (Document Type Definition) • Schema是一種XML文件內容驗證機制,主要是定義文件出現的元素、 屬性和整份文件的架構,也就是元素擁有哪些子元素的內容模型(Content Models) 。 • 一旦XML文件定義好DTD,在使用編輯工具建立XML文件時,編輯工具就可以依據DTD檢查XML是否為合法的內容。 • XML1.0版的Schema預設為DTD,而XML Schema是由MS所提出,其目的就是要取代DTD成為下一代的XML驗證機制。

  26. 內部DTD <booklist> <book sales="Y"> <code>F0001</code> <title> abc </title> <authorlist no="1"> <author>jack1</author> </authorlist> <price>580</price> </book> <book sales="N"> <code>F0002</code> <title> def </title> <authorlist no="1"> <author>jack2</author> </authorlist> <price>550</price> </book> </booklist> <?xml version="1.0" encoding="Big5"?> <!--XML文件內部的DTD--> <!DOCTYPE booklist [ <!ELEMENT booklist (book+)> <!ELEMENT book(code, title, authorlist, price)> <!ATTLIST book sales (N | Y) #REQUIRED> <!ELEMENT code (#PCDATA)> <!ELEMENT title (#PCDATA)> <!ELEMENT authorlist (author+)> <!ELEMENT author (#PCDATA)> <!ATTLIST authorlist no CDATA #REQUIRED > <!ELEMENT price (#PCDATA)> ]>

  27. DTD元素宣告 • <!ELEMENT element_name (element_or_data)> • element_name:XML元素的標籤名稱 • element_or_data:定義元素內含的資料或是其下的子元素,如果不只一個請用「,」分隔,如下所示:child_element, child_element,…

  28. DTD元素使用符號說明

  29. DTD元素資料

  30. DTD元素宣告 <!ATTLIST element_name attribute_name attribute_type default_value> ‧element_name:屬性所屬的XML元素名稱 ‧attribute_name:屬性名稱 ‧attribute_type:指定屬性值的種類

  31. DTD屬性使用符號說明

  32. XML Schema • XML Schema本身屬於一份XML文件,其基本的架構和驗證的XML文件並沒有什麼不同,通常我們將XML Schema驗證的XML文件稱為Instance文件;而XML Schema文件稱為Schema文件。 • 基本架構如下所示: <? xml verson =“1.0” encoding= “Big5” ?> <xsd:schema xmlns:xsd=http://www.w3.org/1999/XMLSchema> …………………… </xsd:schema>

  33. XML Schema (cont.) • 元素Schema間之子元素就是在定義Instance文件的XML元素,常用的XML元素如下表所示:

  34. XML Schema (cont. 2) • 通常我們將XML Schema獨立存成一個Schema文件檔案,副檔名為.xsd,然後在XML的Instance文件指定使用的Schema檔案。 <booklist xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xsi:noNamespaceSchemaLocation= abcd.xsd"> • XML Schema支援多達四十種以上的資料型態,而且允許使用者自定資料型態,這些資料型態是使用在XML元素和內容。例如string、boolean、float、double等。

  35. XML Schema (cont. 3) (1) 0<= OrderQuality <= 100 <simpleType name = “OrderQuality”base= “integer”> <minInclusive value= “1”/> <maxInclusive value= “100”/> </simpleType> (2)列舉清單 <simpleType name= “color”base= “string”> <enumeration value= “red”/> <enumeration value= “white”/> <enumeration value= “blue”/> <enumeration value= “black”/> </simpleType> (3)正規語法 <simpleType name = “PhoneNumber”base= “string”> <length value= “11”/> <pattern value= “\d{2}-\d{8}”/> </simpleType> ps: \d代表為數字

  36. 4.XSLT、XPath轉換XML文件

  37. XSL(eXtensible Style Language) • XML文件本身只是在定義資料的內容,如果需要顯示XML元素的資料,我們需要描述XML元素如何被顯示,這種語言稱為樣式語言。CSS只是其中一個選擇,XSL才是配合XML的樣式語言。 • XSL主要功能分為兩部分: 1.轉換XML文件:將XML文件架構轉換成另一個XML架構的文件, 或是HTML文件。(稱為XSLT;XSL Transformations)目前支援 XML的瀏覽程式不多,所以XSLT的角色即相當重要。 2.格式化XML文件:格式化元素內容樣式,以便顯示出XML文件。

  38. XSLT是如何工作? • XSLT簡單的說,即是將一棵來源為XML的樹,套用XSLT轉換範本樹,修改一些節點結構,最後產生另一棵不同架構的樹狀結構。

  39. XPath Language • 因為XSLT屬於一種範本驅動的轉換,也就是說需要在XML文件正確描述XML元素的位置。XML文件能夠轉換成為一個樹狀結構,如何找到指定節點的定位指令就是XPath的目的。

  40. XPath 的位置路徑

  41. XPath範例XML • <?xml version="1.0" encoding="Big5"?> • <glossary> • <item> • <title version="EN">eXtensible Markup Language</title> • <definition>可擴充展標示語言<title>XML<title></definition> • </item> • <item> • <title version="EN">encoding</title> • <definition>字元集</definition> • </item> • <item> • <title version="EN">Uniform Resource Identifier</title> • <definition>統一資源識別符號<title>URI</title></definition> • </item> • </glossary>

  42. XPath樹狀結構 root <?xml?> glossary item item item title(version) title(version) title(version) definition definition definition text text text title title text text text

  43. 節點索引條件 • Xpath之位置路徑:

  44. XSLT基本架構 <?xml version="1.0" encoding="Big5"?> <xsl:stylesheet version="1.0" xmlns:xsl ="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> ……………………… ……………………… </xsl:template> </xsl:stylesheet>

  45. XSLT常用的元素

  46. XSLT範例1 (hello.xsl) <?xml version="1.0" encoding="Big5"?> <xsl:stylesheet version="1.0" xmlns:xsl ="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html><head> <title>測試XSLT</title> </head> <body> <xsl:value-of select="hello"/> </body> </html> </xsl:template> </xsl:stylesheet> hello.xml <?xml version="1.0" encoding="Big5"?> <?xml-stylesheet type="text/xsl" href="hello.xsl"?> <hello>這是一個測試!!</hello>

  47. XSL範例2 (LCD2.xsl)

  48. XSL範例3(LCD.xsl)

  49. References • XML的入門與應用 • http://www.hpdiy.com.tw/xml.htm • 實戰XML • http://www.softchina.com.tw • XML IE5 • http://webdev.wrox.co.uk/books/1576/ • XML-Data standard • http://www.w3.org/TR/NOTE-XML-data/ • XSL standard • http://www.w3.org/TR/NOTE-XML-data/

  50. XML網站推薦 • IBM網站 • http://www.ibm.com/developer/xml/ • 微軟網站 • http://msdn.microsoft.com/xml/default.asp • Free XML software • http://www.stud.ifi.uio.no/~lmariusg/linker/XMLtools.html • http://www.stud.ifi.uio.no/~lmariusg/linker/xmltools/by-platform.html

More Related