1 / 92

< 標題 > XML 資料庫介紹 </ 標題 >

<?xml version=“1.0”?> < 簡報 > < 封面 >. < 標題 > XML 資料庫介紹 </ 標題 >. < 報告人 > 張景堯 </ 報告人 > < 單位 > 政治大學電子計算機中心 </ 單位 > < 單位 > 政治大學資訊管理系 </ 單位 > < 報告日期 > 2002 年 9 月 4 日 </ 報告日期 >. </ 封面 > < 內容 >. Agenda. 相關議題 XML/DB 相關產品種類定義 現有 RDBMS 支援 XML 的解決方案 MS SQL Server & XML Oracle ’ s XDK

nay
Download Presentation

< 標題 > 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. <?xml version=“1.0”?> <簡報> <封面> <標題>XML資料庫介紹</標題> <報告人>張景堯</報告人> <單位>政治大學電子計算機中心</單位> <單位>政治大學資訊管理系</單位> <報告日期>2002年9月4日</報告日期> </封面> <內容>

  2. Agenda • 相關議題 • XML/DB 相關產品種類定義 • 現有RDBMS支援XML的解決方案 • MS SQL Server & XML • Oracle’s XDK <!-- 中場休息吃點心 --> • 原生式XML資料庫 • Xindice • Tamino • 於電子公文系統與SCORM教材工廠應用

  3. XML是資料庫嗎? • 以極嚴謹角度思考答案為─不是 • 當”XML”只是一份XML文件時; • 它頂多能視為資料的集合,跟檔案一樣。 • 自身沒有任何額外的軟體(通常跟RDBMS有關連)幫助處理或管理文件資料 • 從較寬鬆角度思考答案為─是的 • 當”XML”除了是一份XML文件時; • 我們如果考慮到許多XML周邊所提供的能力,如:資料存放(XML 文件)、架構輪廓(DTDs, XML Schema)、查詢語言(XQuery, XPath, XQL, XML-QL, QUILT, etc.)及程式介面(SAX, DOM, JDOM)

  4. 什麼是XML所欠缺的? • 有效率的儲存能力 • 文件索引功能 • 文件安全性 • 交易與資料完整性 • 多使用者存取能力 • 資料觸發能力(Triggers) • 於多重文件上的查詢能力

  5. 什麼因素讓你想用資料庫? • 請自問底下問題 • 是否想要把過去舊有資料(Legacy Data) 呈現出來? • 您是否需要個儲存網頁的地方? • 資料庫是否用在某些資料交換是採用XML的應用上,諸如電子商務應用? • 您打算存的是資料還是文件?

  6. 資料vs.文件 • XML文件可初略分為兩類: • 以資料為中心(Data-centric) • 以文件為中心(Document-centric) • 兩者差別並不那麼明顯 • 然而,這樣的分類可幫助吾人做出採用何種資料庫系統之正確決策。

  7. 以資料為中心的XML文件 • 具備文件結構非常有規律的特徵 • 資料很細小(Fine-grained)即資料的最小獨立單位是PCDATA元素或屬性而已。 • 文件中極少或沒有混雜的資料內容。 • 同階層的元素順序及PCDATA出現點並不重要。 • 通常是設計來給機器用的文件。 • 文件多僅用在資料交換上。 • 例如:訂單、飛機時刻表、餐廳菜單等。

  8. 範例 <Orders> <SalesOrder SONumber="12345"> <Customer CustNumber="543"> <CustName>ABC Industries</CustName> <Street>123 Main St.</Street> <City>Chicago</City> <State>IL</State> <PostCode>60609</PostCode> </Customer> <OrderDate>981215</OrderDate> <Line LineNumber="1"> <Part PartNumber="123"> <Description> <p><b>Turkey wrench:</b><br /> Stainless steel, one-piece construction, lifetime guarantee.</p> </Description> <Price>9.95</Price> </Part> <Quantity>10</Quantity> </Line> <Line LineNumber="2"> <Part PartNumber="456"> <Description> <p><b>Stuffing separator:<b><br /> Aluminum, one-year guarantee.</p> </Description> <Price>13.27</Price> </Part> <Quantity>5</Quantity> </Line> </SalesOrder> </Orders>

  9. 以文件為中心的XML文件 • 特色是文件結構無規律 • 一份文件資料通常較大,即資料的最小獨立單位可能會是混雜式元素或另一份文件。 • 文件中充斥著混雜的資料內容。 • 同階層的元素順序及PCDATA出現點很重要。 • 通常是設計來給人類閱讀的文件。 • 例如:書、電子郵件、廣告單、教材描述文件以及幾乎概括所有XHTML文件等。

  10. 範例 <record xmlns="http://www.imsproject.org/metadata/"> <metametadata>   <metadatascheme>SCORM</metadatascheme> </metametadata> <general> <title>   <langstring>XML資料庫</langstring>   </title>   <language>zh-TW</language> <description>   <langstring>簡介能處理XML之資料庫</langstring>   </description> <description>   <langstring>還有使用經驗分享</langstring>   </description>   </general> … </record>

  11. 資料、文件與資料庫 • 判斷您要儲存的文件是以資料為中心還是文件為中心有助於找出適用的資料庫。 • 一般來說,若是以資料為主的話,則資料可存放於關連式資料庫、物件導向資料庫或階層式資料庫,再透過中介軟體或資料庫本身所提供XML存取能來轉換XML文件。 • 若是以文件為主的話,則選用原生式XML資料庫或內容管理系統較恰當。

  12. XML/DB 相關產品種類定義與應用(1) • Middleware:中介軟體用來處理來自您應用程式的呼叫以便於XML文件及資料庫間做資料轉換工作;用於以資料為中心的應用上。 (MS ADO, IBM XLE, Oracle XSQL Servlet) • XML-Enabled Databases:具備XML能力的資料庫,擴充了原有資料庫功能可在XML文件與自身資料庫間轉換資料。主要用於以資料為中心的應用上。(Oracle, MS SQL, Sybase, DB2) • Native XML Databases:原生式XML資料庫以”自然”的方式存放XML。此名詞並未被明確定義,但概指被設計來能維護XML結構之資料庫。以資料為中心及文件為中心皆能應用。(Tamino, Xindice)

  13. XML/DB 相關產品種類定義與應用(2) • XML Servers: XML伺服器能提供在各分散式應用程式(如電子商務或B2B應用程式等)間傳遞以XML文件為形式的資料。主要用於以資料為中心的應用上。(HP Total-e-Business) • XML Application Servers: XML應用伺服器為Web應用伺服器提供將XML(通常是由動態網頁產生)送至使用者瀏覽器。以資料為中心及文件為中心皆能應用。(Cocoon, IBM Net.Data) • Content Management Systems:內容管理系統管理人類可讀的文件片段,提供包含編輯、版本控制及從現有片段建立新文件的功能。主要用於以文件為中心的應用上。(Content@XML, GEMt)

  14. XML/DB 相關產品種類定義與應用(3) • XML Query Engines: XML查詢引擎為單一獨立引擎用以查詢XML文件。以資料為中心及文件為中心皆能應用。 • XML Data Binding: XML資料連結能把XML文件連結成物件,有些產品甚至能讓您從資料庫來存取這類物件。用於以資料為中心的應用上。 • XML/DB相關產品清單,請參見: http://www.rpbourret.com/xml/XMLDatabaseProds.htm

  15. 現有RDBMS支援XML的解決方案

  16. 具備XML能力的RDBMS • 透過轉換機制支援以XML為輸出入格式。 • 因為轉換的負擔使效能下降。 • 系統開發時間拉長,管理成本也增加。 • 各RDBMS供應商的XML轉換機制是各自建立。 • 檢索:搜尋能力受限於XML與SQL指令的對映限制。 • 擴充性差:當文件結構不同時RDBMS的適應力不如原生式XML資料庫。 • 優點:可透過SQL指令查詢,不必額外訓練學習。

  17. 關連式資料庫系統與XML • 資料儲存問題 • 資料通常以BLOB型態存放(必須以額外工具才能進行功能受限的查詢) • 資料得經過正規化且存放在多重資料表裡 • 關連式資料庫並不懂XML或XML查詢語言 • 資料庫需外加一層來處理XML相關操作 • 造成不斷的雙向格式與結構轉換的負擔 • XSQL僅允許以XML格式檢索(而非存放) 關連式資料 • 用SQL為基礎的查詢會造成系統巨大負擔 • XML文件樹狀階層結構必須用複雜的資料表聯合才能查詢的了 • XML相關處理資訊是擺在資料庫核心之外 • 可能會浮現出潛在的參考正確性檢查(referential integrity)問題

  18. order Fname Andy Sold-on 19970317 Item Sold-to Price 5.95 Person Book Title Pi Author Smith Lname Lu 關連式資料庫系統與XML的對映 RDBMS ORDER Mapping Code PERSON ITEM BOOK • 效能不彰 • 查詢時會有複雜聯合 • 資料更新時參考正確性限制 • 擴充能力差 • 資料表充斥著外鍵

  19. Design RDBMS for Existing XML Source: Kevin William, etc, “Professional XML Databases” • Always create a primary key • 每個資料表皆加入一其數值會自動增值的欄位。 • 該欄名稱以元素名加上Key字來命名,如elementKey。 • 將該欄設定為主鍵值欄位。 • Basic table creation • 對每一結構性資料元素建立一個資料表。 • 若該元素只有一個母元素,則建立一個外來鍵欄位指向母元素。 • Handling multiple parent elements. • 若該元素在母元素只出現0次或1次的話,在母元素建立一外來鍵欄位指向子元素,並設定成選擇性的或必要的。 • 若該元素在母元素出現0到多次或1到多次的話,加一中間資料表來表示與母元素間的多對多關係。 • Representing text-only elements. • 對最多僅出現一次的純文字元素於其母元素資料表中增一欄位存放。 • 確保該欄位定義的大小足夠放該文字元素內容。 • 如該純文字元素為選擇性的,將欄位定義成可允許NULL值。

  20. Example for Rule 1 & 2 <!ELEMENT Invoice (Customer, LineItem*)> <!ELEMENT Customer (#PCDATA)> <!ELEMENT LineItem (#PCDATA)> CREATE TABLE Customer ( CustomerKey integer PRIMARY KEY) CREATE TABLE Invoice ( InvoiceKey integer PRIMARY KEY, CustomerKey integer CONSTRAINT FK_Invoice_Customer FOREIGN KEY (CustomerKey) REFERENCES Customer (CustomerKey)) CREATE TABLE LineItem ( LineItemKey integer PRIMARY KEY, InvoiceKey integer CONSTRAINT FK_LineItem_Invoice FOREIGN KEY (InvoiceKey) REFERENCES Invoice (InvoiceKey))

  21. Example for Rule 3 <!ELEMENT Invoice (Customer, LineItem*)> <!ELEMENT Customer (Address)> <!ELEMENT LineItem (Product)> <!ELEMENT Product (Manufacturer)> <!ELEMENT Manufacturer (Address)> <!ELEMENT Address (#PCDATA)> CREATE TABLE Customer ( CustomerKey integer PRIMARY KEY, …) CREATE TABLE Manufacturer ( ManufacturerKey integer PRIMARY KEY, …) CREATE TABLE Address ( CustomerKey integer NULL, ManufacturerKey integer NULL, …)

  22. Example for Rule 4 <!ELEMENT Customer (Name, Address, City?, State?, PostalCode)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Address (#PCDATA)> <!ELEMENT City (#PCDATA)> <!ELEMENT State (#PCDATA)> <!ELEMENT PostalCode (#PCDATA)> CREATE TABLE Customer ( CustomerKey integer PRIMARY KEY, Name varchar(50), Address varchar(100), City varchar(50) NULL, State char(2) NULL, PostalCode varchar(10))

  23. Design RDBMS for Existing XML (cont.) • Representing multiple text-only elements. • 若只在單一母元素出現一次以上,所以建立一個資料表,含該元素的文字資料與一外來鍵欄位指向母元素。 • 假如出現在多種母元素裡又不只一次以上,採用中間資料表來表示與母元素間的關係。 • Handling EMPTY elements. • 為每個此種元素建立一資料表,如其僅有一母元素則建立外來鍵欄位。 • Representing mixed content elements. • 建立TableLookup資料表為資料庫中每個資料表插入一行記錄,並插入第0行其資料表名為TextContent • 建立TextContent表格,欄位為:TextContentKey(整數型態鍵值)、 ElementName(存放純文字元素之元素名)、(TextContent)存放文字內容。 • 建立兩個資料表:一為此元素使用直接以元素命名(如:p),另一為連結其內各種次元素之資料表(命名如:pSubelement) • 次元素資料表中需有一外來鍵指向主元素表,另一外來鍵指向TableLookup表標明資料是放在那個資料表中、一個資料表索引鍵以找出內容是擺在哪一行,及一序號欄以標出次元素或文字元素在主元素中的順序。

  24. Example for Rule 5 <!ELEMENT Customer (Name+, Address, City?, State?, PostalCode)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Address (#PCDATA)> <!ELEMENT City (#PCDATA)> CREATE TABLE Customer ( CustomerKey integer, Address varchar(100), City varchar(50) NULL, State char(2) NULL, PostalCode varchar(10), PRIMARY KEY (CustomerKey)) CREATE TABLE CustomerName ( CustomerKey integer, Name varchar(50) CONSTRAINT FK_CustomerName_Customer FOREIGN KEY (CustomerKey) REFERENCES Customer (CustomerKey))

  25. Example for Rule 7 <!ELEMENT p (#PCDATA | a | br | span | bdo | object | img | map | tt | i | b | big | small | em | strong | dfn | code | q | sub | sup | samp | kbd | var | cite | abbr | acronym | input | select | textarea | label | button | ins | del | script | noscript)*> CREATE TABLE p ( pKey integer, PRIMARY KEY (pKey)) CREATE TABLE TableLookup ( TableLookupKey integer, TableName varchar(255), PRIMARY KEY (TableLookupKey)) CREATE TABLE TextContent ( TextContentKey integer, ElementName varchar(255) NULL, TextContent varchar(255)) CREATE TABLE pSubelements ( pKey integer, CONSTRAINT FK_pSubelements_p FOREIGN KEY (pKey) REFERENCES p (pKey), TableLookupKey integer, CONSTRAINT FK_pSubelements_TableLookup FOREIGN KEY (TableLookupKey) REFERENCES TableLookup (TableLookupKey), TableKey integer, Sequence integer)

  26. Design RDBMS for Existing XML (cont.) • Handling the “ANY” content elements. • 作法與處理mixed content element方式一樣。 • CDATA attributes • 在含此屬性之元素的資料表中加入一個此屬性的欄位。 • 設定資料型態為varchar並依經驗設定最大長度。 • REQUIRED/IMPLIED/FIXED attributes • 屬性是#REQIURED – 表每筆record中必有此資料。 • 屬性是#IMPLIED – 表此欄位允許NULL值。 • 屬性是#FIXED – 原則上不必進資料庫,有必要才放進去。 • ENUMERATED attributes • 為此屬性在該元素資料表中建立一個欄位,但只存放代碼。 • 建立一個對照表(Lookup Table)存放所有可能的數值列。 • 每次插入資料時都到對照表轉譯成代碼後再存入該元素的資料表中。 • Handling ID attributes • 若該ID屬性資料帶有意義者,將其當作CDATA存進資料庫中。 • 若該ID屬性資料表示為主鍵值,可以拿它來insert或update資料庫資料。

  27. Design RDBMS for Existing XML (cont.) • Handling IDREF attributes • 若只指向一種特定元素的話,則當作外來鍵指向以該ID屬性為主鍵值的元素。 • 若指向不只一種元素的話,則透過建立TableLookup表格方式,指向相應的鍵值。 • Handling IDREFS attributes • 若只指向一種特定元素的話,建立一個join table,內容為兩個互相有關係的外來鍵值。 • 若指向不只一種元素的話,則透過建立TableLookup表格方式,指向相應的鍵值。 • 在關係表中與lookup table間要建立外來鍵設定。 • NMTOKEN attributes • 對每個NMTOKEN型態屬性建立一欄位。 • NMTOKENS attributes • 對每個NMTOKENS型態屬性建立一資料表,含:一自動增值之主鍵、一指向元素資料表的外來鍵與TOKEN字串欄位。 • ENTITY and ENTITES attributes • 處理方式同NMTOKEN與NMTOKENS • Check for name collisions • 檢查所有表格,如有名稱衝突,就改名。

  28. Example for Rule 13 <!ELEMENT Order (Customer, Invoice)> <!ELEMENT Customer EMPTY> <!ATTLIST Customer CustomerID ID #REQUIRED> <!ELEMENT Invoice EMPTY> <!ATTLIST Invoice InvoiceID ID #REQUIRED CustomerIDREF IDREF #REQUIRED> CREATE TABLE Customer ( CustomerKey integer, PRIMARY KEY (CustomerKey)) CREATE TABLE Invoice ( InvoiceKey integer, CustomerKey integer CONSTRAINT FK_Invoice_Customer FOREIGN KEY (CustomerKey) REFERENCES Customer (CustomerKey))

  29. 不利於RDBMS的基本論點(1) • 關連式資料庫管理系統是設計用來對關連式資料幹活的 • 假如資料無法被簡單地正規化則採用關連式解決方案是不妥的。 • 關連式資料庫無法處理複雜的XML階層/巢狀結構 • 形成太多資料表 • 形成太多外部參考 • 形成太多外鍵 • 資料表聯合太複雜 • 查詢很麻煩 • 無物件鎖定功能

  30. 不利於RDBMS的基本論點(2) • 關連式資料庫提供許多存放選擇,這是因為沒有一種是好的方案 • BLOBs 型態資料無法輕易地查詢 • BLOBs 型態資料無法輕易地更新 • 把XML資料存放在多重資料表中會造成效能的負荷,所以必須儘量少採用此方法。

  31. MS SQL Server & XML 從SQL Server存取XML (SQL Server 2000 with XML extensions)

  32. SQL Server 2000: SQLXML • Microsoft’s Mapping technology (and more…) • Shipped with SQL Server 2000 • Two subsequent releases have shipped to the Web • Provides a rich XML view of relational data • Semi-structured, hierarchical view of flat relational data • Two-way view: query and update • Multiple access mechanisms (HTTP, ADO, ADO.NET, SOAP) • Middle-tier and Server side support

  33. Query/Update Technologies • FOR XML (raw, auto, nested, explicit) • SQL language extension to retrieve XML instead of rowsets • XML Views • Work with your relational database as if it was XML file (through annotated schema) • Bulkload • Shred large XML files into existing tables • Updategrams • Update through XML View • XPath query support

  34. XML BCP/SQL XMLBulkload SQL update/ insert/ delete Updategrams XML SQL Server SQL Queries Rowsets FOR XML SQL Queries FOR XML Queries FOR XML Queries FOR XML Architecture Annotated XSD Mapping Schemas Query Query Processor XML XPath

  35. Mapping Technologies • Optimized for data-centric documents • “Shredding” of XML documents into and out of non-XML logical model, e.g. Relational • Provides a denomalized logical view on top of highly structured, normalized data • User-defined mapping • Not all data may be mapped to the destination • Physical structure may be lost • Schema-less content might be discarded • In some cases, a default (system-defined) mapping may suffice

  36. XML Views • Map between relational data and XML • Declarative • Noninvasive • No changes to legacy data sources • No control over DB Server required • XML View is an XML Schema • XSD and XDR supported

  37. Default Mapping Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“ xmlns:msdata="urn:schemas-microsoft-com:mapping-schema“> <xsd:element name=“Customers”> <xsd:complexType> <xsd:attribute name=“CustID”/> </xsd:complexType> </xsd:element > </xsd:schema> Relational Data: Customers Table XML View CustID ------ A NULL B <Customers CustID=“A”/> <Customers/> <Customers CustID=“B”/>

  38. Explicit Mapping Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“ xmlns:msdata="urn:schemas-microsoft-com:mapping-schema“> <xsd:element name=“Customers”msdata:relation=“tblCustomers"> <xsd:complexType> <xsd:attribute name=“ID”msdata:field=“pkCustID"/> </xsd:complexType> </xsd:element > </xsd:schema> Relational Data: tblCustomers Table pkCustID ------ A NULL B <Customers ID=“A”/> <Customers/> <Customers ID=“B”/>

  39. XML Results from SQL Server Query: "SELECT name,address FROM EmpTable FOR XML AUTO" XML result: <?xml version="1.0" encoding="UTF-8"?> <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <EmpTable name="Tom" address="123 Main St"/> <EmpTable name="Maria" address="456 Broadway"/> </ROOT> • AUTO mode uses table name for row element • RAW mode uses 'row' for the row element

  40. Controls,Designers,Code-gen, etc XSL/T, X-Path, etc XmlData-Document DataSet Mapping .NET Data Provider DataAdapter DataReader XmlReader Command Connection ADO.NET and XML

  41. ADO.NET and XML • The DataSet • Loads/saves XML data into/out of DataSet • Schema can be loaded/saved as XSD • Schema can be inferred from XML Data • The DataSet can be associated with an XmlDataDocument • Exposes a relational view over structured XML • According to the DataSet schema • Allows strong typing, control binding, relational access of XML data • Allows XML tools (schema validation, XSL/T, XPath queries) against relational data • Preserves full fidelity of XML Document

  42. // Load DataSet with XML DataSet ds = new DataSet(); ds.ReadXml("inventory.xml"); // Add a record to the Inventory table DataTable inventory = ds.Tables["Inventory"]; DataRow row = inventory.NewRow(); row["TitleID"]=1; row["Quantity"]=25; inventory.Rows.Add(row); // Write out XML ds.WriteXml("updatedinventory.xml"); Loading XML

  43. Oracle’s XDK XML Development Kit

  44. Support for XML in Oracle Products XML Developer Kit • XML Parser • XSLT Processor • XSQL Pages • XML SQL Utility Oracle 9i SYS.xmltype Datatype Oracle Text JDeveloper PLSXML

  45. XML Developer Kit (XDK) Features based on XML standards: • XML parser • DOM and SAX support • XSLT processor Oracle-specific features: • XSQL pages • XML SQL utility A single Oracle module that XML-enables your Oracle database

  46. XDK Availability and Compatibility • Installs automatically with Oracle 9i and Oracle 8i Release 3 (8.1.7) databases • Available for Oracle 8i Release 1 and 2 from the Oracle Technology Network at technet.oracle.com • Not available for Oracle7 or Oracle8 Oracle’s XDK is evolving rapidly. Check OTN periodically to see if a newer version of the XDK is available for download.

  47. XDK Fun Facts • Java applications can run inside or outside the database. • You must install Oracle’s JVM in the database in order to run PL/SQL applications that use Oracle’s XDK. • Some XDK features (such as SAX support and XSQL pages) are only available in the XDK for Java.

  48. Oracle Java/XML Tools XML Parsers XML Class Generators XML SQL Utility for Java XSQL Servlet XML Schema Processor

  49. XML Class Generator DTD parser Class Generator Java classes (one per element) Valid document User written Java Application

  50. XML SQL Utility • Invoke an XSLT to transform data from any format into a <ROWSET><ROW> style XML document. • Convert the <ROWSET><ROW> document into a SQL INSERT statement and load the data into a table. • Capabilities exist for updating and deleting data as well. • Extract data from the database into a document of any format by reversing the process. A facility for loading XML documents into the database and retrieving data from the database into XML documents, without storing the XML text in one large CLOB column

More Related