Скачать презентацию Chapter 10 XML Database System Concepts Silberschatz Korth Скачать презентацию Chapter 10 XML Database System Concepts Silberschatz Korth

9482eabdce1abc952d7ddc6377df3b19.ppt

  • Количество слайдов: 59

Chapter 10: XML Database System Concepts ©Silberschatz, Korth and Sudarshan See www. db-book. com Chapter 10: XML Database System Concepts ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use

XML n Structure of XML Data n XML Document Schema n Querying and Transformation XML n Structure of XML Data n XML Document Schema n Querying and Transformation n Application Program Interfaces to XML n Storage of XML Data n XML Applications Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 2 ©Silberschatz, Korth and Sudarshan

Introduction n XML: Extensible Markup Language n Defined by the WWW Consortium (W 3 Introduction n XML: Extensible Markup Language n Defined by the WWW Consortium (W 3 C) n Derived from SGML (Standard Generalized Markup Language), but simpler to use than SGML n Documents have tags giving extra information about sections of the document l E. g. XML Introduction … n Extensible, unlike HTML l Users can add new tags, and separately specify how the tag should be handled for display Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 3 ©Silberschatz, Korth and Sudarshan

XML Introduction (Cont. ) n The ability to specify new tags, and to create XML Introduction (Cont. ) n The ability to specify new tags, and to create nested tag structures make XML a great way to exchange data, not just documents. l Much of the use of XML has been in data exchange applications, not as a replacement for HTML n Tags make data (relatively) self-documenting l E. g. A-101 Downtown 500 A-101 Johnson Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 4 ©Silberschatz, Korth and Sudarshan

XML: Motivation n Data interchange is critical in today’s networked world l Examples: 4 XML: Motivation n Data interchange is critical in today’s networked world l Examples: 4 Banking: 4 Order funds transfer processing (especially inter-company orders) 4 Scientific data – Chemistry: Chem. ML, … – Genetics: l BSML (Bio-Sequence Markup Language), … Paper flow of information between organizations is being replaced by electronic flow of information n Each application area has its own set of standards for representing information n XML has become the basis for all new generation data interchange formats Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 5 ©Silberschatz, Korth and Sudarshan

XML Motivation (Cont. ) n Earlier generation formats were based on plain text with XML Motivation (Cont. ) n Earlier generation formats were based on plain text with line headers indicating the meaning of fields l Similar in concept to email headers l Does not allow for nested structures, no standard “type” language l Tied too closely to low level document structure (lines, spaces, etc) n Each XML based standard defines what are valid elements, using l XML type specification languages to specify the syntax 4 DTD 4 XML l (Document Type Descriptors) Schema Plus textual descriptions of the semantics n XML allows new tags to be defined as required l However, this may be constrained by DTDs n A wide variety of tools is available for parsing, browsing and querying XML documents/data Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 6 ©Silberschatz, Korth and Sudarshan

Comparison with Relational Data n Inefficient: tags, which in effect represent schema information, are Comparison with Relational Data n Inefficient: tags, which in effect represent schema information, are repeated n Better than relational tuples as a data-exchange format l Unlike relational tuples, XML data is self-documenting due to presence of tags l Non-rigid format: tags can be added l Allows nested structures l Wide acceptance, not only in database systems, but also in browsers, tools, and applications Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 7 ©Silberschatz, Korth and Sudarshan

Structure of XML Data n Tag: label for a section of data n Element: Structure of XML Data n Tag: label for a section of data n Element: section of data beginning with and ending with matching n Elements must be properly nested l Proper nesting 4 l Improper nesting 4 l …. …. Formally: every start tag must have a unique matching end tag, that is in the context of the same parent element. n Every document must have a single top-level element Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 8 ©Silberschatz, Korth and Sudarshan

Example of Nested Elements <bank-1> <customer_name> Hayes </customer_name> <customer_street> Main </customer_street> <customer_city> Harrison </customer_city> Example of Nested Elements Hayes Main Harrison A-102 Perryridge 400 . . Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 9 ©Silberschatz, Korth and Sudarshan

Motivation for Nesting n Nesting of data is useful in data transfer l Example: Motivation for Nesting n Nesting of data is useful in data transfer l Example: elements representing customer_id, customer_name, and address nested within an order element n Nesting is not supported, or discouraged, in relational databases l With multiple orders, customer name and address are stored redundantly l normalization replaces nested structures in each order by foreign key into table storing customer name and address information l Nesting is supported in object-relational databases n But nesting is appropriate when transferring data l External application does not have direct access to data referenced by a foreign key Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 10 ©Silberschatz, Korth and Sudarshan

Structure of XML Data (Cont. ) n Mixture of text with sub-elements is legal Structure of XML Data (Cont. ) n Mixture of text with sub-elements is legal in XML. Example: This account is seldom used any more. A-102 Perryridge 400 l Useful for document markup, but discouraged for data representation l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 11 ©Silberschatz, Korth and Sudarshan

Attributes n Elements can have attributes <account acct-type = “checking” > <account_number> A-102 </account_number> Attributes n Elements can have attributes A-102 Perryridge 400 n Attributes are specified by name=value pairs inside the starting tag of an element n An element may have several attributes, but each attribute name can only occur once Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 12 ©Silberschatz, Korth and Sudarshan

Attributes vs. Subelements n Distinction between subelement and attribute l In the context of Attributes vs. Subelements n Distinction between subelement and attribute l In the context of documents, attributes are part of markup, while subelement contents are part of the basic document contents l In the context of data representation, the difference is unclear and may be confusing 4 Same information can be represented in two ways – …. – A-101 l Suggestion: use attributes for identifiers of elements, and use subelements for contents Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 13 ©Silberschatz, Korth and Sudarshan

Namespaces n XML data has to be exchanged between organizations n Same tag name Namespaces n XML data has to be exchanged between organizations n Same tag name may have different meaning in different organizations, causing confusion on exchanged documents n Specifying a unique string as an element name avoids confusion n Better solution: use unique-name: element-name n Avoid using long unique names all over document by using XML Namespaces Downtown Brooklyn Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 14 ©Silberschatz, Korth and Sudarshan

More on XML Syntax n Elements without subelements or text content can be abbreviated More on XML Syntax n Elements without subelements or text content can be abbreviated by ending the start tag with a /> and deleting the end tag l n To store string data that may contain tags, without the tags being interpreted as subelements, use CDATA as below l ]]> Here, and are treated as just strings CDATA stands for “character data” Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 15 ©Silberschatz, Korth and Sudarshan

