nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 1 XML Query Languages Database Systems (4th edition) Chapter 30.5, 30.3.3, 30.3.4 Articles & Excerpts XML Query Languages * 2 XQuery Computer Environment Tutorials DB2 & XML Querying XML Data with XQuery
42
Embed
Nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 1 XML Query Languages Database Systems (4th edition) Chapter 30.5, 30.3.3, 30.3.4 Articles & Excerpts.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
1
XML Query Languages
Database Systems (4th edition)
Chapter 30.5, 30.3.3, 30.3.4
Articles & Excerpts
XML Query Languages * 2
XQuery
Computer Environment Tutorials
DB2 & XML
Querying XML Data with XQuery
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
2
XML Query Languages
Languages for querying data stored inside XML structures
•XPath•XSLT•XQuery•XML/SQL (SQL 2003c)•Proprietary languages (IBM, Oracle, etc.)•…
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
3
XPath
• Path expressions for navigating through XML structures
• Possible to set conditions and use wildcards
• Also includes many functions
• Example:– /Book[@Price=500]/Author/@Name
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
4
XSLT• Enables transformations between different XML
structures (mostly used for XML to HTML transformations)
• Oracle:– SQL UDFs: Extract-functions, existsNode, etc.
• Others
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
8
Example<Test Place="Kista" Deadline="2002.09.21"> <Description version="2.1"> This test is about the effects of computer games on the human brain </Description> <Phase Status="finished" Index="2" Date="2001.03.11"> <Details>Choose the computer games to be used for the test</Details> <Results>Age of Empires</Results> <Results>Flight Simulator</Results> <Results>Tetris</Results> </Phase> <Phase Status="started" Index="3"> <Details>Let users try the computer games</Details> <Results> Many users find Flight Simulator hard due to 3D environment and multiple controls </Results> </Phase> <Phase Status="cancelled" Index="1" Date="2001.01.09"> <Details>Try to get funding from EU</Details> </Phase> <Phase Status="not-started" Index="4"> <Details>Present the results of the test</Details> </Phase></Test>
• attribute()– /Test/attribute() (similar to /Test/@*)
• node() (follows the standard??)– /node() – element nodes and text nodes– /@node() – attribute nodes
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
21
XQuery – Computed Constructors
• element– element name value:
let $a := “a”, $b := 2
return <x>{element {$a} {$b}}</x>
• attribute– attribute name value:
let $a := “a”, $b := 2
return <x>{attribute {$a} {$b}}</x>
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
22
XQuery – Conditionals
• if-then-elsefor $a in (1 to 5)
return
if ($a mod 2 = 0)
then <even>{$a}</even>
else <odd>{$a}</odd>
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
23
XQuery – Quantifiers
• somefor $a in /Testwhere some $b in $a/Phase/@Status satisfies string($b) = "finished"return $a/Description
• everyfor $a in /Testwhere every $b in $a/Phase/@Status satisfies
string($b) = "finished"return $a/Description
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
24
XQuery – Nesting
• One result becomes the source of another expression:for $x in distinct-values
(for $a in (1 to 5), $b in (1 to 5)
return <sum>{$a + $b}</sum>)
return <unique>{$x}</unique>
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
25
XML/SQL
• Functions for generating XML documents as SQL results:– XMLELEMENT– XMLFOREST– XMLATTRIBUTES– XMLAGG– XMLCONCAT
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
26
Example table structure and dataPERSONpid name yearofbirth
1 John Higgins 19752 Steven Hendry 19733 Mathew Stevens 19824 Ronnie O'Sullivan 19805 Ken Doherty 19746 Steve Davis 19607 Paul Hunter 1983
CARlicencenumber color brand model year owner
ABC123 black NISSAN Cherry 1995 1CCD457 blue FIAT Forza 2001 2DKL998 green SAAB 9000C 1998 3RSQ199 black NISSAN Micra 1999 4WID387 red FIAT Nova 2003 5ROO197 blue SAAB 900i 1982 3TYD226 black NISSAN Cherry 1990 1PTF357 red VOLVO V70 2001 6
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
27
XMLELEMENT• Creates an XML element with a specified name and content:
SELECT XMLELEMENT(NAME "Person", name)FROM Person
<Person>John Higgins</Person>
<Person>Steven Hendry</Person>
<Person>Mathew Stevens</Person>
<Person>Ken Doherty</Person>
<Person>Steve Davis</Person>
<Person>Paul Hunter</Person>
<Person>Ronnie O'Sullivan</Person> (1 result row per element)
DB2: SELECT xml2clob(XMLELEMENT(NAME "Person", name)) FROM Person
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007
28
XMLATTRIBUTES• Creates an XML attributes to be placed inside an XML element:
SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth), name)FROM Person