Download presentation
Presentation is loading. Please wait.
Published byViolet Watkins Modified over 9 years ago
1
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
2
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.) …
3
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
4
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 4 XSLT Enables transformations between different XML structures (mostly used for XML to HTML transformations) Example: a b
5
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 5 XQuery Query language for XML Combines XPath and FLWOR expressions –FLWOR: For Let Where Order by Return Supports use of all the functions included in XPath Inspired by many other languages like SQL, OQL, Lorel, etc.
6
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 6 XML/SQL SQL 2003 (ISO standard) –Support for XML in “relational” databases Storage Querying through XQuery –Support for constructing XML from relational data with SQL
7
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 7 Proprietary Languages Support for XML data and transformations between XML and relational data. IBM: –SQL UDFs: Extract-functions, Update-function –Mapping schemes: DAD-files Oracle: –SQL UDFs: Extract-functions, existsNode, etc. Others
8
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 8 Example This test is about the effects of computer games on the human brain Choose the computer games to be used for the test Age of Empires Flight Simulator Tetris Let users try the computer games Many users find Flight Simulator hard due to 3D environment and multiple controls Try to get funding from EU Present the results of the test
9
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 9 XPath / // @ /element/@attribute /elementX//elementY Wildcards * nodes() Predicates: [predicate]: –/element[1] –/element[@attribute=value]. and.. (current node and parent node) | (concatenation)
10
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 10 XPath Examples Any Results node: –//Results Any Phase that is cancelled –//Phase[@status=‘cancelled’] The Date of a Phase that has a Results –//Phase/Results/../@Date The Results of the Phase with Index 2 or 3 –/Test/Phase[@Index=2]/Results | /Test/Phase[@Index=3]/Results
11
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 11 XPath Axes child, ancestor, descendant, parent –/Test/child::Phase (equivalent to /Test/Phase) –//Results/ancestor::Test
12
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 12 XQuery For –Loop through a list/set of nodes/values Let –Assignments Where –Conditions Order By –Sort the result Return –Construct an output structure
13
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 13 XQuery FLWOR expressions can be nested. No clause is compulsory. XPath expressions can be used in any clause The result can be a valid XML structure, but it doesn’t have to be. The function doc() can be used to define the input (XML source), or the execution environment can define an input context.
14
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 14 XQuery Variables start with $: –for $a in //Book/Author –let $n := $a/@Name Sets: –for $x in (1, 2, 3) –let $y := (1, 2, 3) Evaluating expressions: –Enclose the expression in { }: – {$x*3}
15
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 15 XQuery – Multiple results One result per value in the loop created by the for clause: for $x in (1,2,3) return {$x} Place the result in a new result: { for $x in (1,2,3) return {$x} }
16
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 16 XQuery/XPath functions Sequence functions: –distinct-values(s) –min(s), max(s), sum(s), avg(s) –empty(s), exists(s) –union(s1,s2), instersect(s1,s2), except(s1,s2) –concat(s1,s2) Node functions: –name(n), local-name(n), node-name(n)
17
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 17 XQuery/XPath functions String functions: –matches(s, regexp) –concat(s1,s2) –starts-with(s1,s2), ends-with(s1,s2), contains(s1,s2) –substring(s, start), substring(s, start, length) –lower-case(s), upper-case(s) –replace(s, pattern, replacement) –tokenize(s, pattern)
18
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 18 XQuery/XPath functions Other functions: –doc(URI) –not(e) –several date/time functions –several numeric functions –data(ns) – Sequence of nodes to sequence of atomic values –number(n) – The value of a node as a number or NaN –string(n) – The value of the node as a string –current-time(), current-date(), current-dateTime()
19
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 19 XQuery/XPath operators +, -, *, div, mod =, !=, >, = eq, ne, lt, le, gt, ge or, and, not is, >>, <<
20
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 20 XQuery functions element() –/element() (similar to /*) attribute() –/Test/attribute() (similar to /Test/@*) node() (follows the standard??) –/node() – element nodes and text nodes –/@node() – attribute nodes
21
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 21 XQuery – Computed Constructors element –element name value: let $a := “a”, $b := 2 return {element {$a} {$b}} attribute –attribute name value: let $a := “a”, $b := 2 return {attribute {$a} {$b}}
22
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 22 XQuery – Conditionals if-then-else for $a in (1 to 5) return if ($a mod 2 = 0) then {$a} else {$a}
23
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 23 XQuery – Quantifiers some for $a in /Test where some $b in $a/Phase/@Status satisfies string($b) = "finished" return $a/Description every for $a in /Test where every $b in $a/Phase/@Status satisfies string($b) = "finished" return $a/Description
24
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 {$a + $b} ) return {$x}
25
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 25 XML/SQL Functions for generating XML documents as SQL results: –XMLELEMENT –XMLFOREST –XMLATTRIBUTES –XMLAGG –XMLCONCAT
26
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 26 Example table structure and data
27
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 John Higgins Steven Hendry Mathew Stevens Ken Doherty Steve Davis Paul Hunter Ronnie O'Sullivan (1 result row per element) DB2: SELECT xml2clob(XMLELEMENT(NAME "Person", name)) FROM Person
28
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 John Higgins Steven Hendry Mathew Stevens Ken Doherty Steve Davis Paul Hunter Ronnie O'Sullivan
29
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 29 XMLATTRIBUTES SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth AS "Year"), name) FROM Person John Higgins Steven Hendry Mathew Stevens Ken Doherty Steve Davis Paul Hunter Ronnie O'Sullivan
30
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 30 XMLATTRIBUTES SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth AS "Year", name)) FROM Person
31
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 31 XMLCONCAT Combine more elements as a sequence of element. This would give us two columns: SELECT XMLELEMENT(NAME "Name", name), XMLELEMENT(NAME "Year", yearofbirth) FROM Person This would give us one column in the result: SELECT XMLCONCAT(XMLELEMENT(NAME "Name", name), XMLELEMENT(NAME "Year", yearofbirth)) FROM Person
32
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 32 XMLCONCAT John Higgins 1975 Steven Hendry 1973 Mathew Stevens 1982 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Ronnie O'Sullivan 1980 Still multiple rows though
33
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 33 XMLFOREST Create multiple elements: SELECT XMLFOREST(name AS "Name", yearofbirth AS "Year") FROM Person John Higgins 1975 Steven Hendry 1973 Mathew Stevens 1982 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Ronnie O'Sullivan 1980
34
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 34 Combinations Or make this more complete by combining different functions: SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(pid AS "ID"), XMLFOREST(name AS "Name", yearofbirth AS "Year")) FROM Person John Higgins 1975 Steven Hendry 1973 Mathew Stevens 1982 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Ronnie O'Sullivan 1980
35
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 35 XMLAGG Grouping many rows into one result. Can be used together with a GROUP BY clause. Put all the persons in one result row: SELECT XMLAGG(XMLELEMENT(NAME "Person", name)) FROM Person John Higgins Steven Hendry Mathew Stevens Ken Doherty Steve Davis Paul Hunter Ronnie O'Sullivan One result row! (though not well-formed XML)
36
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 36 Combinations Put all the persons in one Persons element: SELECT XMLELEMENT(NAME "Persons", XMLAGG(XMLELEMENT(NAME "Person", name))) FROM Person John Higgins Steven Hendry Mathew Stevens Ken Doherty Steve Davis Paul Hunter Ronnie O'Sullivan One result row! (AND well-formed XML)
37
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 37 XMLAGG with GROUP BY One row per group: SELECT XMLELEMENT(NAME "Color", XMLATTRIBUTES(color), XMLAGG(XMLELEMENT(NAME "Car", licencenumber))) FROM Car GROUP BY color ABC123 TYD226 RSQ199 CCD457 ROO197 DKL998 WID387 PTF357
38
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 38 Aggregates without XMLAGG SELECT XMLELEMENT(NAME "Color", XMLATTRIBUTES(color, COUNT(*) as "Amount")) FROM Car GROUP BY color
39
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 39 DB2's extra XML support Store XML documents Query data inside the XML structures Change data inside the XML structures Validate XML documents against DTDs Decompose XML documents into tables
40
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 40 Query XML data Path expressions –simple version of XPath extract-functions –two functions for each data type 1 singleton function, e.g. extractInteger 1 plural function, e.g. extractIntegers
41
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 41 Update XML data Path expressions –simple version of XPath update-function –replaces a specified path with a new value –returns the entire XML document
42
nikos dimitrakas – IS4/2i1242/2i4042 spring 2007 42 More Information XPath –http://www.w3schools.com/xpath/ XQuery –http://www.w3schools.com/xquery/ –http://www.stylusstudio.com/xml_tutorial.html XML/SQL –http://docs.openlinksw.com/virtuoso/composingxm linsql.html –http://www.stylusstudio.com/sqlxml_tutorial.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.