XML Document Schema n Database schemas constrain what information can be stored, and the XML Document Schema n Database schemas constrain what information can be stored, and the data types of stored values n XML documents are not required to have an associated schema n However, schemas are very important for XML data exchange l Otherwise, a site cannot automatically interpret data received from another site n Two mechanisms for specifying XML schema l Document Type Definition (DTD) 4 Widely l used XML Schema 4 Newer, increasing use Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 16 ©Silberschatz, Korth and Sudarshan

Document Type Definition (DTD) n The type of an XML document can be specified Document Type Definition (DTD) n The type of an XML document can be specified using a DTD n DTD constraints structure of XML data l What elements can occur l What attributes can/must an element have l What subelements can/must occur inside each element, and how many times. n DTD does not constrain data types l All values represented as strings in XML n DTD syntax l l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 17 ©Silberschatz, Korth and Sudarshan

Element Specification in DTD n Subelements can be specified as l names of elements, Element Specification in DTD n Subelements can be specified as l names of elements, or l #PCDATA (parsed character data), i. e. , character strings l EMPTY (no subelements) or ANY (anything can be a subelement) n Example n Subelement specification may have regular expressions 4 Notation: – “|” - alternatives – “+” - 1 or more occurrences – “*” - 0 or more occurrences Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 18 ©Silberschatz, Korth and Sudarshan

