Distinction between subelement and attribute
● In the context of documents, attributes are part of markup, while
subelement contents are part of the basic document contents
● In the context of data representation, the difference is unclear and
may be confusing
Same information can be represented in two ways
– .
–
A101
● Suggestion: use attributes for identifiers of elements, and use
subelements for contents
59 trang |
Chia sẻ: huyhoang44 | Lượt xem: 771 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Cơ sở dữ liệu - Chapter 10: XML, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Database System Concepts
©Silberschatz, Korth and Sudarshan
See www.dbbook.com for conditions on reuse
Chapter 10: XML
©Silberschatz, Korth and Sudarshan10.2Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.3Database System Concepts 5th Edition, Aug 22, 2005.
Introduction
n XML: Extensible Markup Language
n Defined by the WWW Consortium (W3C)
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
©Silberschatz, Korth and Sudarshan10.4Database System Concepts 5th Edition, Aug 22, 2005.
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) selfdocumenting
l E.g.
A101
Downtown
500
A101
Johnson
©Silberschatz, Korth and Sudarshan10.5Database System Concepts 5th Edition, Aug 22, 2005.
XML: Motivation
n Data interchange is critical in today’s networked world
l Examples:
Banking: funds transfer
Order processing (especially intercompany orders)
Scientific data
– Chemistry: ChemML,
– Genetics: BSML (BioSequence Markup Language),
l 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
©Silberschatz, Korth and Sudarshan10.6Database System Concepts 5th Edition, Aug 22, 2005.
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
DTD (Document Type Descriptors)
XML Schema
l 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
©Silberschatz, Korth and Sudarshan10.7Database System Concepts 5th Edition, Aug 22, 2005.
Comparison with Relational Data
n Inefficient: tags, which in effect represent schema information, are
repeated
n Better than relational tuples as a dataexchange format
l Unlike relational tuples, XML data is selfdocumenting due to
presence of tags
l Nonrigid format: tags can be added
l Allows nested structures
l Wide acceptance, not only in database systems, but also in
browsers, tools, and applications
©Silberschatz, Korth and Sudarshan10.8Database System Concepts 5th Edition, Aug 22, 2005.
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
.
l Improper nesting
.
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 toplevel element
©Silberschatz, Korth and Sudarshan10.9Database System Concepts 5th Edition, Aug 22, 2005.
Example of Nested Elements
Hayes
Main
Harrison
A102
Perryridge
400
.
.
©Silberschatz, Korth and Sudarshan10.10Database System Concepts 5th Edition, Aug 22, 2005.
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 objectrelational databases
n But nesting is appropriate when transferring data
l External application does not have direct access to data referenced
by a foreign key
©Silberschatz, Korth and Sudarshan10.11Database System Concepts 5th Edition, Aug 22, 2005.
Structure of XML Data (Cont.)
n Mixture of text with subelements is legal in XML.
l Example:
This account is seldom used any more.
A102
Perryridge
400
l Useful for document markup, but discouraged for data
representation
©Silberschatz, Korth and Sudarshan10.12Database System Concepts 5th Edition, Aug 22, 2005.
Attributes
n Elements can have attributes
A102
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
©Silberschatz, Korth and Sudarshan10.13Database System Concepts 5th Edition, Aug 22, 2005.
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
Same information can be represented in two ways
– .
–
A101
l Suggestion: use attributes for identifiers of elements, and use
subelements for contents
©Silberschatz, Korth and Sudarshan10.14Database System Concepts 5th Edition, Aug 22, 2005.
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 uniquename:elementname
n Avoid using long unique names all over document by using XML
Namespaces
Downtown
Brooklyn
©Silberschatz, Korth and Sudarshan10.15Database System Concepts 5th Edition, Aug 22, 2005.
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”
©Silberschatz, Korth and Sudarshan10.16Database System Concepts 5th Edition, Aug 22, 2005.
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)
Widely used
l XML Schema
Newer, increasing use
©Silberschatz, Korth and Sudarshan10.17Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.18Database System Concepts 5th Edition, Aug 22, 2005.
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
Notation:
– “|” alternatives
– “+” 1 or more occurrences
– “*” 0 or more occurrences
©Silberschatz, Korth and Sudarshan10.19Database System Concepts 5th Edition, Aug 22, 2005.
Bank DTD
<!DOCTYPE bank [
<! ELEMENT customer(customer_name customer_street
customer_city)>
]>
©Silberschatz, Korth and Sudarshan10.20Database System Concepts 5th Edition, Aug 22, 2005.
Attribute Specification in DTD
n Attribute specification : for each attribute
l Name
l Type of attribute
CDATA
ID (identifier) or IDREF (ID reference) or IDREFS (multiple IDREFs)
– more on this later
l Whether
mandatory (#REQUIRED)
has a default value (value),
or neither (#IMPLIED)
n Examples
l
l <!ATTLIST customer
customer_id ID # REQUIRED
accounts IDREFS # REQUIRED >
©Silberschatz, Korth and Sudarshan10.21Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.22Database System Concepts 5th Edition, Aug 22, 2005.
Bank DTD with Attributes
n Bank DTD with ID and IDREF attribute types.
<!DOCTYPE bank2[
<!ATTLIST account
account_number ID # REQUIRED
owners IDREFS # REQUIRED>
<!ELEMENT customer(customer_name, customer_street,
customer_city)>
<!ATTLIST customer
customer_id ID # REQUIRED
accounts IDREFS # REQUIRED>
declarations for branch, balance, customer_name,
customer_street and customer_city
]>
©Silberschatz, Korth and Sudarshan10.23Database System Concepts 5th Edition, Aug 22, 2005.
XML data with ID and IDREF attributes
Downtown
500
..
Joe
Monroe
Madison
Mary
Erin
Newark
©Silberschatz, Korth and Sudarshan10.24Database System Concepts 5th Edition, Aug 22, 2005.
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 document
layout environment from which XML evolved)
l (A | B)* allows specification of an unordered set, but
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
owners attribute should ideally be constrained to refer to
customer elements
©Silberschatz, Korth and Sudarshan10.25Database System Concepts 5th Edition, Aug 22, 2005.
XML Schema
n XML Schema is a more sophisticated schema language which
addresses the drawbacks of DTDs. Supports
l Typing of values
E.g. integer, string, etc
Also, constraints on min/max values
l Userdefined, comlex types
l Many more features, including
uniqueness and foreign key constraints, inheritance
n XML Schema is itself specified in XML syntax, unlike DTDs
l Morestandard representation, but verbose
n XML Scheme is integrated with namespaces
n BUT: XML Schema is significantly more complicated than DTDs.
©Silberschatz, Korth and Sudarshan10.26Database System Concepts 5th Edition, Aug 22, 2005.
XML Schema Version of Bank DTD
.. definitions of customer and depositor .
©Silberschatz, Korth and Sudarshan10.27Database System Concepts 5th Edition, Aug 22, 2005.
XML Schema Version of Bank DTD
n Choice of “xs:” was ours any other namespace prefix could be
chosen
n Element “bank” has type “BankType”, which is defined separately
l xs:complexType is used later to create the named complex type
“BankType”
n Element “account” has its type defined inline
©Silberschatz, Korth and Sudarshan10.28Database System Concepts 5th Edition, Aug 22, 2005.
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:
©Silberschatz, Korth and Sudarshan10.29Database System Concepts 5th Edition, Aug 22, 2005.
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
Simple language consisting of path expressions
l XQuery
An XML query language with a rich set of features
l XSLT
Simple language designed for translation from XML to XML
and XML to HTML
©Silberschatz, Korth and Sudarshan10.30Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.31Database System Concepts 5th Edition, Aug 22, 2005.
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. /bank2/customer/customer_name evaluated on the
bank2 data we saw earlier returns
Joe
Mary
n E.g. /bank2/customer/customer_name/text( )
returns the same names, but without the enclosing tags
©Silberschatz, Korth and Sudarshan10.32Database System Concepts 5th Edition, Aug 22, 2005.
XPath (Cont.)
n The initial “/” denotes root of the document (above the toplevel 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. /bank2/account[balance > 400]
returns account elements with a balance value greater than 400
/bank2/account[balance] returns account elements containing a
balance subelement
n Attributes are accessed using “@”
l E.g. /bank2/account[balance > 400]/@account_number
returns the account numbers of accounts with balance > 400
l IDREF attributes are not dereferenced automatically (more on this
later)
©Silberschatz, Korth and Sudarshan10.33Database System Concepts 5th Edition, Aug 22, 2005.
Functions in XPath
n XPath provides several functions
l The function count() at the end of a path counts the number of
elements in the set generated by the path
E.g. /bank2/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 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. /bank2/account/id(@owner)
returns all customers referred to from the owners attribute of
account elements.
©Silberschatz, Korth and Sudarshan10.34Database System Concepts 5th Edition, Aug 22, 2005.
More XPath Features
n Operator “|” used to implement union
l E.g. /bank2/account/id(@owner) | /bank2/loan/id(@borrower)
Gives customers with either accounts or loans
However, “|” cannot be nested inside other operators.
n “//” can be used to skip multiple levels of nodes
l E.g. /bank2//customer_name
finds any customer_name element anywhere under the
/bank2 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
©Silberschatz, Korth and Sudarshan10.35Database System Concepts 5th Edition, Aug 22, 2005.
XQuery
n XQuery is a general purpose query language for XML data
n Currently being standardized by the World Wide Web Consortium
(W3C)
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 XMLQL
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
©Silberschatz, Korth and Sudarshan10.36Database System Concepts 5th Edition, Aug 22, 2005.
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 /bank2/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 /bank2/account[balance>400]
return { $x/@account_number }
©Silberschatz, Korth and Sudarshan10.37Database System Concepts 5th Edition, Aug 22, 2005.
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 }
©Silberschatz, Korth and Sudarshan10.38Database System Concepts 5th Edition, Aug 22, 2005.
Nested Queries
n The following query converts data from the flat structure for bank
information into the nested structure used in bank1
{
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 toplevel tag
n $c/text() gives text content of an element without any subelements / tags
©Silberschatz, Korth and Sudarshan10.39Database System Concepts 5th Edition, Aug 22, 2005.
Sorting in XQuery
n 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 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/* }
}
©Silberschatz, Korth and Sudarshan10.40Database System Concepts 5th Edition, Aug 22, 2005.
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 Ifthenelse clauses
©Silberschatz, Korth and Sudarshan10.41Database System Concepts 5th Edition, Aug 22, 2005.
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 generalpurpose 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
©Silberschatz, Korth and Sudarshan10.42Database System Concepts 5th Edition, Aug 22, 2005.
XSLT Templates
n Example of XSLT template with match and select part
n The match attribute of xsl:template specifies a pattern in XPath
n Elements in the XML document matching the pattern are processed by the
actions within the xsl:template element
l xsl:valueof selects (outputs) specified values (here, customer_name)
n For elements that do not match any template
l Attributes and text contents are output as is
l Templates are recursively applied on subelements
n The template matches all
elements that do not match any other template
l Used to ensure that their contents do not get output.
n If an element matches several templates, only one is used based on a
complex priority scheme/userdefined priorities
©Silberschatz, Korth and Sudarshan10.43Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.44Database System Concepts 5th Edition, Aug 22, 2005.
Creating XML Output (Cont.)
n Note: Cannot directly insert a xsl:valueof tag inside another tag
l E.g. cannot create an attribute for in the previous example
by directly using xsl:valueof
l XSLT provides a construct xsl:attribute to handle this situation
xsl:attribute adds attribute to the preceding element
E.g.
results in output of the form
.
n xsl:element is used to create output elements with computed names
©Silberschatz, Korth and Sudarshan10.45Database System Concepts 5th Edition, Aug 22, 2005.
Structural Recursion
n Template action can apply templates recursively to the contents of a
matched element
n Example output:
John
Mary
©Silberschatz, Korth and Sudarshan10.46Database System Concepts 5th Edition, Aug 22, 2005.
Joins in XSLT
n 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.
<xsl:key name=“acctno” match=“account”
use=“account_number”/>
<xsl:valueof select=key(“acctno”, “A101”)
n Keys permit (some) joins to be expressed in XSLT
©Silberschatz, Korth and Sudarshan10.47Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.48Database System Concepts 5th Edition, Aug 22, 2005.
Application Program Interface
n There are two standard application program interfaces to XML data:
l SAX (Simple API for XML)
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)
XML data is parsed into a tree representation
Variety of functions provided for traversing the DOM tree
E.g.: Java DOM API provides Node class with methods
getParentNode( ), getFirstChild( ), getNextSibling( )
getAttribute( ), getData( ) (for text node)
getElementsByTagName( ),
Also provides functions for updating DOM tree
©Silberschatz, Korth and Sudarshan10.49Database System Concepts 5th Edition, Aug 22, 2005.
Storage of XML Data
n XML data can be stored in
l Nonrelational data stores
Flat files
– Natural for storing XML
– But has all problems discussed in Chapter 1 (no concurrency,
no recovery, )
XML database
– Database built specifically for storing XML data, supporting
DOM model and declarative querying
– Currently no commercialgrade systems
l Relational databases
Data must be translated into relational form
Advantage: mature database systems
Disadvantages: overhead of translating data and queries
©Silberschatz, Korth and Sudarshan10.50Database System Concepts 5th Edition, Aug 22, 2005.
Storage of XML in Relational Databases
n Alternatives:
l String Representation
l Tree Representation
l Map to relations
©Silberschatz, Korth and Sudarshan10.51Database System Concepts 5th Edition, Aug 22, 2005.
String Representation
n Store each child of 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 toplevel element type
E.g. account, customer, depositor relations
– Each with a stringvalued 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
E.g. customer_name or account_number
l Some database systems support function indices, which use the
result of a function as the key value.
The function should return the value of the required
subelement/attribute
©Silberschatz, Korth and Sudarshan10.52Database System Concepts 5th Edition, Aug 22, 2005.
String Representation (Cont.)
n Benefits:
l Can store any XML data even without DTD
l As long as the toplevel element in a document has a large
number of children, strings are small compared to full document
Allows fast access to individual elements.
n Drawback: Need to parse strings to access values inside the elements
l Parsing is slow.
©Silberschatz, Korth and Sudarshan10.53Database System Concepts 5th Edition, Aug 22, 2005.
Tree Representation
n Tree representation: model XML data as tree and store using relations
nodes(id, type, label, value)
child (child_id, parent_id)
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 parentchild relationships in the tree
l Can add an extra attribute to child to record ordering of children
bank (id:1)
customer (id:2) account (id: 5)
customer_name
(id: 3)
account_number
(id: 7)
©Silberschatz, Korth and Sudarshan10.54Database System Concepts 5th Edition, Aug 22, 2005.
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
©Silberschatz, Korth and Sudarshan10.55Database System Concepts 5th Edition, Aug 22, 2005.
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
As in the tree representation
Position information (ith child) can be stored too
n All subelements that occur only once can become relation attributes
l For textvalued 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
©Silberschatz, Korth and Sudarshan10.56Database System Concepts 5th Edition, Aug 22, 2005.
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 XMLenabled 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
©Silberschatz, Korth and Sudarshan10.57Database System Concepts 5th Edition, Aug 22, 2005.
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
A101
Downtown
500
. more rows if there are more output tuples
©Silberschatz, Korth and Sudarshan10.58Database System Concepts 5th Edition, Aug 22, 2005.
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
n xmlforest(attr1, attr2, ..) creates a sequence (“forest”) of one or more
elements, with tag names same as the SQL attribute name
n xmlagg: aggregate function creates a forest from elements in group
select xmlelement (name “branch”, branch_name,
xmlagg(xmlforest(account_number)
order by account_number)
from account
group by branch_name
©Silberschatz, Korth and Sudarshan10.59Database System Concepts 5th Edition, Aug 22, 2005.
XML Application: 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
Các file đính kèm theo tài liệu này:
- ch10_5297_7369.pdf