Querying XML Documents With Xquery
Querying XML Documents With Xquery
An Introduction to XML and Web Technologies How XML generalizes relational databases
The XQuery language
Querying XML Documents How XML may be supported in databases
with XQuery
An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4
1
Only Some Trees are Relations Trees Are Not Relations
They have height two Not all trees satisfy the previous characterization
The root has an unbounded number of children Trees are ordered, while both rows and columns
All nodes in the second layer (records) have a of tables may be permuted without changing the
fixed number of child nodes (fields) meaning of the data
An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6
<students>
<student id="100026">
<name>Joe Average</name>
<age>21</age>
<major>Biology</major>
<results>
<result course="Math 101" grade="C-"/>
<result course="Biology 101" grade="C+"/>
<result course="Statistics 101" grade="D"/>
</results>
</student>
An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8
2
A More Natural Model (2/2) Usage Scenario: Data-
Data-Oriented
<student id="100078">
We want to carry over the kinds of queries that we
<name>Jack Doe</name>
<age>18</age> performed in the original relational model
<major>Physics</major>
<major>XML Science</major>
<results>
<result course="Math 101" grade="A"/>
<result course="XML 101" grade="A-"/>
<result course="Physics 101" grade="B+"/>
<result course="XML 102" grade="A"/>
</results>
</student>
</students>
An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10
An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12
3
Usage Scenario: Hybrid XQuery Design Requirements
Queries could be used to data mine hybrid data, Must have at least one XML syntax and at least
such as patient records one human-readable syntax
Must be declarative
Must be namespace aware
Must coordinate with XML Schema
Must support simple and complex datatypes
Must combine information from multiple
documents
Must be able to transform and create XML trees
An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14
An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16
4
XQuery Prolog More From the Prolog
Like XPath expressions, XQuery expressions are declare default element namespace URI;
declare default function namespace URI;
evaluated relatively to a context
import schema at URI;
This is explicitly provided by a prolog declare namespace NCName = URI;
Settings define various parameters for the
XQuery processor language, such as:
An Introduction to XML and Web Technologies 17 An Introduction to XML and Web Technologies 18
An Introduction to XML and Web Technologies 19 An Introduction to XML and Web Technologies 20
5
Datatype Expressions XML Expressions
Same atomic values as XPath 2.0 XQuery expressions may compute new XML
Also lots of primitive simple values: nodes
xs:string("XML is fun")
Expressions may denote element, character data,
xs:boolean("true")
xs:decimal("3.1415") comment, and processing instruction nodes
xs:float("6.02214199E23")
xs:dateTime("1999-05-31T13:20:00-05:00")
Each node is created with a unique node identity
xs:time("13:20:00-05:00") Constructors may be either direct or computed
xs:date("1999-05-31")
xs:gYearMonth("1999-05")
xs:gYear("1999")
xs:hexBinary("48656c6c6f0a")
xs:base64Binary("SGVsbG8K")
xs:anyURI("http://www.brics.dk/ixwt/")
xs:QName("rcp:recipe")
An Introduction to XML and Web Technologies 21 An Introduction to XML and Web Technologies 22
Uses the standard XML syntax declare default element namespace "http://businesscard.org";
<card>
The expression <name>John Doe</name>
<title>CEO, Widget Inc.</title>
<foo><bar/>baz</foo> <email>[email protected]</email>
<phone>(202) 555-1414</phone>
evaluates to the given XML fragment <logo uri="widget.gif"/>
<foo/> is <foo/>
evaluates to false
An Introduction to XML and Web Technologies 23 An Introduction to XML and Web Technologies 24
6
Namespaces in Constructors (2/3) Namespaces in Constructors (3/3)
An Introduction to XML and Web Technologies 25 An Introduction to XML and Web Technologies 26
element card {
<foo bar="1 2 3 4 5"/> namespace { "http://businesscard.org" },
element name { text { "John Doe" } },
<foo bar="{1, 2, 3, 4, 5}"/> element title { text { "CEO, Widget Inc." } } ,
<foo bar="1 {2 to 4} 5"/> element email { text { "[email protected]" } },
element phone { text { "(202) 555-1414" } },
element logo {
attribute uri { "widget.gif" }
}
}
An Introduction to XML and Web Technologies 27 An Introduction to XML and Web Technologies 28
7
Computed QNames Biliingual Business Cards
An Introduction to XML and Web Technologies 29 An Introduction to XML and Web Technologies 30
An Introduction to XML and Web Technologies 31 An Introduction to XML and Web Technologies 32
8
The Difference Between For and Let (2/4) The Difference Between For and Let (3/4)
1, 2, 3, 4, a, 1, 2, 3, 4, b, 1, 2, 3, 4, c 1, a, 1, b, 1, c, 2, a, 2, b, 2, c,
3, a, 3, b, 3, c, 4, a, 4, b, 4, c
An Introduction to XML and Web Technologies 33 An Introduction to XML and Web Technologies 34
<fridge>
<stuff>eggs</stuff>
<stuff>olive oil</stuff>
<stuff>ketchup</stuff>
<stuff>unrecognizable moldy thing</stuff>
</fridge>
An Introduction to XML and Web Technologies 35 An Introduction to XML and Web Technologies 36
9
Inverting a Relation Sorting the Results
<gpas>
{ for $s in fn:doc("students.xml")//student
return <gpa id="{$s/@id}" gpa="{local:gpa($s)}"/> }
</gpas>
An Introduction to XML and Web Technologies 39 An Introduction to XML and Web Technologies 40
10
A Height Function A Textual Outline
An Introduction to XML and Web Technologies 41 An Introduction to XML and Web Technologies 42
An Introduction to XML and Web Technologies 43 An Introduction to XML and Web Technologies 44
11
An Untyped Function A Default Typed Function
An Introduction to XML and Web Technologies 45 An Introduction to XML and Web Technologies 46
An Introduction to XML and Web Technologies 47 An Introduction to XML and Web Technologies 48
12
Runtime Type Checks Built-
Built-In Functions Have Signatures
An Introduction to XML and Web Technologies 49 An Introduction to XML and Web Technologies 50
for $t in fn:doc("recipes.xml")/rcp:collection/rcp:recipe/rcp:title
return $t
How can XML and databases be merged?
<xqx:module
xmlns:xqx="http://www.w3.org/2003/12/XQueryX"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <xqx:stepExpr> Several different approaches:
<xqx:xpathAxis>child</xqx:xpathAxis>
xsi:schemaLocation="http://www.w3.org/2003/12/XQueryX xqueryx.xsd">
<xqx:mainModule>
<xqx:queryBody>
xqx:nodeName>
<xqx:elementTest>
<xqx:nodeName> • extract XML views of relations
<xqx:QName>rcp:title</xqx:QName> <xqx:QName>rcp:collection</xqx:QName>
<xqx:expr xsi:type="xqx:flwrExpr">
</xqx:nodeName>
<xqx:forClause>
</xqx:elementTest>
</xqx:nodeName>
</xqx:elementTest> • use SQL to generate XML
</xqx:stepExpr>
<xqx:forClauseItem> </xqx:stepExpr>
</xqx:expr>
<xqx:typedVariableBinding>
</xqx:forExpr>
<xqx:varName>t</xqx:varName>
<xqx:stepExpr>
<xqx:xpathAxis>child</xqx:xpathAxis>
• shred XML into relational databases
</xqx:forClauseItem> <xqx:elementTest>
</xqx:typedVariableBinding>
</xqx:forClause> <xqx:nodeName>
<xqx:forExpr>
<xqx:returnClause> <xqx:QName>rcp:recipe</xqx:QName>
<xqx:expr xsi:type="xqx:variable">
<xqx:expr xsi:type="xqx:pathExpr"> </xqx:nodeName>
<xqx:name>t</xqx:name> </xqx:elementTest>
<xqx:expr xsi:type="xqx:functionCallExpr">
</xqx:expr> </xqx:stepExpr>
<xqx:functionName>doc</xqx:functionName>
</xqx:returnClause> <xqx:stepExpr>
</xqx:expr> <xqx:parameters> <xqx:xpathAxis>child</xqx:xpathAxis>
<xqx:expr xsi:type="xqx:stringConstantExpr">
</xqx:elementContent>
</xqx:expr> <xqx:value>recipes.xml</xqx:value>
</xqx:queryBody> </xqx:expr>
</xqx:mainModule>
</xqx:parameters>
</xqx:module>
An Introduction to XML and Web Technologies 51 An Introduction to XML and Web Technologies 52
13
The Student Database Again Automatic XML Views (1/2)
<Students>
<record id="100026" name="Joe Average" age="21"/>
<record id="100078" name="Jack Doe" age="18"/>
</Students>
An Introduction to XML and Web Technologies 53 An Introduction to XML and Web Technologies 54
An Introduction to XML and Web Technologies 55 An Introduction to XML and Web Technologies 56
14
XML Shredding From XQuery to SQL
Each element type is represented by a relation Any XML document can be faithfully represented
Each element node is assigned a unique key in This takes advantage of the existing database
document order implementation
Each element node contains the key of its parent Queries must now be phrased in ordinary SQL
The possible attributes are represented as fields, rather than XQuery
where absent attributes have the null value But an automatic translation is possible
//rcp:ingredient[@name="butter"]/@amount
Contents consisting of a single character data
node is inlined as a field select ingredient.amount
from ingredient
where ingredient.name="butter"
An Introduction to XML and Web Technologies 57 An Introduction to XML and Web Technologies 58
An Introduction to XML and Web Technologies 59 An Introduction to XML and Web Technologies 60
15