Bank DTD <!DOCTYPE bank [ <!ELEMENT bank ( ( account | customer | depositor)+)> Bank DTD ]> Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 19 ©Silberschatz, Korth and Sudarshan

Attribute Specification in DTD n Attribute specification : for each attribute l Name l Attribute Specification in DTD n Attribute specification : for each attribute l Name l Type of attribute 4 CDATA 4 ID (identifier) or IDREF (ID reference) or IDREFS (multiple IDREFs) – more on this later l Whether 4 mandatory 4 has 4 or (#REQUIRED) a default value (value), neither (#IMPLIED) n Examples l l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 20 ©Silberschatz, Korth and Sudarshan

IDs and IDREFs n An element can have at most one attribute of type IDs and IDREFs n An element can have at most one attribute of type ID n The ID attribute value of each element in an XML document must be distinct l Thus the ID attribute value is an object identifier n An attribute of type IDREF must contain the ID value of an element in the same document n An attribute of type IDREFS contains a set of (0 or more) ID values. Each ID value must contain the ID value of an element in the same document Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 21 ©Silberschatz, Korth and Sudarshan

Bank DTD with Attributes n Bank DTD with ID and IDREF attribute types. <!DOCTYPE Bank DTD with Attributes n Bank DTD with ID and IDREF attribute types. … declarations for branch, balance, customer_name, customer_street and customer_city ]> Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 22 ©Silberschatz, Korth and Sudarshan

XML data with ID and IDREF attributes <bank-2> <account_number=“A-401” owners=“C 100 C 102”> <branch_name> XML data with ID and IDREF attributes Downtown 500 Joe Monroe Madison Mary Erin Newark Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 23 ©Silberschatz, Korth and Sudarshan

Limitations of DTDs n No typing of text elements and attributes l All values Limitations of DTDs n No typing of text elements and attributes l All values are strings, no integers, reals, etc. n Difficult to specify unordered sets of subelements l Order is usually irrelevant in databases (unlike in the documentlayout environment from which XML evolved) l (A | B)* allows specification of an unordered set, but 4 Cannot ensure that each of A and B occurs only once n IDs and IDREFs are untyped l The owners attribute of an account may contain a reference to another account, which is meaningless 4 owners attribute should ideally be constrained to refer to customer elements Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 24 ©Silberschatz, Korth and Sudarshan

XML Schema n XML Schema is a more sophisticated schema language which addresses the XML Schema n XML Schema is a more sophisticated schema language which addresses the drawbacks of DTDs. Supports l Typing of values 4 E. g. integer, string, etc 4 Also, constraints on min/max values l User-defined, complex types l Many more features, including 4 uniqueness and foreign key constraints, inheritance n XML Schema is itself specified in XML syntax, unlike DTDs l More-standard representation, but verbose n XML Scheme is integrated with namespaces n BUT: XML Schema is significantly more complicated than DTDs. Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 25 ©Silberschatz, Korth and Sudarshan

XML Schema Version of Bank DTD <xs: schema xmlns: xs=http: //www. w 3. org/2001/XMLSchema> XML Schema Version of Bank DTD …. . definitions of customer and depositor …. Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 26 ©Silberschatz, Korth and Sudarshan

XML Schema Version of Bank DTD n Choice of “xs: ” was ours -- XML Schema Version of Bank DTD n Choice of “xs: ” was ours -- any other namespace prefix could be chosen n Element “bank” has type “Bank. Type”, which is defined separately l xs: complex. Type is used later to create the named complex type “Bank. Type” n Element “account” has its type defined in-line Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 27 ©Silberschatz, Korth and Sudarshan

More features of XML Schema n Attributes specified by xs: attribute tag: l <xs: More features of XML Schema n Attributes specified by xs: attribute tag: l l adding the attribute use = “required” means value must be specified n Key constraint: “account numbers form a key for account elements under the root bank element: n Foreign key constraint from depositor to account: Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 28 ©Silberschatz, Korth and Sudarshan

