0% found this document useful (0 votes)
10 views11 pages

It3031 L04 XMLDB

The document provides an overview of XML databases, including their structure, storage, and querying methods. It covers topics such as XML schemas, well-formed XML, and the use of XPath and XQuery for data retrieval. Additionally, it discusses the differences between native XML databases and XML-enabled databases, along with examples of XML data handling in SQL Server.

Uploaded by

jdoe52682
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

It3031 L04 XMLDB

The document provides an overview of XML databases, including their structure, storage, and querying methods. It covers topics such as XML schemas, well-formed XML, and the use of XPath and XQuery for data retrieval. Additionally, it discusses the differences between native XML databases and XML-enabled databases, along with examples of XML data handling in SQL Server.

Uploaded by

jdoe52682
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Database Systems and Outline

Data-Driven
 Introduction to XML
Applications  XML Document, XML Schema
 XML data storage
XML Databases  Native XML Databases
 XML data type
 XML enable Databases
 XML data retrieve
 XPath
 XQuery
 FLWOR
1 2

XML
Example: XML Document
 XML = eXtensible Markup Language.

 While HTML uses tags for formatting (e.g., <catalog>
“italic”), XML uses tags for semantics (e.g., “this <product dept="WMN">
is an address”). <number>557</number>
<name language="en">Fleece Pullover</name>
 Key idea: create tag sets for a domain, and
translate all data into properly tagged XML <colorChoices>navy black</colorChoices>
documents. </product>
<product dept="ACC">
3 … 4

Well-Formed XML Tags

 Start the document with a declaration,  Tags, as in HTML, are normally matched
surrounded by <?xml … ?> . pairs, as <FOO> … </FOO> .

Normal declaration is:  Tags may be nested arbitrarily.


<?xml version = “1.0”?>

 Balance of document is a root tag  XML tags are case sensitive.


surrounding nested tags.

5 6

1
Example: Well-Formed XML Example

<?xml version = “1.0”?> A NAME  The <BARS> XML document is:


<BARS> subobject
<BAR><NAME>Joe’s Bar</NAME> BARS
<BEER><NAME>Bud</NAME>
BAR BAR
<PRICE>2.50</PRICE></BEER>
BAR
<BEER><NAME>Miller</NAME> NAME ...
<PRICE>3.00</PRICE></BEER> A BEER BEER
BEER
</BAR> subobject Joe’s Bar
<BAR> … PRICE NAME PRICE
NAME
</BARS>
Bud 2.50 Miller 3.00
7 8

XML schema language Example:


XML Schema and XML Document
 Describe the structure and data content of an XML
document
 The description of an element consists of its name (tag),
and a parenthesized description of any nested tags.
 Includes order of sub tags and their multiplicity.
 etc…
 Can be used to validate XML documents

 Type of schema languages


 DTD (Document type Definition)
 XML Schema
 RELAX NG
9 Xml doc 10
XML Schema

XML Data Storage The main approaches…

 XML is de-facto standard for exchanging data  Native XML databases (Eg: Timber, Tamino, Xindice)
 stores and retrieves XML data in its native form
 contain new techniques for storage and retrieval

 Storing and accessing large XML data stores  XML-enabled databases (Eg: Oracle, MS SQL
Server,DB2)
is gaining in importance  Use existing database technology to store XML data
 Database technology has matured over the last three decades
 Advantage: Use more powerful existing database
technologies
 Both approaches have merit!
 Schema is static (structured)  XML enabled DB approach is
advantageous
 Schema is not static (unstructured/semi-structured)  Native
11 XML DB approach is advantageous 12

2
Native storage as XML data
XML enabled approach type
 Native storage as XML data type
XML storage options
 XMLType data type - Oracle
 Mapping between XML and existing data R. Murthy and S. Banerjee, “XML Schemas in Oracle XML DB”, VLDB,
2003.
models supported by DBMSs
 Xml Type - SQL Server 2005
S. Pal, I. Cseri, O. Seeliger, G. Schaller, L. Giakoumakis and V. Zolotov,
“Indexing XML Data Stored in a Relational Database”, VLDB, 2004.
 Large object storage (CLOB, BLOB)
 Internal representation of stored data is preserve the
