XML Databases 6. SQL/XML Silke Eckstein Andreas Kupfer Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 2
6.1 Introduction Creating XML documents from a database Introduced in the last chapter On a more or less conceptual level Not handled so far Creating XML documents inside a database Retrieving data from XML documents Changing XML document content Solution: Integration in database SQL/XML XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 3
6.1 Introduction SQL/XML Storage of XML in all big commercial DBMS available Proprietary solution for embedding in SQL SQL/XML = Part 14 of the SQL-Standard: XML functionality Incorporates the corresponding standards for XML (XML Schema, XQuery) Basic idea: Mapping of SQL concepts to XML (see last chapter) Own datatype to store XML [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 4
6.1 Introduction SQL/XML Storing XML documents inside the database as values of type XML <City> <City> <City> <Name> Braunschweig </Name> <Zip>38100</Zip> <Zip>38106</Zip> <State> Niedersachsen </State> </City> Generating XML documents using SQL/XML functions Datatype XML with belonging functions Mapping between SQL and XML Embedding XQuery in SQL Mapping between SQl and XML SQL XQuery SQL database XML datatype [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 5
6.1 Introduction Mapping SQL database to XML SQL charset to unicode (depends on implementation) SQL identifiers to XML names SQL data types to XML schema data types SQL values to XML values SQL tables to XML and XML schema documents SQL schemas to XML and XML schema documents SQL catalogues to XML and XML schema documents [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 6
Mapping SQL tables CREATE TABLE Account ( Name CHAR(20), Balance NUMERIC(12,2), ); Name Balance Joe 2000 Jim 3500 [Tür08] Mapping SQL table columns to XML elements Mapping table rowstoxml <row> elements <ACCOUNT> <row> <NAME>Joe</NAME> <BALANCE>2000</BALANCE> </row> <row> <NAME>Jim</NAME> <BALANCE>3500</BALANCE> </row> </ACCOUNT> <xsd:complextypename="row.account"> <xsd:sequence> <xsd:element name="name" type="char_20"/> <xsd:element name="balance" type="numeric_12_2"/> </xsd:sequence> </xsd:complextype> <xsd:complextype name="table.account"> <xsd:annotation><xsd:appinfo> <xqlxml:sqlname type="base TABLE" localname="account"/> </xsd:appinfo></xsd:annotation> <xsd:sequence> <xsd:element name="row" type="row.account"/> </xsd:sequence> </xsd:complextype> <xsd:element name="account" type="table.account"/> XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 7
6.1 Introduction Relational table: Cities City Zip State Braunschweig 38100 Niedersachsen Braunschweig 38106 Niedersachsen Hannover 30159 Niedersachsen Many possible XML documents... <City> <Name>Braunschweig</Name> <Zip>38100</Zip> <Zip>38106</Zip> <State>Niedersachsen</State> </City>...... <State name="niedersachsen"> <City name="braunschweig"> <Zip>38100</Zip> <Zip>38106</Zip> </City> </State>... XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 8
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 9
6.2 Publishing relational data XMLELEMENT creates an XML element Example: creating name and content XMLELEMENT( NAME "City", 'Bad Oeynhausen' ) Creates <City>Bad Oeynhausen</City> Can contain attributes, comments and other elements and options XMLELEMENT( NAME "City", XMLCOMMENT ( "Example 2" ), XMLATTRIBUTES('Bayern' AS "State", '80469' AS "Zip" ),'München' ) Creates <City State="Bayern" Zip="80469"><! Example 2 --> München</City> [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 10
6.2 Publishing relational data XMLELEMENT referencing the database Can be used directly from an SQL statement SELECT XMLELEMENT( NAME "City", XMLCOMMENT ( "Example 3" ), XMLATTRIBUTES( "State", "Zip" AS "PLZ" ), "City" ) FROM Cities WHERE ; Creates <City STATE="Niedersachsen" PLZ="38100"> <! Example 3 --> Braunschweig </City> [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 11
6.2 Publishing relational data XMLELEMENT nesting Example SELECT XMLELEMENT( NAME "City", XMLELEMENT( NAME "Name", "City" ), XMLELEMENT( NAME "State", "State" ), XMLELEMENT( NAME "Zip", "Zip" ) ) FROM Cities WHERE ; Creates <City> <Name>Braunschweig</Name> <State>Niedersachsen</State> <Zip>38100</Zip> </City> [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 12
6.2 Publishing relational data XMLELEMENT syntax diagram [IBM] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 13
6.2 Publishing relational data XMLFOREST Constructs a forest of elements without attributes SELECT XMLFOREST ( "City", "State" ) FROM Cities; Creates <City>Braunschweig</City><State>Niedersachsen</State> <City>Braunschweig</City><State>Niedersachsen</State> <City>Braunschweig</City><State>Niedersachsen</State> <City>Hannover</City><State>Niedersachsen</State> [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 14
6.2 Publishing relational data XMLFOREST syntax diagram [IBM] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 15
6.2 Publishing relational data XMLCONCAT Concatenates multiple XML fragments into a single XML pattern Compare outputs SELECT XMLELEMENT("city", City) AS "CITY", XMLELEMENT("zip", Zip) AS "ZIP", XMLELEMENT("state", State) AS "STATE" FROM Cities; SELECT XMLCONCAT( XMLELEMENT("city", CITY), XMLELEMENT("zip", ZIP), XMLELEMENT("state", STATE) ) FROM Cities; [Pow07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 16
6.2 Publishing relational data XMLAGG Aggregates seperate lines of output into a single string SELECT CITY, XMLAGG( XMLELEMENT(NAME "Zip", Zip)) AS "Zipcodes" FROM Cities GROUP BY City; Creates City Braunschweig Hannover Zipcodes <Zip>38100</Zip> <Zip>38106</Zip> <Zip>30159</Zip> [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 17
6.2 Publishing relational data XMLAGG Allows sorting SELECT XMLAGG( XMLELEMENT("address", Zip ' ' City) ORDER BY Zip DESC) FROM Cities; Creates <address>38106 Braunschweig</address> <address>38100 Braunschweig</address> <address>30159 Hannover</address> Disadvantage: Can only aggregate a single element, and thus fields are concatenated [Pow07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 18
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 19
6.3 XML data type Storing XML in relational databases is possible as Character data (VARCHAR, Character Large OBject) New data type XML A value of the data type XML can contain whole XML document XML element a set of XML elements All XML publishing operators from chapter 6.2 create values of the data type XML, not a string XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 20
6.3 XML data type XML(SEQUENCE) NULL or document node Untyped elements & attributes, elements not NULL XML(CONTENT(ANY)) 1 element child Validated against schema XML(CONTENT(UNTYPED)) XML(CONTENT(XMLSCHEMA)) 1 element child XML(DOCUMENT(ANY)) Validated against schema 1 element child XML(DOCUMENT(UNTYPED)) XML(DOCUMENT(XMLSCHEMA)) [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 21
6.3 XML data type Specification of XML type XML [({DOCUMENT CONTENT SEQUENCE} [({ANY UNTYPED XMLSCHEMA schema name})])] Modifiers are optional Primary type modifier DOCUMENT (XML document) CONTENT (XML element) SEQUENCE (sequence of XML elements) Secondary type modifier UNTYPED XMLSCHEMA (typed) ANY (may be typed) [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 22
6.3 XML data type Create a table that is an XML data type in itself CREATE TABLE XMLDOCUMENT OF XMLTYPE; Create a table containing an XMLType data type column CREATE TABLE XML ( ID NUMBER NOT NULL, XML XMLTYPE, CONSTRAINT XPK PRIMARY KEY (ID) ); [Pow07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 23
6.3 XML data type Example: Definition of an XML type column CREATE TABLE Groups ( ID INTEGER, Name XML ); ID Name 123 <Groups>Annabelle</Groups> 234 <Groups>Magdalena, Marius</Groups> 345 <?xml version 1.0?> <Groups> <Person>Patrick</Person> <Person>Robert</Person> </Groups> 654 <Groups>Rebecca</Groups> <Groups>Torben</Groups> XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 24
6.3 XML data type Characteristics Allowed values: XML documents (including prolog) XML content according to XML 1.0 (includes pure text comments, PI?) NULL No comparison possible (compare CLOB in SQL) User can define an order, if comparison is necessary No corresponding type in programming languages for embedding in SQL available Standard defines operators to convert to other SQL data types [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 25
6.3 XML data type Parsing & Serialization XMLParse: Parses a string value using an XML parser Produces value whose specific type is <City> <Name> Braunschweig </Name> <Zip>38100</Zip> <Zip>38106</Zip> <State> Niedersachsen </State> </City> XML(DOCUMENT(ANY)), or CONTENT, or XMLSerialize Transforms an XML value into a string value (CHAR, VARCHAR, CLOB, or BLOB) <City> <Name> Braunschweig </Name> <Zip>38100</Zip> <Zip>38106</Zip> <State> Niedersachsen </State> </City> [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 26
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 27
6.4 Queries Motivation How can SQL applications locate and retrieve information in XML documents stored in an SQL database cell? Invoking XML query language within SQL statements Retrieve information in SELECT list Locate information in WHERE clause Details on XML query language XQuery later [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 28
6.4 Queries XMLQuery A new SQL expression, invoked as a pseudofunction, whose data type can be an XML type such as XML(CONTENT(ANY)) or an ordinary SQL type XMLExists A new SQL predicate, invoked as a pseudo-function, returning true when the contained XQuery expression returns anything other than the empty sequence (false) or SQL null value (unknown) [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 29
6.4 Queries XMLQuery syntax XMLQUERY(<XQuery expression> [PASSING <argument list>] {NULL EMPTY} ON EMPTY) argument list := <SQL value> AS <XQuery variable> Example SELECT XMLQUERY( '<State name="{$name}"><city>{$city}</city></state>' PASSING State as $Name, City AS $City NULL ON EMPTY) AS CityList FROM Cities; CityList <State name="niedersachsen"><city>braunschweig</city></state> <State name="niedersachsen"><city>hannover</city></state> [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 30
6.4 Queries CREATE TABLE Papers (ID INTEGER, Paper XML); ID Paper 123 <Paper> <author>alice</author><title>perpetual Motion</title><year>1999</year></Paper> 345 <Paper><year>2005</year><author>Bob</author><author>Charlie </author><title>beer</title> </Paper> SELECT ID, XMLQUERY( 'FOR $a IN $p//author RETURN <Authors>{$a/text()}</Authors>' PASSING Paper AS "p") AS AuthorNames FROM Papers; ID AuthorNames 123 <Authors>Alice</Authors> 345 <Authors>Bob</Authors> <Authors>Charlie</Authors> [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 31
6.4 Queries XMLTABLE Provides an SQL view of XML data Output is not of the XML type Evaluates an XQuery row pattern with optional arguments (as with XMLQuery) Element/attribute values mapped to columns using XQuery column patterns Names & types of columns required; default values optional Syntax: XMLTABLE (<XQuery expression> PASSING <argument list> COLUMNS <column list>) column := <name> <type> PATH <path expression> [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 32
6.4 Queries XMLTable: Example SELECT ID, t.* FROM Papers p, XMLTABLE( 'for $root in $papers where $root//author/text() = "Bob" return $root/paper' PASSING p.paper as "papers" COLUMNS About VARCHAR(30) PATH '/Paper/title', Created INTEGER PATH '/Paper/year' ) AS t; ID About Created 345 Beer 2005 [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 33
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 34
6.5 Validation Validation of XML Is like integrity constraints in DBs Requires an XML Schema XML Schemas may be registered with the SQL-server Implementation-defined mechanism Known by SQL name & by target namespace URI Schema does need a unique name Used by XMLValidate(), IS VALID, and to restrict values of XML(DOCUMENT-or-CONTENT(XMLSCHEMA)) [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 35
6.5 Validation Schema registration Register XMLSCHEMA 'http://www.alfred-moos.de/grussschema.xsd' FROM 'file://c:/xml_schemata/grussschema.xsd' AS GrussSchema COMPLETE ; CREATE TABLE Dokument_XML (Dokument_XML_Nr CHAR (4) NOT NULL PRIMARY KEY, Dokument XML, CONSTRAINT validieren CHECK (Dokument IS VALIDATED ACCORDING TO XMLSCHEMA ID GrussSchema ) ) ; XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 36
6.5 Validation Schema definition Syntax XML(CONTENT(XMLSCHEMA) <schema> [<elements>])) <schema> := URI <namespace> [LOCATION <loc>] NO NAMESPACE [LOCATION <loc>] ID <registered schema name> <element> := [NAMESPACE <namespace>] ELEMENT <element name> [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 37
6.5 Validation New functions and predicates: XMLValidate Validates an XML value against an XML Schema (or target namespace), returning new XML value with type annotations IS VALID Tests an XML value to determine whether or not it is valid according to an XML Schema (or target namespace); return true/false without altering the XML value itself IS DOCUMENT determines whether an XML value satisfies the (SQL/XML) criteria for an XML document IS CONTENT determines whether an XML value satisfies the (SQL/XML) criteria for XML content [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 38
6.5 Validation Benefits of schema registration Security issues Schemas cannot disappear without SQLserver knowing about it Schemas cannot be hijacked (altered in inappropriate ways) without SQL-server knowing about it Documents cannot be marked valid against schemas unless SQL-server knows about them [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 39
6.5 Validation Predefined schemas (build-in namespaces) xs:http://www.w3.org/2001/xmlschema xsi:http://www.w3.org/2001/xmlschema-instance sqlxml:http://standards.iso.org/iso/9075/2003/sqlxml More depending on the DB implementation Completely supported per XML+Namespaces: XMLElement, XMLForest, XMLTable Default namespace, explicit namespace (prefix) Declare namespace within scopes of WITH clause, column definitions, constraint definitions, insert/delete/update statements, compound statements [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 40
6. SQL/XML 6.1Introduction 6.2 Publishing relational data in XML 6.3 XML data type 6.4 Queries 6.5 Validation 6.6 SQL/XML standard 6.7 Overview 6.8 References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 41
6.6 SQL/XML standard SQL/XML standard published as ISO/IEC 9074-14:2003 Mappings and Publishing Functions ISO/IEC 9075-14:2006 Adds XQuery, including Data Model, Validation ISO/IEC 9075-14:2008 Updates Something else? [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 42
6.6 SQL/XML standard SQL/XML:2003 plus Additional publishing functions XQuery data model More precise XML type (modifiers) XMLQuery, XMLTable XMLValidate, IS VALID XMLExists, IS DOCUMENT, IS CONTENT Casting between XML type and SQL types [Mel05] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 43
6.6 SQL/XML standard Overview of some operators for the XML type XMLELEMENT creates an XML element node XMLFOREST creates a sequence of XML element nodes from a table XMLCOMMENT creates an XML comment node XMLTEXT creates a text node XMLPI creates a processing instruction XMLAGG aggregates XML values of a group XMLCONCAT concatenates XML type values XMLTRANSFORM applies an XSL to a document [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 44
6.6 SQL/XML standard... Overview of some operators for the XML type XMLPARSE a well-formed SQL text to XML value XMLSERIALIZE converts an XML value to a SQL text XMLDOCUMENT creates an XML document node from an XML value XMLVALIDATE validates an XML value with a schema XMLQUERY evaluates an XQuery expression XMLTABLE transforms an XQuery result to a SQL table XMLITERATE transforms an XQery sequence to a SQL table [Tür08] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 45
6.6 SQL/XML standard Review of SQL/XML Two components A data type XML to store XML data Functions to map relational structures to XML Only construction operators No extraction of values or search But construction operators are based on XQuery Mapping of tables, schemas, catalogues ignores some information from the relational schema UNIQUE REFERENCES CHECK Further extensions are expected [Kud07] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 46
6.7 Overview 1. Introduction 2. XML Basics 3. Schema definition 4. XML query languages I 5. Mapping relational data to XML 6. SQL/XML 7. XML processing 8. XML query languages II 9. XML storage I 10. XML storage - index 11. XML storage - native 12. Updates / Transactions 13. Systems 14. XML Benchmarks XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 47
6.8 References "XML und Datenbanken" [Tür08] Can Türker Lecture, University of Zurich, 2008 Beginning XML Databases. [Pow07] Gavin Powell Wiley & Sons, 2007, ISBN 0471791202 "XML-Datenbanken", [Kud07] Thomas Kudraß Lecture, HTWK Leipzig, WS2007/2008 "SQL/XML", [Mel05] Jim Melton, Oracle Corp. 2005 XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 48
6.8 References XQuery und SQL/XML in DB2-Datenbanken: Verwaltung und Erzeugung von XML- Dokumenten in DB2 [Moo08] Alfred Moos Vieweg+Teubner, 2008 ISO/IEC 9075-14:2003 Information Technology - Database Languages - SQL - Part 14: XML-Related Specifications (SQL/XML) DB2 SQL-Reference, IBM, March 2008 [IBM] XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 49
Questions, Ideas, Comments Now, or... Room: IZ 232 Office our: Tuesday, 12:30 13:30 Uhr or on appointment Email: eckstein@ifis.cs.tu-bs.de XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 50