Querying and Transforming XML Data n Translation of information from one XML schema to Querying and Transforming XML Data n Translation of information from one XML schema to another n Querying on XML data n Above two are closely related, and handled by the same tools n Standard XML querying/translation languages l XPath 4 Simple l language consisting of path expressions XSLT 4 Simple language designed for translation from XML to XML and XML to HTML l XQuery 4 An XML query language with a rich set of features Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 29 ©Silberschatz, Korth and Sudarshan

Tree Model of XML Data n Query and transformation languages are based on a Tree Model of XML Data n Query and transformation languages are based on a tree model of XML data n An XML document is modeled as a tree, with nodes corresponding to elements and attributes l Element nodes have child nodes, which can be attributes or subelements l Text in an element is modeled as a text node child of the element l Children of a node are ordered according to their order in the XML document l Element and attribute nodes (except for the root node) have a single parent, which is an element node l The root node has a single child, which is the root element of the document Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 30 ©Silberschatz, Korth and Sudarshan

XPath n XPath is used to address (select) parts of documents using path expressions XPath n XPath is used to address (select) parts of documents using path expressions n A path expression is a sequence of steps separated by “/” l Think of file names in a directory hierarchy n Result of path expression: set of values that along with their containing elements/attributes match the specified path n E. g. /bank-2/customer_name evaluated on the bank-2 data we saw earlier returns Joe Mary n E. g. /bank-2/customer_name/text( ) returns the same names, but without the enclosing tags Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 31 ©Silberschatz, Korth and Sudarshan