XML content of the data, such as containment
hierarchy, document order, element and attribute
values, and so on

 Difference between Native XML databases and XML


13 type are blurred 14

Untyped and typed XML data Untyped and typed XML data
type type
 XML values can be stored natively in an XML data type
column, which can be typed according to a collection of For MS SQL Server,
XML schemas, or can be left untyped
Untyped XML Column in Table:
 Use untyped XML data type under the following CREATE TABLE AdminDocs (
conditions: id int primary key,
 You do not have a schema for your XML data xDoc XML not null
 You have schemas but you do not want the server to ) Schema collection
validate the data
Typed XML Column in Table:
 Use typed XML data type under the following conditions: CREATE TABLE AdminDocs (
 You have schemas for your XML data and you want the id int primary key,
server to validate your XML data according on the XML
xDoc XML (CONTENT myCollection)
schemas
15 ) 16
 You want to take advantage of storage and query
optimizations based on type information

Untyped and typed XML data


type How to Query XML Data?
Inserting Data: Example
 Main two ways to extract data
INSERT INTO AdminDocs VALUES (1,
'<book >  Path expressions
<title>Writing Secure Code</title>
<author>  great if you just want to copy certain elements and
<first-name>Michael</first-name> attributes as is
<last-name>Howard</last-name>
</author>  XQuery expressions
<author>
<first-name>David</first-name>  XQuery extends XPath to a query language
<last-name>LeBlanc</last-name> that has power similar to SQL.
</author>
<price>39.99</price>
</book>')

17 18

3
Path Descriptors
Path Expressions
 XPath is a language for describing paths in  Simple path descriptors are sequences of
XML documents. tags separated by slashes (/).
 i.e. Xpath 1.0 and Xpath 2.0  If the descriptor begins with /, then the path
starts at the root and has those tags, in order.
 Really think of the semistructured data graph  If the descriptor begins with //, then the path
and its paths. can start anywhere.

19 20

Value of a Path Descriptor Example: /BARS/BAR/PRICE

 Each path descriptor, applied to a <BARS>


document, has a value that is a sequence of <BAR name = “JoesBar”>
elements. <PRICE theBeer = “Bud”>2.50</PRICE>
 An element is an atomic value or a node. <PRICE theBeer = “Miller”>3.00</PRICE>
 A node is matching tags and everything in </BAR> …
between. <BEER name = “Bud” soldBy = “JoesBar
 i.e., a node of the semistructured graph. SuesBar …”/> … /BARS/BAR/PRICE describes the
set with these two PRICE elements
</BARS> as well as the PRICE elements for
21 any other bars. 22

Example: //PRICE Wild-Card *

 A star (*) in place of a tag represents any one


<BARS>
tag.
<BAR name = “JoesBar”>
 Example: /*/*/PRICE represents all price
<PRICE theBeer = “Bud”>2.50</PRICE>
objects at the third level of nesting.
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR> …
<BEER name = “Bud” soldBy = “JoesBar
SuesBar …”/>… //PRICE describes the PRICE
Elements to appear within
</BARS> the document.
23 24

4
Example: /BARS/* Attributes

 In XPath, we refer to attributes by prepending


<BARS>
@ to their name.
<BAR name = “JoesBar”>
 Attributes of a tag may appear in paths as if
<PRICE theBeer = “Bud”>2.50</PRICE>
they were nested within that tag.
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR> …
<BEER name = “Bud” soldBy = “JoesBar
SuesBar …”/> …
</BARS> /BARS/* captures all BAR
and BEER elements, such
as these. 25 26

Example: /BARS/*/@name Axes

<BARS>  In general, path expressions allow us to


<BAR name = “JoesBar”> start at the root and execute steps to find a
<PRICE theBeer = “Bud”>2.50</PRICE> sequence of nodes at each step.
<PRICE theBeer = “Miller”>3.00</PRICE>  At each step, we may follow any one of
</BAR> … several axes.
<BEER name = “Bud” soldBy = “JoesBar
SuesBar …”/> …
/BARS/*/@name selects all
</BARS> name attributes of immediate
subelements of the BARS element.
27 28

Axes Example: Axes

 /BARS/BEER is really shorter way for


/child::BAR[@name=“JoesBar”] /BARS/child::BEER .
 @ is really shorthand for the attribute:: axis.
Axis Node test Predicate
 Thus, /BARS/BEER[@name = “Bud” ] is
1. The axis (Optional) shorthand for
- Direction to navigate /BARS/BEER[attribute::name = “Bud”]
2. The node test
- The node of interest by name
3. Predicate
- The criteria used to filter nodes
29 30

5
More Axes Predicates
 Some other useful axes are:
 A condition inside […] may follow a tag.
 parent:: = parent(s) of the current node(s).
 descendant-or-self:: = the current node(s)
 If so, then only paths that have that tag and
and all descendants. also satisfy the condition are included in the
 Note: // is really shorthand for this axis. result of a path expression.
 ancestor::, ancestor-or-self, etc.
 the default axis is child:: --- go to all the
children of the current set of nodes.

31 32

Example: Selection Condition


Example: Attribute in Selection

 /BARS/BAR[PRICE < 2.75]/PRICE  /BARS/BAR/PRICE[@theBeer = “Miller”]


<BARS> <BARS>
<BAR name = “JoesBar”> <BAR name = “JoesBar”>
<PRICE theBeer = “Bud”>2.50</PRICE> <PRICE theBeer = “Bud”>2.50</PRICE>
<PRICE theBeer = “Miller”>3.00</PRICE> <PRICE theBeer = “Miller”>3.00</PRICE>
</BAR> … </BAR> …
The condition that the PRICE be Now, this PRICE element
< $2.75 makes this price but not is selected, along with
the Miller price satisfy the path any other prices for Miller.
descriptor.
33 34

Example: Predicates and Returned


Elements

• Using number in a predicate does not mean that number


elements are returned:

35 36

6
Using Position in Predicates Using Position in Predicates

• Can use a number in the predicate to indicate the • More than one predicate can be used to
position of the child
specify multiple constraints in a step
- evaluated left to right

37 38

XQuery

 XQuery extends XPath to a query language that has


power similar to SQL.
 Basic building blocks,

39 40

XQuery Comments

• Variables are identified by a name preceded by a $

41 42

7
Clauses of a FLWOR Expression for Clauses

43 44

Range expressions Multiple Variables in a for clause

45 46

Positional variables in for clause let clauses

47 48

8
Multiple for and let clauses where clause

49 50

return clause order by Clause

51 52

Variable binding and referencing Comparisons

53 54

9
Value vs. General
Comparisons
Comparisons
General Comparisons

55 56

Conditional expressions Effective boolean value

57 58

Nesting conditional Functions


expressions

59 60

10
Built in Functions: A Sample More built in functions

61 62

MS SQL Server: MS SQL Server:


Methods on XML Data Type Methods on XML Data Type
 query() - fragments of an XML document can be  value() - Scalar values can be extracted from
extracted using the query() method of XML data an XML instance using the value() method by
type. The query() method accepts an XQuery
specifying an XQuery expression and the
expression as an argument and returns an untyped
XML instance.
desired SQL type to be returned.
Eg.
 Eg. SELECT xDoc.value(
SELECT xDoc.query('/doc[@id = 123]//section') 'data((/doc//section[@num = 3]/title)[1])', 'nvarchar(max)')
FROM AdminDocs FROM AdminDocs

63 64

MS SQL Server : MS SQL Server:


Methods on XML Data Type Methods on XML Data Type
 modify() - Data manipulation operations can be performed on an
XML instance using the modify() method. Support for XML DML is
 exist() - method is useful for existential checks on provided through insert, delete, and update keywords added to
an XML instance. It returns 1 if the XQuery XQuery. One or more nodes can be inserted, deleted, and updated
expression evaluates to non-null node list; otherwise using the insert, delete, and update keywords, respectively.
it returns 0.  Eg.
UPDATE AdminDocs SET xDoc.modify('
insert
SELECT xDoc.query('/doc[@id = 123]//section') <section num="2">
FROM AdminDocs <title>Background</title>
WHERE xDoc.exist ('/doc[@id = 123]') = 1 </section>
after (/doc//section[@num=1])[1]')

65 66

11

You might also like