XML Query (XQuery) – Part II

XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)

Erik Wilde, UC Berkeley School of Information
2013-12-02

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: XML Query (XQuery) – Part II

Contents

E. Wilde: XML Query (XQuery) – Part II

(2) Abstract

XML and relational databases are not entirely different things: they share the basic model of providing support for storing and processing (potentially large amounts of) data. SQL/XML is an approach to bridge the two worlds of relational and XML databases, by allowing relational database to produce XML, and even to store and query XML. In the second half of this lecture we are taking a second look at XQuery, specifically its processing model and the way how queries are specifying the input documents to XQuery expressions.



XML Storage in Databases

Outline (XML Storage in Databases)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(4) Model Mapping



XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(5) XML is Text



XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(6) XML → ∗LOB

xml-storage-lob.png

XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(7) XML as a Datatype



XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(8) XML Datatype

xml-storage-datatype.png

XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(9) Mapping XML to Models



XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(10) Shredding (XML → Columns)

xml-storage-shredding.png

XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(11) XML as First-Class Citizen



XML Storage in Databases E. Wilde: XML Query (XQuery) – Part II

(12) XML DBMS

xml-storage-xdbms.png

XML in Relational Databases

Outline (XML in Relational Databases)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
XML in Relational Databases E. Wilde: XML Query (XQuery) – Part II

(14) RDBish XML



XML in Relational Databases E. Wilde: XML Query (XQuery) – Part II

(15) Problematic XML



SQL/XML

SQL/XML E. Wilde: XML Query (XQuery) – Part II

(17) SQL/XML:2003

  • SQL/XML provides XML Support in RDBMSs
    • it introduces XML as a Datatype [XML as a Datatype (1)]
    • it introduces a number of operations for generating XML from query results
    • it defines mappings to bridge both worlds (SQL and XML)
  • SQL/XML does not change anything about the database model
    • data is still stored in tables only
    • a column of a table may use the XML type
    • queries may return results in XML rather than as SQL result sets


SQL/XML E. Wilde: XML Query (XQuery) – Part II

(18) SQL/XML Example

SELECT
  e.EmpId,
  e.FirstName,
  e.LastName,
  e.StartDate,
  e.EndDate
FROM Employees e WHERE e.EmpId = 12
SELECT
  XMLELEMENT(NAME "employee",
    XMLATTRIBUTES(e.EmpId as "id"),
    XMLELEMENT(NAME "names",
      XMLELEMENT(NAME "first", e.FirstName),
      XMLELEMENT(NAME "last", e.LastName)),
    XMLELEMENT(NAME "hire-dates",
      XMLATTRIBUTES(e.StartDate as "start", e.EndDate as "end")))
FROM Employees e WHERE e.EmpId = 12


SQL/XML E. Wilde: XML Query (XQuery) – Part II

(19) SQL/XML:2008

  • Adds the concept of XML Tables
  • XML Tables are not tables, they are containers for XML
  • SQL/XML:2008 changes the database's data model
    • it is now possible to have a database with no tables
    • likely use cases are to have both: traditional and XML tables
  • SQL/XML:2008 defines a hybrid database: relational and XML database


Processing Model

Outline (Processing Model)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
Processing Model E. Wilde: XML Query (XQuery) – Part II

(21) Prolog



Processing Model E. Wilde: XML Query (XQuery) – Part II

(22) Tuple Stream



Processing Model E. Wilde: XML Query (XQuery) – Part II

(23) Serialization



XQuery Sources

Outline (XQuery Sources)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(25) XQuery using Files

File-based XQuery Processing

XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(26) XQuery using an XML Database

DB-based XQuery Processing

XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(27) Specifying XQuery Input



XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(28) Finding Activities Saxon)

declare default element namespace "http://www.topografix.com/GPX/1/1";
declare namespace saxon="http://saxon.sf.net/";
declare option saxon:output "method=text";

declare variable $dir := '/Users/dret/Desktop/Dropbox/training/';
declare variable $file := '2010-*.gpx';
declare variable $files := collection(concat($dir,'?select=',$file));

declare variable $lon := -122.48;
declare variable $lat := 37.82;
declare variable $box := 0.01;

declare variable $lonlower := $lon - $box;
declare variable $lonupper := $lon + $box;
declare variable $latlower := $lat - $box;
declare variable $latupper := $lat + $box;

( "Searching", count($files), "files with", count($files/gpx/trk/trkseg/trkpt), "track points:
", 

for $activity in $files
    where exists($activity/gpx/trk/trkseg/trkpt[ (@lon > $lonlower) and
                                                 (@lon < $lonupper) and
                                                 (@lat > $latlower) and
                                                 (@lat < $latupper) ])
    return ( saxon:format-date(xs:date(substring($activity/gpx/metadata/time/text(),1,10)), "[MNn] [D], [Y]"),
             ": ",
             $activity/gpx/trk/name/text(),
             "&#xa;") )


XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(29) Finding Activities (xDB)

declare default element namespace "http://www.topografix.com/GPX/1/1";

declare variable $files := doc("/");

declare variable $lon := -122.48;
declare variable $lat := 37.82;
declare variable $box := 0.01;

declare variable $lonlower := $lon - $box;
declare variable $lonupper := $lon + $box;
declare variable $latlower := $lat - $box;
declare variable $latupper := $lat + $box;

( "Searching", count($files), "files with", count($files/gpx/trk/trkseg/trkpt), "track points:&#xa;", 

for $activity in $files
    where exists($activity/gpx/trk/trkseg/trkpt[ (@lon > $lonlower) and
                                                 (@lon < $lonupper) and
                                                 (@lat > $latlower) and
                                                 (@lat < $latupper) ])
    return ( substring($activity/gpx/metadata/time/text(),1,10),
             ": ",
             $activity/gpx/trk/name/text(),
             "&#xa;") )


XQuery Sources E. Wilde: XML Query (XQuery) – Part II

(30) XQuery Input in xDB



More XQuery Details

Outline (More XQuery Details)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
More XQuery Details E. Wilde: XML Query (XQuery) – Part II

(32) Comma Operator



More XQuery Details E. Wilde: XML Query (XQuery) – Part II

(33) Whitespace in XQuery

declare boundary-space strip;
let $a := "Bob Glushko"
return
  <book>
    <title>Document Engineering</title>
    <author> { $a } </author>
  </book>


More XQuery Details E. Wilde: XML Query (XQuery) – Part II

(34) Constructors



Conclusions

Outline (Conclusions)

  1. XML Storage in Databases [9]
  2. XML in Relational Databases [5]
    1. SQL/XML [3]
  3. Processing Model [3]
  4. XQuery Sources [6]
  5. More XQuery Details [3]
  6. Conclusions [1]
Conclusions E. Wilde: XML Query (XQuery) – Part II

(36) XQuery vs. XSLT



2013-12-02 XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)