XPath (Cont. ) n The initial “/” denotes root of the document (above the XPath (Cont. ) n The initial “/” denotes root of the document (above the top-level tag) n Path expressions are evaluated left to right l Each step operates on the set of instances produced by the previous step n Selection predicates may follow any step in a path, in [ ] l E. g. /bank-2/account[balance > 400] 4 returns account elements with a balance value greater than 400 4 /bank-2/account[balance] returns account elements containing a balance subelement n Attributes are accessed using “@” l E. g. /bank-2/account[balance > 400]/@account_number 4 returns l the account numbers of accounts with balance > 400 IDREF attributes are not dereferenced automatically (more on this later) Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 32 ©Silberschatz, Korth and Sudarshan

Functions in XPath provides several functions The function count() at the end of a Functions in XPath provides several functions The function count() at the end of a path counts the number of elements in the set generated by the path 4 E. g. /bank-2/account[count(. /customer) > 2] – Returns accounts with > 2 customers l Also function for testing position (1, 2, . . ) of node w. r. t. siblings n Boolean connectives and or and function not() can be used in predicates n IDREFs can be referenced using function id() l id() can also be applied to sets of references such as IDREFS and even to strings containing multiple references separated by blanks l E. g. /bank-2/account/id(@owner) 4 returns all customers referred to from the owners attribute of account elements. l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 33 ©Silberschatz, Korth and Sudarshan

More XPath Features n Operator “|” used to implement union E. g. /bank-2/account/id(@owner) | More XPath Features n Operator “|” used to implement union E. g. /bank-2/account/id(@owner) | /bank-2/loan/id(@borrower) 4 Gives customers with either accounts or loans 4 However, “|” cannot be nested inside other operators. n “//” can be used to skip multiple levels of nodes l E. g. /bank-2//customer_name 4 finds any customer_name element anywhere under the /bank-2 element, regardless of the element in which it is contained. n A step in the path can go to parents, siblings, ancestors and descendants of the nodes generated by the previous step, not just to the children l “//”, described above, is a short from for specifying “all descendants” l “. . ” specifies the parent. n doc(name) returns the root of a named document l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 34 ©Silberschatz, Korth and Sudarshan

XQuery n XQuery is a general purpose query language for XML data n Currently XQuery n XQuery is a general purpose query language for XML data n Currently being standardized by the World Wide Web Consortium (W 3 C) l The textbook description is based on a January 2005 draft of the standard. The final version may differ, but major features likely to stay unchanged. n XQuery is derived from the Quilt query language, which itself borrows from SQL, XQL and XML-QL n XQuery uses a for … let … where … order by …result … syntax for SQL from where SQL where order by SQL order by result SQL select let allows temporary variables, and has no equivalent in SQL Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 35 ©Silberschatz, Korth and Sudarshan

FLWOR Syntax in XQuery n For clause uses XPath expressions, and variable in for FLWOR Syntax in XQuery n For clause uses XPath expressions, and variable in for clause ranges over values in the set returned by XPath n Simple FLWOR expression in XQuery l find all accounts with balance > 400, with each result enclosed in an . . tag for $x in /bank-2/account let $acctno : = $x/@account_number where $x/balance > 400 return { $acctno } l Items in the return clause are XML text unless enclosed in {}, in which case they are evaluated n Let clause not really needed in this query, and selection can be done In XPath. Query can be written as: for $x in /bank-2/account[balance>400] return { $x/@account_number } Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 36 ©Silberschatz, Korth and Sudarshan

Joins n Joins are specified in a manner very similar to SQL for $a Joins n Joins are specified in a manner very similar to SQL for $a in /bank/account, $c in /bank/customer, $d in /bank/depositor where $a/account_number = $d/account_number and $c/customer_name = $d/customer_name return { $c $a } n The same query can be expressed with the selections specified as XPath selections: for $a in /bank/account $c in /bank/customer $d in /bank/depositor[ account_number = $a/account_number and customer_name = $c/customer_name] return { $c $a } Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 37 ©Silberschatz, Korth and Sudarshan

Nested Queries n The following query converts data from the flat structure for bank Nested Queries n The following query converts data from the flat structure for bank (fig 10. 1) information into the nested structure used in bank-1(fig. 10. 4) { for $c in /bank/customer return { $c/* } { for $d in /bank/depositor[customer_name = $c/customer_name], $a in /bank/account[account_number=$d/account_number] return $a } } n $c/* denotes all the children of the node to which $c is bound, without the enclosing top-level tag n $c/text() gives text content of an element without any subelements / tags Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 38 ©Silberschatz, Korth and Sudarshan

Sorting in XQuery The order by clause can be used at the end of Sorting in XQuery The order by clause can be used at the end of any expression. E. g. to return customers sorted by name for $c in /bank/customer order by $c/customer_name return { $c/* } n Use order by $c/customer_name to sort in descending order n n Can sort at multiple levels of nesting (sort by customer_name, and by account_number within each customer) { for $c in /bank/customer order by $c/customer_name return { $c/* } { for $d in /bank/depositor[customer_name=$c/customer_name], $a in /bank/account[account_number=$d/account_number] } order by $a/account_number return $a/* } Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 39 ©Silberschatz, Korth and Sudarshan

Functions and Other XQuery Features n User defined functions with the type system of Functions and Other XQuery Features n User defined functions with the type system of XMLSchema function balances(xs: string $c) returns list(xs: decimal*) { for $d in /bank/depositor[customer_name = $c], $a in /bank/account[account_number = $d/account_number] return $a/balance } n Types are optional for function parameters and return values n The * (as in decimal*) indicates a sequence of values of that type n Universal and existential quantification in where clause predicates l some $e in path satisfies P l every $e in path satisfies P n XQuery also supports If-then-else clauses Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 40 ©Silberschatz, Korth and Sudarshan

XSLT n A stylesheet stores formatting options for a document, usually separately from document XSLT n A stylesheet stores formatting options for a document, usually separately from document l E. g. an HTML style sheet may specify font colors and sizes for headings, etc. n The XML Stylesheet Language (XSL) was originally designed for generating HTML from XML n XSLT is a general-purpose transformation language l Can translate XML to XML, and XML to HTML n XSLT transformations are expressed using rules called templates l Templates combine selection using XPath with construction of results Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 41 ©Silberschatz, Korth and Sudarshan

XSLT Templates n Example of XSLT template with match and select part <xsl: template XSLT Templates n Example of XSLT template with match and select part n n n The match attribute of xsl: template specifies a pattern in XPath Elements in the XML document matching the pattern are processed by the actions within the xsl: template element l xsl: value-of selects (outputs) specified values (here, customer_name) For elements that do not match any template l Attributes and text contents are output as is l Templates are recursively applied on subelements The template matches all elements that do not match any other template l Used to ensure that their contents do not get output. If an element matches several templates, only one is used based on a complex priority scheme/user-defined priorities Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 42 ©Silberschatz, Korth and Sudarshan

Creating XML Output n Any text or tag in the XSL stylesheet that is Creating XML Output n Any text or tag in the XSL stylesheet that is not in the xsl namespace is output as is n E. g. to wrap results in new XML elements. l Example output: Joe Mary Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 43 ©Silberschatz, Korth and Sudarshan

Creating XML Output (Cont. ) n Note: Cannot directly insert a xsl: value-of tag Creating XML Output (Cont. ) n Note: Cannot directly insert a xsl: value-of tag inside another tag E. g. cannot create an attribute for in the previous example by directly using xsl: value-of l XSLT provides a construct xsl: attribute to handle this situation 4 xsl: attribute adds attribute to the preceding element 4 E. g. results in output of the form …. l Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 44 ©Silberschatz, Korth and Sudarshan

Structural Recursion n Template action can apply templates recursively to the contents of a Structural Recursion n Template action can apply templates recursively to the contents of a matched element n Example output: John Mary Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 45 ©Silberschatz, Korth and Sudarshan

Joins in XSLT keys allow elements to be looked up (indexed) by values of Joins in XSLT keys allow elements to be looked up (indexed) by values of subelements or attributes l Keys must be declared (with a name) and, the key() function can then be used for lookup. E. g. Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 46 ©Silberschatz, Korth and Sudarshan

Sorting in XSLT n Using an xsl: sort directive inside a template causes all Sorting in XSLT n Using an xsl: sort directive inside a template causes all elements matching the template to be sorted l Sorting is done before applying other templates Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 47 ©Silberschatz, Korth and Sudarshan

Application Program Interface n There are two standard application program interfaces to XML data: Application Program Interface n There are two standard application program interfaces to XML data: l SAX (Simple API for XML) 4 Based on parser model, user provides event handlers for parsing events – E. g. start of element, end of element – Not suitable for database applications l DOM (Document Object Model) 4 XML data is parsed into a tree representation 4 Variety 4 E. g. : 4 Also of functions provided for traversing the DOM tree Java DOM API provides Node class with methods get. Parent. Node( ), get. First. Child( ), get. Next. Sibling( ) get. Attribute( ), get. Data( ) (for text node) get. Elements. By. Tag. Name( ), … provides functions for updating DOM tree Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 48 ©Silberschatz, Korth and Sudarshan

Storage of XML Data n XML data can be stored in l Non-relational data Storage of XML Data n XML data can be stored in l Non-relational data stores 4 Flat files – Natural for storing XML – But has all problems discussed in Chapter 1 (no concurrency, no recovery, …) 4 XML database – Database built specifically for storing XML data, supporting DOM model and declarative querying – Currently no commercial-grade systems l Relational databases 4 Data must be translated into relational form 4 Advantage: mature database systems 4 Disadvantages: Database System Concepts - 5 th Edition, Aug 22, 2005. overhead of translating data and queries 10. 49 ©Silberschatz, Korth and Sudarshan

Storage of XML in Relational Databases n Alternatives: l String Representation l Tree Representation Storage of XML in Relational Databases n Alternatives: l String Representation l Tree Representation l Map to relations Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 50 ©Silberschatz, Korth and Sudarshan

String Representation n Store each top level element as a string field of a String Representation n Store each top level element as a string field of a tuple in a relational database l Use a single relation to store all elements, or l Use a separate relation for each top-level element type 4 E. g. account, customer, depositor relations – Each with a string-valued attribute to store the element n Indexing: l Store values of subelements/attributes to be indexed as extra fields of the relation, and build indices on these fields 4 E. g. l customer_name or account_number Some database systems support function indices, which use the result of a function as the key value. 4 The function should return the value of the required subelement/attribute Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 51 ©Silberschatz, Korth and Sudarshan

String Representation (Cont. ) n Benefits: l Can store any XML data even without String Representation (Cont. ) n Benefits: l Can store any XML data even without DTD l As long as there are many top-level elements in a document, strings are small compared to full document 4 Allows fast access to individual elements. n Drawback: Need to parse strings to access values inside the elements l Parsing is slow. Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 52 ©Silberschatz, Korth and Sudarshan

Tree Representation n Tree representation: model XML data as tree and store using relations Tree Representation n Tree representation: model XML data as tree and store using relations nodes(id, type, label, value) child (child_id, parent_id) bank (id: 1) customer (id: 2) account (id: 5) customer_name (id: 3) account_number (id: 7) n Each element/attribute is given a unique identifier n Type indicates element/attribute n Label specifies the tag name of the element/name of attribute n Value is the text value of the element/attribute n The relation child notes the parent-child relationships in the tree l Can add an extra attribute to child to record ordering of children Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 53 ©Silberschatz, Korth and Sudarshan

Tree Representation (Cont. ) n Benefit: Can store any XML data, even without DTD Tree Representation (Cont. ) n Benefit: Can store any XML data, even without DTD n Drawbacks: l Data is broken up into too many pieces, increasing space overheads l Even simple queries require a large number of joins, which can be slow Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 54 ©Silberschatz, Korth and Sudarshan

Mapping XML Data to Relations n Relation created for each element type whose schema Mapping XML Data to Relations n Relation created for each element type whose schema is known: l An id attribute to store a unique id for each element l A relation attribute corresponding to each element attribute l A parent_id attribute to keep track of parent element 4 As in the tree representation 4 Position information (ith child) can be store too n All subelements that occur only once can become relation attributes l For text-valued subelements, store the text as attribute value l For complex subelements, can store the id of the subelement n Subelements that can occur multiple times represented in a separate table l Similar to handling of multivalued attributes when converting ER diagrams to tables Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 55 ©Silberschatz, Korth and Sudarshan

Storing XML Data in Relational Systems n Publishing: process of converting relational data to Storing XML Data in Relational Systems n Publishing: process of converting relational data to an XML format n Shredding: process of converting an XML document into a set of tuples to be inserted into one or more relations n XML-enabled database systems support automated publishing and shredding n Some systems offer native storage of XML data using the xml data type. Special internal data structures and indices are used for efficiency Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 56 ©Silberschatz, Korth and Sudarshan

SQL/XML n New standard SQL extension that allows creation of nested XML output l SQL/XML n New standard SQL extension that allows creation of nested XML output l Each output tuple is mapped to an XML element row A-101 Downtown 500 …. more rows if there are more output tuples … Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 57 ©Silberschatz, Korth and Sudarshan

SQL Extensions n xmlelement creates XML elements n xmlattributes creates attributes select xmlelement (name SQL Extensions n xmlelement creates XML elements n xmlattributes creates attributes select xmlelement (name “account, xmlattributes (account_number as account_number), xmlelement (name “branch_name”, branch_name), xmlelement (name “balance”, balance)) from account Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 58 ©Silberschatz, Korth and Sudarshan

Web Services n The Simple Object Access Protocol (SOAP) standard: l Invocation of procedures Web Services n The Simple Object Access Protocol (SOAP) standard: l Invocation of procedures across applications with distinct databases l XML used to represent procedure input and output n A Web service is a site providing a collection of SOAP procedures l Described using the Web Services Description Language (WSDL) l Directories of Web services are described using the Universal Description, Discovery, and Integration (UDDI) standard Database System Concepts - 5 th Edition, Aug 22, 2005. 10. 59 ©Silberschatz, Korth and Sudarshan