nikos dimitrakas – IS4/2i1242/2i4042 spring XML Query Languages Database Systems (4th edition) Chapter 30.5, , Articles & Excerpts XML Query Languages * 2 XQuery Computer Environment Tutorials DB2 & XML Querying XML Data with XQuery
nikos dimitrakas – IS4/2i1242/2i4042 spring 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 XPath Path expressions for navigating through XML structures Possible to set conditions and use wildcards Also includes many functions Example:
nikos dimitrakas – IS4/2i1242/2i4042 spring XSLT Enables transformations between different XML structures (mostly used for XML to HTML transformations) Example: a b
nikos dimitrakas – IS4/2i1242/2i4042 spring 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.
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring XPath / /elementX//elementY Wildcards * nodes() Predicates: [predicate]: –/element[1] and.. (current node and parent node) | (concatenation)
nikos dimitrakas – IS4/2i1242/2i4042 spring XPath Examples Any Results node: –//Results Any Phase that is cancelled The Date of a Phase that has a Results The Results of the Phase with Index 2 or 3 |
nikos dimitrakas – IS4/2i1242/2i4042 spring XPath Axes child, ancestor, descendant, parent –/Test/child::Phase (equivalent to /Test/Phase) –//Results/ancestor::Test
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery For –Loop through a list/set of nodes/values Let –Assignments Where –Conditions Order By –Sort the result Return –Construct an output structure
nikos dimitrakas – IS4/2i1242/2i4042 spring 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.
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery Variables start with $: –for $a in //Book/Author –let $n := Sets: –for $x in (1, 2, 3) –let $y := (1, 2, 3) Evaluating expressions: –Enclose the expression in { }: – {$x*3}
nikos dimitrakas – IS4/2i1242/2i4042 spring 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} }
nikos dimitrakas – IS4/2i1242/2i4042 spring 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)
nikos dimitrakas – IS4/2i1242/2i4042 spring 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)
nikos dimitrakas – IS4/2i1242/2i4042 spring 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()
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery/XPath operators +, -, *, div, mod =, !=, >, = eq, ne, lt, le, gt, ge or, and, not is, >>, <<
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery functions element() –/element() (similar to /*) attribute() –/Test/attribute() (similar to node() (follows the standard??) –/node() – element nodes and text nodes – attribute nodes
nikos dimitrakas – IS4/2i1242/2i4042 spring 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}}
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery – Conditionals if-then-else for $a in (1 to 5) return if ($a mod 2 = 0) then {$a} else {$a}
nikos dimitrakas – IS4/2i1242/2i4042 spring XQuery – Quantifiers some for $a in /Test where some $b in satisfies string($b) = "finished" return $a/Description every for $a in /Test where every $b in satisfies string($b) = "finished" return $a/Description
nikos dimitrakas – IS4/2i1242/2i4042 spring 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}
nikos dimitrakas – IS4/2i1242/2i4042 spring XML/SQL Functions for generating XML documents as SQL results: –XMLELEMENT –XMLFOREST –XMLATTRIBUTES –XMLAGG –XMLCONCAT
nikos dimitrakas – IS4/2i1242/2i4042 spring Example table structure and data
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring XMLATTRIBUTES SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth AS "Year", name)) FROM Person
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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)
nikos dimitrakas – IS4/2i1242/2i4042 spring 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)
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring Aggregates without XMLAGG SELECT XMLELEMENT(NAME "Color", XMLATTRIBUTES(color, COUNT(*) as "Amount")) FROM Car GROUP BY color
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring 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
nikos dimitrakas – IS4/2i1242/2i4042 spring Update XML data Path expressions –simple version of XPath update-function –replaces a specified path with a new value –returns the entire XML document
nikos dimitrakas – IS4/2i1242/2i4042 spring More Information XPath – XQuery – – XML/SQL – linsql.html –