new xml und datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · lecture "xml and...

90
Chapter 10 Database Systems with XML Support Oracle12c DB2 11.0 Microsoft SQL Server 2017 PostgreSQL 11

Upload: others

Post on 12-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

Chapter 10

Database Systems with XML Support

Oracle12c

DB2 11.0

Microsoft SQL Server 2017

PostgreSQL 11

Page 2: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-2Lecture "XML and Databases" - Dr. Can Türker

Example:XML Schema Document

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="book" type="book"/><xsd:complexType name="book">

<xsd:sequence><xsd:element name="author" type="xsd:string"/><xsd:element name="title" type="xsd:string"/><xsd:element name="subtitle" type="xsd:string"/><xsd:element name="publisher">

<xsd:complexType><xsd:simpleContent>

<xsd:extension base="xsd:string"> <xsd:attribute name="city" type="xsd:string"/>

</xsd:extension></xsd:simpleContent>

</xsd:complexType></xsd:element><xsd:element name="edition" type="xsd:int" minOccurs="0"/><xsd:choice minOccurs="0">

<xsd:element name="covertext" type="xsd:string"/><xsd:element name="description" type="xsd:string"/>

</xsd:choice></xsd:sequence><xsd:attribute name="isbn" type="xsd:string"/><xsd:attribute name="year" type="xsd:gYear"/>

</xsd:complexType></xsd:schema>

Page 3: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-3Lecture "XML and Databases" - Dr. Can Türker

Example: XML Documents

<book isbn="3-8266-00258-7" year="1997"><author>Gunter Saake</author><author>Ingo Schmitt</author><author>Can Türker</author><title>Objektdatenbanken</title><subtitle>

Konzepte, Sprachen, Architekturen</subtitle><publisher city="Bonn">

International Thomson Publishing</publisher><covertext>Dieses Lehrbuch ...</covertext>

</book>

<book isbn="3-89864-148-1" year="2003"><author>Meike Klettke</author><author>Holger Meyer</author><title>XML & Datenbanken</title><subtitle>

XML-Dokumente effizient speichernund verarbeiten

</subtitle><publisher city="Heidelberg">dpunkt</publisher><description>Mit der wachsenden ...</description>

</book>XML Document book1.xml XML Document book2.xml

<book isbn="3-89864-219-4" year="2003"><author>Can Türker</author><title>SQL:1999 & SQL:2003</title><subtitle>

Objektrelationales SQL, SQLJ & SQL/XML</subtitle><publisher city="Heidelberg">dpunkt</publisher><description>SQL ist ... </description>

</book>

XML Document book3.xml

<book isbn="3-89864-228-3" year="2003"><author>Gunter Saake</author><author>Kai-Uwe Sattler</author><title>Datenbanken & Java</title><subtitle>JDBC, SQLJ, ODMG & JDO</subtitle><publisher>dpunkt</publisher><edition>2</edition><description> Das Zusammenspiel ...</description>

</book>

XML Document book4.xml

Page 4: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-4Lecture "XML and Databases" - Dr. Can Türker

Oracle XML DB Architecture

Figure taken from Oracle® XML DB Developer's Guide 12c Release 1 (12.1.0.2)

* Note: Oracle deprecated text-based XML storage with Version 12c

Page 5: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-5Lecture "XML and Databases" - Dr. Can Türker

Oracle XML DB Benefits

Figure taken from Oracle® XML DB Developer's Guide 12c Release 1 (12.1.0.2)

Page 6: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-6Lecture "XML and Databases" - Dr. Can Türker

Oracle XML DB Storage Models

Figure taken from Oracle® XML DB Developer's Guide 12c Release 1 (12.1.0.2)

Page 7: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-7Lecture "XML and Databases" - Dr. Can Türker

Mapping XML to Databases

Approaches

– XML column approach: column of type XMLTYPE

– XML table approach: table of type XMLTYPE

Storage options

– Binary (compact in binary XML format)

– Object-relational (structured, schema-based)

Exploit object-relational extensions

– XMLTYPE as predefined object type with SQL/XML functions as methods

– Intermedia-Text package with full-text functionality

– DBMS_XMLDOM package with DOM methods

– DBMS_XMLSCHEMA package with methods for creating and managing XML schemas

– DBMS_XMLGEN package with methods to generate XML from SQL

Page 8: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

XML Column vs. XML Table

Table with XML column

Example

Insert XML value as XML column value

Table with XML rows

Example

Insert XML value as XML row value

11-8Lecture "XML and Databases" - Dr. Can Türker

CREATE TABLE book (content XMLTYPE) ;

INSERT INTO book(content) VALUES(XMLTYPE('<book isbn="3-89864-148-1" year="2003">

<author>Meike Klettke</author><author>Holger Meyer</author> ...

</book>'));

CREATE TABLE book OF XMLTYPE;

INSERT INTO book VALUES(XMLTYPE('<book isbn="3-89864-148-1" year="2003">

<author>Meike Klettke</author><author>Holger Meyer</author> ...

</book>'));

CREATE TABLE <tablename> (<columnname> XMLTYPE)[XMLTYPE [COLUMN] <columnname> [STORE AS BINARY XML][XMLSCHEMA <url> ELEMENT [<url>#]<element>]]

CREATE TABLE <tablename> OF XMLTYPE [XMLTYPESTORE AS BINARY XML][XMLSCHEMA <url> ELEMENT [<url>#]<element>]]

Page 9: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-9Lecture "XML and Databases" - Dr. Can Türker

User-Defined Function to Read XML Documents

CREATE DIRECTORY xmldir AS 'c:\xmldir';GRANT READ ON DIRECTORY xmldir TO PUBLIC;

CREATE FUNCTION getDocument(filename VARCHAR2) RETURN CLOB IS xclob CLOB;BEGIN

SELECT TO_CLOB(BFILENAME('XMLDIR', filename)) INTO xclob FROM DUAL; RETURN xclob;

END;/

INSERT INTO book VALUES (XMLTYPE(getDocument('book1.xml')));

Page 10: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-10Lecture "XML and Databases" - Dr. Can Türker

XML Schema Handling

Package DBMS_XMLSCHEMA provides methods to register, compile, generate, and remove XML schemas

Register XML schema

Create XML table based on this schema (object-relational storage by default)

DBMS_XMLSCHEMA.registerSchema('schema-url', 'xml-schema');DBMS_XMLSCHEMA.compileSchema('schema-url');DBMS_XMLSCHEMA.generateSchema('schema', 'type-name');DBMS_XMLSCHEMA.deleteSchema('schema-url', deleteoption);

deleteoption:1=DELETE_RESTRICT2=DELETE_INVALIDATE3=DELETE_CASCADE4=DELETE_CASCADE_FORCE

BEGINDBMS_XMLSCHEMA.registerSchema('book.xsd', getDocument('book.xsd'));

END;/

CREATE TABLE book OF XMLTYPEXMLSCHEMA "book.xsd" ELEMENT "book";

Page 11: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-11Lecture "XML and Databases" - Dr. Can Türker

Queries

Support for SQL/XML functions

– XMLQUERY, XMLTABLE, XMLEXISTS, XMLCAST

– XMLELEMENT, XMLFOREST, XMLCONCAT, XMLAGG

– XMLPI, XMLCOMMENT, XMLSERIALIZE, XMLPARSE

Plus additional functions

– XMLROOT

– XMLCOLATTVAL

– XMLCDATA

Full-text search

Page 12: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-12Lecture "XML and Databases" - Dr. Can Türker

XMLTYPE

creates XML value

Example:

XMLTYPE(<string-value-expression> )

Joe

Jim

2000

3500

name salaryemployee SELECT XMLTYPE('<employee name="' || name || '"> <salary>' || 12 * salary || '</salary> </employee>') AS worker

FROM employee;

<employee name="Joe"><salary>24000</salary>

</employee>

<employee name="Jim"><salary>42000</salary>

</employee>

WORKER

Page 13: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

creates XML value from an XQuery expression

Example:

7-13Lecture "XML and Databases" - Dr. Can Türker

<book year="2003"><title>Datenbanken & Java</title><authors><author>Gunter Saake</author><author>Kai-Uwe Sattler</author></authors></book>

BOOK

<book year="1997"><title>Objektdatenbanken</title><authors><author>Gunter Saake</author> <author>Ingo Schmitt</author><author>Can Türker</author></authors></book>

XMLQUERY

XMLQUERY(<xquery-expression> [PASSING [BY VALUE] <xquery-argument-list>]RETURNING CONTENT [NULL ON EMPTY])

<xquery-argument> := <sql-value-expression> AS "<xquery-variable-name>"

SELECT XMLQUERY('<book year="{$i//@year}"><title>{$i//title/text()}</title><authors>{for $a in $i//author return <author>{$a/text()}</author>}</authors></book>'

PASSING VALUE(b) AS "i" RETURNING CONTENT) AS bookFROM book bWHERE XMLQUERY('$i/book[author[1] = "Gunter Saake"]' PASSING VALUE(b) AS "i"

RETURNING CONTENT) IS NOT NULL;

Page 14: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-14Lecture "XML and Databases" - Dr. Can Türker

XMLTABLE

creates SQL table from an XQuery expression. Columns option allows user-defined mapping to the table columns. Without this option, an XML table is created.

Example:

XMLTABLE ([<namespace-clause>] <xquery-expression> [PASSING [BY VALUE] <xquery-argument-list>] [COLUMNS <xml-table-column-list>])

<xml-table-column> := <column-name> <column-type> [PATH <xpath-expression>] [DEFAULT<value-expression>]

3-8266-0258-7

3-89864-219-4

3-89864-148-1

ISBN

3-89864-228-3

Objektdatenbanken

SQL:1999 & SQL:2003

XML & Datenbanken

BOOKTITLE

Datenbanken & Java

SELECT t.*FROM book b, XMLTABLE('$i' PASSING VALUE(b) AS "i" COLUMNS

isbn VARCHAR(30) PATH '/book/@isbn', booktitle VARCHAR(50) PATH '/book/title') AS t;

Page 15: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

returns true if the XQuery expression yields a non-null value

Example:

XMLEXISTS(<xquery-expression> [PASSING [BY VALUE] <xquery-expression-list>])

<xquery-argument> := <sql-value-expression> AS "<xquery-variable-name>"

7-15Lecture "XML and Databases" - Dr. Can Türker

XMLEXISTS

Objektdatenbanken

Datenbanken &amp; Java

BOOKTITLE

SELECT XMLQuery('$i//title/text()' PASSING VALUE(b) AS "i"RETURNING CONTENT) AS booktitle

FROM book bWHERE XMLEXISTS('$i//author[text() = "Gunter Saake"]' PASSING VALUE(b) AS "i");

Page 16: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-16Lecture "XML and Databases" - Dr. Can Türker

XMLCAST

Target data type can be any (alpha)numeric SQL data-type, including CLOB/BLOB plus REF XMLTYPE

Example:

XMLCAST(<xml-value-expression> AS <sql-data-type> )

Objektdatenbanken

SQL:1999 & SQL:2003

XML & Datenbanken

BOOKTITLE

Datenbanken & Java

SELECT XMLCAST(XMLQUERY('$i//book/title' PASSING VALUE(b) AS "i" RETURNING CONTENT) AS CLOB) AS booktitle

FROM book b;

Page 17: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-17Lecture "XML and Databases" - Dr. Can Türker

XMLELEMENT

created XML element

Example:

XMLELEMENT(NAME <element-name>[, XMLATTRIBUTES(<xml-attribute-list>)][, <value-expression-list>])

<xml-attribute> := <value-expression> [AS <attribute-name>]

<worker SALARY="2000">Joe</worker>

<worker SALARY="3500">Jim</worker>

WORKER

Joe

Jim

2000

3500

name salaryemployee SELECT XMLELEMENT(NAME "worker", XMLATTRIBUTES(salary), name) AS worker

FROM employee;

Page 18: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

<NAME>Joe</NAME><INCOME><euro>24000</euro></INCOME>

<NAME>Jim</NAME><INCOME><euro>42000</euro></ INCOME>

WORKER

11-18Lecture "XML and Databases" - Dr. Can Türker

XMLFOREST

creates forest of XML elements with optional attributes

Example:

XMLFOREST(<xml-forest-element-list>)

<xml-forest-element>:=<value-expression> [AS <element-name>]

Joe

Jim

2000

3500

name salaryemployee SELECT XMLFOREST(name, XMLELEMENT(NAME "euro", 12*salary) AS income) AS worker

FROM employee;

Page 19: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-19Lecture "XML and Databases" - Dr. Can Türker

XMLCONCAT

concatenates XML elements to a forest

Example:

XMLCONCAT(<xml-value-expression-list>)

Joe

Jim

2000

3500

name salaryemployee

<employee>Joe</employee><euro>24000</euro>

<employee>Jim</employee><euro>42000</euro>

WORKER

SELECT XMLCONCAT(XMLELEMENT(NAME "employee", name), XMLELEMENT(NAME "euro", 12*salary))

AS workerFROM employee;

Page 20: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-20Lecture "XML and Databases" - Dr. Can Türker

XMLAGG

aggregates XML elements

Example:

XMLAGG(<xml-value-expression>[ORDER BY <order-expression-list>] )

SELECT name, XMLAGG(XMLELEMENT(NAME "salary", salary))AS income

FROM employeeGROUP BY name;

Joe

Jim

2000

3500

name salaryemployee

Jim 5000

<salary>2000</salary>

<salary>3500</salary><salary>5000</salary>

Joe

Jim

NAME INCOME

Page 21: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-21Lecture "XML and Databases" - Dr. Can Türker

XMLPARSE, XMLSERIALIZE, XMLCOMMENT & XMLPI

creates XML value (WELLFORMED avoids validity checking)

converts XML value to an SQL string or LOB (in case of schema-based XML values, SHOW DEFAULTS uses the defaults of the given XML schema)

creates XML comment node

creates XML processing instruction node

XMLCOMMENT (<string-value-expression>)

XMLPI (NAME <Identifier> [, <string-value-expression>]))

XMLPARSE({DOCUMENT | CONTENT} <string-value-expression> [WELLFORMED])

XMLSERIALIZE({DOCUMENT | CONTENT} <xml-value-expression> [AS <string-data-type>] [ENCODING <encoding>] [VERSION <xml-version>] [{SHOW|HIDE} DEFAULTS])

Page 22: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-22Lecture "XML and Databases" - Dr. Can Türker

XMLCDATA, XMLROOT & XMLCOLATTVAL

creates XML node of form <![CDATA[<string-value>]]>

creates XML node (if available the XML attributes version and standalone are set)

creates XML elements of the form <column name="Identifier">Value</column>

XMLCDATA(<string-value-expression>)

XMLROOT(<xml-value-expression>, VERSION {<number-text>|NO VALUE} [, STANDALONE {YES|NO [VALUE]}])

XMLCOLATTVAL(<xml-column-value-list>)

<xml-column-value>:=<value-expression> [AS <Identifier>]

Page 23: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

Index Types

– Functional (value)

index

– Full-text index

– XML index

available for binary or text-based XML columns/tables and creates a set of secondary indexes

Path index on all XML tags and fragments

Value index on the order of the document nodes

Value index on the values of the document nodes

Command to avoid using XML index in query

Command to avoid using XML index in query and performing XQuery optimization

CREATE INDEX xmlfunctionalidx ON book b (XMLCAST(XMLQUERY( '$i//@year' PASSING VALUE(b) AS "i" RETURNING CONTENT) AS INT));

11-23Lecture "XML and Databases" - Dr. Can Türker

Indexing (1)

CREATE INDEX xmlfulltextidx ON book b (VALUE(b)) INDEXTYPE IS CTXSYS.CONTEXT;

CREATE INDEX xmlidx ON book b (VALUE(b)) INDEXTYPE IS XDB.XMLIndex;

SELECT /*+ NO_XMLINDEX_REWRITE */ ...

SELECT /*+ NO_XML_QUERY_REWRITE */ ...

Page 24: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-24Lecture "XML and Databases" - Dr. Can Türker

Indexing (2)

Query exploiting the functional index

Query exploiting the full-text index

Query exploiting the XML index

SELECT XMLQUERY('<book title="{$i//title/text()}" year="{$i//@year}"/>' PASSING VALUE(b) AS "i" RETURNING CONTENT) AS book

FROM book bWHERE XMLCAST(XMLQUERY('$i//@year' PASSING VALUE(b) AS "i" RETURNING CONTENT)

AS INT) > 2000;

SELECT XMLQUERY('$i//book/title' PASSING VALUE(b) AS "i" RETURNING CONTENT) AS booktitleFROM book bWHERE XMLEXISTS('$i//book/title' PASSING VALUE(b) AS "i");

SELECT XMLQUERY('$i//@isbn' PASSING VALUE(b) AS "i" RETURNING CONTENT) AS isbnFROM book bWHERE CONTAINS(VALUE(b), 'Das book', 0) > 0ORDER BY SCORE(0) DESC;

Page 25: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

Manipulation

Persistent updates via SQL UPDATE: XQuery Update Facility supported!

Example:

1-25Lecture "XML and Databases" - Dr. Can Türker

<book isbn="3-89864-219-4" year="2003"> ...<publisher city="Zürich">dpunkt-Verlag</publisher> ... </book>

<book isbn="3-8266-00258-7" year="1997"> ... <publisher city="Bonn">International Thomson Publishing</publisher> ... </book>

<book isbn="3-89864-148-1" year="2003"> ...<publisher city="Zürich">dpunkt-Verlag</publisher> ... </book>

book SYS_NC_ROWINFO$

<book isbn="3-89864-228-3" year="2003"> ...<publisher city="Zürich">dpunkt-Verlag</publisher> ... </book>

UPDATE book bSET VALUE(b) = XMLQuery('copy $t := $i modify ( (for $p in $t//publisher[text()="dpunkt"]/@city return replace value of node $p with $city),(for $p in $t//publisher[text()="dpunkt"] return replace value of node $p with $name),delete node $t//book[@isbn="3-89864-148-1"]/author[text()="Holger Meyer"]) return $t' PASSING VALUE(b) AS "i", 'Zürich' AS "city", 'dpunkt-Verlag' AS "name“ RETURNING CONTENT);

Page 26: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-26Lecture "XML and Databases" - Dr. Can Türker

XML Views

Based on the concept of object views using XMLTYPE as object type

Example: CREATE VIEW dpunktbook OF XMLTYPE WITH OBJECT ID DEFAULT AS

SELECT VALUE(b)

FROM book b

WHERE XMLEXISTS('$i//publisher[text()="dpunkt"]' PASSING VALUE(b) AS "i");

Page 27: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-27Lecture "XML and Databases" - Dr. Can Türker

Export SQL Data to XML

Standard SQL-XML mapping using

– Table content mapped to <ROWSET> elements

– Column values of each row are mapped to child elements of a <row> element

– Structured (complex) columns are mapped to elements with corresponding child (hierarchically nested) elements

– Collections mapped to list of elements

– Object reference and foreign keys mapped to ID/IDREFs

User-defined transformation from SQL to XML possible via XSLT

<ROWSET>

<ROW num="1"> … </ROW> … <ROW num="n"> … </ROW>

</ROWSET>

DBMS_XMLGEN.getXML('query')

Page 28: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-28Lecture "XML and Databases" - Dr. Can Türker

Summary: Oracle

XML Storage

Model extensional, object-relational

Schema validation possible

Storage Type binary, text-based, schema-based

SQL-XML Mapping Using SQL/XML functions, schema generators, XML views

XML Data Type available

XML Indexing

Value/Functional Index available

Full-text Index available

Path Index available (implicitly as part of XML indexes)

Queries and Manipulation

Queries SQL/XML with XQuery support

Full-text Search using Intermedia-Text package

Manipulation XQuery Update Facility supported

Page 29: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-29Lecture "XML and Databases" - Dr. Can Türker

IBM DB2

Database

File-system

File-system

File-system

FileSystem

DB2

XML DocumentsApplication

Page 30: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-30Lecture "XML and Databases" - Dr. Can Türker

Mapping XML to Databases

After IBM has deprecated the DB2 XML Extender, DB2 supports only an XML column approach

Table with column of type XML

– Binary/text-based storage

– XML schema validation possible

Page 31: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-31Lecture "XML and Databases" - Dr. Can Türker

Table with XML Column

Register XML schema

Create table with XML column

Load XML documents from a file

REGISTER XMLSCHEMA 'book.xsd' FROM 'file://C:\XMLDIR\book.xsd' AS book.xsd COMPLETE

IMPORT FROM 'C:\XMLDIR\book.del' OF DEL XML FROM C:\XMLDIRXMLVALIDATE USING XDS DEFAULT book.xsdINSERT INTO book

CREATE TABLE book (Id INT PRIMARY KEY,content XML

)

1,<XDS FIL='book1.xml'/> 2,<XDS FIL='book2.xml'/> 3,<XDS FIL='book3.xml'/> 4,<XDS FIL='book4.xml'/>

File book.del

Page 32: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-32Lecture "XML and Databases" - Dr. Can Türker

Queries

Use XQuery in SQL via SQL/XML functions

– XMLQUERY, XMLTABLE, XMLEXISTS, XMLCAST

– XMLELEMENT, XMLFOREST, XMLCONCAT, XMLAGG

– XMLPARSE, XMLSERIALIZE, XMLCOMMENT, XMLPI

– XMLTEXT, XMLDOCUMENT, XMLVALIDATE, XSLTRANSFORM

– XMLROW, XMLGROUP

Use SQL in XQuery

– XQUERY db2-fn:xmlcolumn ('t1.xml1')

yields value of column xml1 of table t1 as a node sequence provided the column is of type XML

– XQUERY db2-fn:sqlquery ('SELECT xml1 FROM t1')

yields value of the column xml1 of table t1 as a node sequence provided the column is of type XML

Full-text search requires installation and activation of DB2 Text Extender

Page 33: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

creates XML value from an XQuery expression. Difference to Oracle: resulting XML value is never NULL; an empty sequence is delivered as such!

Example:

7-33Lecture "XML and Databases" - Dr. Can Türker

<book year="2003"><title>Datenbanken & Java</title><authors><author>Gunter Saake</author><author>Kai-Uwe Sattler</author></authors></book>

BOOK

<book year="1997"><title>Objektdatenbanken</title><authors><author>Gunter Saake</author> <author>Ingo Schmitt</author><author>Can Türker</author></authors></book>

XMLQUERY

XMLQUERY(<xquery-expression> [PASSING [BY REF] <xquery-expression-list>][RETURNING SEQUENCE [BY REF] [EMPTY ON EMPTY])

<xquery-argument> := <sql-value-expression> AS "<xquery-variable-name>"

SELECT XMLQUERY('<book year="{$i//@year}"><title>{$i//title/text()}</title><authors>{for $a in $i//author return <author>{$a/text()}</author>}</authors></book>'

PASSING content AS "i") AS bookFROM book bWHERE XMLEXISTS ('$i/book[author[1] = "Gunter Saake"]' PASSING content AS "i");

Page 34: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-34Lecture "XML and Databases" - Dr. Can Türker

XMLTABLE

creates SQL table from an XQuery expression. Without the COLUMNS option, a table with an XML column is created. Difference to Oracle: XMLQUERY und XMLTABLE can receive the XML input via reference.

Example:

XMLTABLE ([<namespace-clause>] <xquery-expression> [PASSING [BY REF] < xquery-argument-list>] [COLUMNS <xml-table-column-list>])

<xml-table-column> := <column-name> <column-type> [BY REF] [PATH <xpath-expression>] [DEFAULT<value-expression>] }

3-8266-0258-7

3-89864-219-4

3-89864-148-1

ISBN

3-89864-228-3

Objektdatenbanken

SQL:1999 & SQL:2003

XML & Datenbanken

BOOKTITLE

Datenbanken & Java

SELECT t.*FROM book, XMLTABLE('$i' PASSING content AS "i" COLUMNS

isbn VARCHAR(30) PATH '/book/@isbn', booktitle VARCHAR(50) PATH '/book/title') AS t;

Page 35: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

return true if the XQuery expression yields a non-null value

Example:

XMLEXISTS(<xquery-expression> [PASSING [BY REF] <xquery-expression-list>])

<xquery-argument> := <sql-value-expression> AS "<xquery-variable-name>"

7-35Lecture "XML and Databases" - Dr. Can Türker

XMLEXISTS

Objektdatenbanken

Datenbanken &amp; Java

BOOKTITLE

SELECT XMLQuery('$i//title/text()' PASSING content AS "i") AS booktitleFROM bookWHERE XMLEXISTS('$i//author[text() = "Gunter Saake"]' PASSING content AS "i");

Page 36: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-36Lecture "XML and Databases" - Dr. Can Türker

XMLCAST

Difference to Oracle: SQL data type XML can be used here as target type!

Example:

XMLCAST(<xml-value-expression> AS <sql-data-type> )

Objektdatenbanken

SQL:1999 & SQL:2003

XML & Datenbanken

BOOKTITLE

Datenbanken & Java

SELECT XMLCAST(XMLQUERY('$i//book/title' PASSING content AS "i") AS CLOB) AS booktitleFROM book;

Page 37: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-37Lecture "XML and Databases" - Dr. Can Türker

XMLELEMENT

creates XML-Element (XMLBINARY determines the encoding of the input)

Example:

<worker SALARY="2000">Joe</worker>

<worker SALARY="3500">Jim</worker>

WORKER

Joe

Jim

2000

3500

name salaryemployee SELECT XMLELEMENT(NAME "worker", XMLATTRIBUTES(salary), name) AS worker

FROM employee;

XMLELEMENT(NAME <element-name>[, XMLATTRIBUTES(<xml-attribute-list>)][, <value-expression-list>] [XML-content-option-list>])

<xml-attribute> := <value-expression> [AS <attribute-name>]

<xml-content-option> := {NULL | EMPTY} ON NULL | XMLBINARY [USING] {BASE64 | HEX}

Page 38: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-38Lecture "XML and Databases" - Dr. Can Türker

XMLFOREST

creates forest of XML elements

Example:

XMLFOREST(<xml-forest-element-list> [XML-content-option-list>])

<xml-forest-element>:=<value-expression> [AS <element-name>]

Joe

Jim

2000

3500

name salaryemployee SELECT XMLFOREST(name, XMLELEMENT(NAME "euro", 12*salary) AS income) AS worker

FROM employee;

<NAME>Joe</NAME><INCOME><euro>24000</euro></INCOME>

<NAME>Jim</NAME><INCOME><euro>42000</euro></ INCOME>

WORKER

Page 39: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-39Lecture "XML and Databases" - Dr. Can Türker

XMLCONCAT

concatenates XML elements to a forest

Example:

XMLCONCAT(<xml-value-expression-list>)

Joe

Jim

2000

3500

name salaryemployee

<employee>Joe</employee><euro>24000</euro>

<employee>Jim</employee><euro>42000</euro>

WORKER

SELECT XMLCONCAT(XMLELEMENT(NAME "employee", name), XMLELEMENT(NAME "euro", 12*salary))

AS workerFROM employee;

Page 40: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-40Lecture "XML and Databases" - Dr. Can Türker

XMLAGG

aggregates XML elements

Example:

XMLAGG(<xml-value-expression>[ORDER BY <order-expression-list>] )

SELECT name, XMLAGG(XMLELEMENT(NAME "salary", salary))AS income

FROM employeeGROUP BY name;

Joe

Jim

2000

3500

name salaryemployee

Jim 5000

<salary>2000</salary>

<salary>3500</salary><salary>5000</salary>

Joe

Jim

NAME INCOME

Page 41: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-41Lecture "XML and Databases" - Dr. Can Türker

XMLPARSE, XMLSERIALIZE, XMLCOMMENT, XMLPI

creates XML value. Default option STRIP WHITESPACE eliminates spaces from text nodes which consist only out of spaces

converts XML value to an SQL string. Option INCLUDING XMLDECLARATION additionally inserts the XML declaration <?xml version="1.0" encoding="UTF-8"?>. Default: EXCLUDING.

creates XML comment node

creates XML processing instruction node

XMLCOMMENT (<string-value-expression>)

XMLPI (NAME <Identifier> [, <string-value-expression>]))

XMLPARSE (DOCUMENT <string-value-expression> [{STRIP|PRESERVE} WHITESPACE])

XMLSERIALIZE([CONTENT] <xml-value-expression> [AS <string-data-type>] [VERSION ˈ1.0ˈ] [{EXCLUDING|INLUDING} XMLDECLARATION])

Page 42: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-42Lecture "XML and Databases" - Dr. Can Türker

XMLTEXT, XMLDOCUMENT, XMLVALIDATE, XSLTRANSFORM

creates XML text node

creates XML document node

returns copy of XML value enriched by information from the schema validation, including the default values

transforms XML value using an XSL style sheet

XMLTEXT(<string-value-expression>)

XMLDOCUMENT(<xml-value-expression)

XMLVALIDATE([DOCUMENT] <xml-value-expression>) [ACCORDING TO XMLSCHEMA {ID <xml-schema-name> | {URI <xml-uri1> | NO NAMESPACE} [LOCATION <xml-uri2>]}[[NAMESPACE <xml-uri3> | NO NAMESPACE] ELEMENT <xml-element-name>]])

XSLTRANSFORM(<xml-value-expression> USING <xsl-style-sheet> [WITH <xsl-parameter-list>] [AS <string-data-type>])

Page 43: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-43Lecture "XML and Databases" - Dr. Can Türker

XMLROW

returns XML document node <row> where each value expression is attached as a child node. Option AS ATTRIBUTES creates attribute nodes instead of child nodes. Option ROW can be used to rename the document element.

XMLROW(<row-value-expression-list> [OPTION <option-list>]

<row-value-expression-list> := <sql-value-expression> [AS "<qname-identifier>"]

<option> := ROW "<qname-identifier>" | AS ATTRIBUTES

SELECT name, XMLROW(name, salary) AS incomeFROM employee;

Joe

Jim

2000

3500

name salaryemployee

<row><NAME>Joe</NAME><SALARY>2000</SALARY></row>

<row><NAME>Jim</NAME><SALARY>3500</SALARY></row>

Joe

Jim

NAME INCOME

Page 44: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-44Lecture "XML and Databases" - Dr. Can Türker

XMLGROUP

returns XML document element node <rowset> with child element nodes <row> for each expression. Option AS ATTRIBUTES creates attribute nodes instead of <row> element nodes. Option ROWSET can be used to rename the document element node.

XMLROW(<row-value-expression-list> [ORDER BY <order-expression>] [OPTION <Option-list>]

<row-value-expression-list> := <sql-value-expression> [AS "<qname-identifier>"]

<Option> := ROOT "<qname-identifier>" | ROW "<qname-identifier>" | AS ATTRIBUTES

SELECT name, XMLGROUP(salary) AS incomeFROM employeeGROUP BY name;

Joe

Jim

2000

3500

name salaryemployee

Jim 5000

<rowset><row><SALARY>2000</SALARY></row></rowset>

<rowset><row><SALARY>3500</SALARY></row><row><SALARY>5000</SALARY></row></rowset>

Joe

Jim

NAME INCOME

Page 45: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-45Lecture "XML and Databases" - Dr. Can Türker

XQuery returns single-column table with a row for each item of the resulting sequence

XMLQuery returns table with a (possibly empty) sequence for each table row

XQuery vs. XMLQuery

<authors><author>Gunter Saake</author><author>Kai-Uwe Sattler</author></authors>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Meike Klettke</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Holger Meyer</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Can Türker</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Gunter Saake</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Kai-Uwe Sattler</author>

<authors><author>Gunter Saake</author><author>Kai-Uwe Sattler</author></authors>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Meike Klettke</author><author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Holger Meyer</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Can Türker</author>

<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Gunter Saake</author><author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Kai-Uwe Sattler</author>

XQUERY db2-fn:sqlquery('SELECT content FROM book')//book[@year > 2000]/author;

SELECT XMLQUERY('$i//book[@year > 2000]/author' PASSING content as "i") FROM book;

Page 46: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

Index Types

– XML index

– Full-text index

Query using the XML index

Query using (and requiring) the full-text index

CREATE INDEX xmlbooktitleidx ON book (content) GENERATE KEY USING XMLPATTERN '/book/title' AS SQL VARCHAR(50);

11-46Lecture "XML and Databases" - Dr. Can Türker

Indexing

CREATE INDEX xmlfulltextidx FOR TEXT ON book(content);

SELECT XMLQUERY('$i/book/title' PASSING content AS "i") AS booktitleFROM bookWHERE XMLEXISTS('$i/book/title' PASSING content AS "i");

SELECT id, INTEGER(SCORE(content, '(XML OR book)')*1000) AS score FROM book WHERE CONTAINS(content, '(XML OR book)')=1ORDER BY score DESC;

Page 47: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-47

Persistent updates via SQL UPDATE: XQuery Update Facility supported!

Manipulation

<book isbn="3-89864-148-1" year="2003"><author>Jim Bob</author> <author>Meike Klettke</author><author>Holger Meyer</author><title>XML & Datenbanken</title><subtitle>

XML-Dokumente effizient speichernund verarbeiten

</subtitle><publisher city="Heidelberg">dpunkt</publisher><description>Mit der wachsenden ...</description>

</book>

UPDATE book SET content = XMLQUERY('transform copy $pi := $i modify do

insert document { <author>Jim Bob</author> } before $pi//book/author[1] return $pi' PASSING content AS "i")

WHERE XMLEXISTS('$i/book[author/text() = "Holger Meyer"]' PASSING content AS "i");

Page 48: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-48Lecture "XML and Databases" - Dr. Can Türker

Summary: IBM DB2

XML Storage

Model extensible, object-relational

Schema Validation possible

Storage Type binary/text-based (XML stored as LOBs)

SQL-XML Mapping XQUERY + XMLQUERY

XML Data Type available

Indexing

Value Index available (as part of the XML index)

Full-text Index using Text Extender*

Path Index available (as part of the XML index)

Queries and Manipulation

Queries SQL/XML with XQuery support

Full-text Search using Text Extender*

Manipulation XQuery Update Facility supported

*Text Extender must explicitly be installed and activated!

Page 49: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-49Lecture "XML and Databases" - Dr. Can Türker

Microsoft SQL Server - Architecture

Database

MS SQL Server

XML DocumentsApplication

Page 50: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-50Lecture "XML and Databases" - Dr. Can Türker

Mapping XML to Databases

Storage Types

– Native (binary) storage

– Text-based storage as CLOB

– Model-based storage using the EDGE approach (extended SQL)

– Schema-based storage using STORED queries (extended SQL)

Data type XML with XQuery methods

– Query() – evaluates XQuery and returns a value of type XML

– Value() – evaluates XQuery and returns a scalar SQL value

– Exist() – returns true if the XQuery yields a non-empty result

– Modify() – updates a value of type XML

Integrated usage of SQL and XQuery

– Access to SQL data in XQuery using the functions sql:column("columnname") and sql:variable("variablename"), respectively

– Evaluation of XQuery in SQL using XML methods above

Page 51: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-51Lecture "XML and Databases" - Dr. Can Türker

Native Storage – Table Definition

Register XML schema

Create table with XML column

Insert XML document from file

CREATE XML SCHEMA COLLECTION bookXSD AS '<?xml version="1.0"?>…'

INSERT INTO bookSELECT 1, xColFROM (SELECT *

FROM OPENROWSET (BULK 'C:\XMLDIR\book1.xml', SINGLE_BLOB) AS xCol) AS R(xCol)

CREATE TABLE book (Id INT PRIMARY KEY,content XML (bookXSD)

)

Page 52: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-52Lecture "XML and Databases" - Dr. Can Türker

Native Storage - Indexing

Create primary XML index

– creates clustered index with entries of the form (ID, ORDPATH, TAG, NODETYPE, VALUE, PATH_ID, …)

– needed to created secondary XML indexes

Secondary XML index types:

– Path index (Path, Value)

– Property index (Primary key , Path, Value)

– Value index (Value, Path)

Secondary XML (path plus property) indexes

Full-text index

CREATE PRIMARY XML INDEX contentidx ON book (content)

CREATE XML INDEX contentpathidx ON book (content) USING XML INDEX contentidx FOR PATH CREATE XML INDEX contentpropertyidx ON book (content) USING XML INDEX contentidx FOR PROPERTY

PATH | PROPERTY | VALUE

CREATE UNIQUE INDEX bookkeyidx ON book (Id)CREATE FULLTEXT CATALOG xmldbftcat AS DEFAULTCREATE FULLTEXT INDEX ON book (content) KEY INDEX bookkeyidx

Page 53: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-53Lecture "XML and Databases" - Dr. Can Türker

Example: XQuery using SQL data to create new XML values

Native Storage – Queries (1)

SELECT content.query('<book id="{sql:column("id")}" year="{//@year}"><title>{//title}</title><authors>{for $a in //author return <author>{$a}</author>}</authors></book>') AS book

FROM bookWHERE content.exist('/book[author[1] = "Gunter Saake"]') = 1

<book id="2" year="1997"><title>Objektdatenbanken</title><authors><author>Gunter Saake</author><author>Ingo Schmitt</author><author>Can Türker</author></authors></book>

<book id="4" year="2003"><title>Datenbanken &amp; Java</title><authors><author>Gunter Saake</author><author>Kai-Uwe Sattler</author></authors></book>

book

Page 54: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-54Lecture "XML and Databases" - Dr. Can Türker

Example: Query using the XML index

Example: Query using the full-text index

Native Storage – Queries (2)

SELECT content.query('//author') AS authorsFROM bookWHERE content.exist('/book[author[1] = "Gunter Saake"]') = 1

<author>Gunter Saake</author><author>Kai-Uwe Sattler</author>

<author>Gunter Saake</author><author>Ingo Schmitt</author><author>Can Türker</author>

authors

SELECT id, contentFROM book WHERE CONTAINS(content, '(XML OR book)')

Page 55: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-55Lecture "XML and Databases" - Dr. Can Türker

Example: insert new element

Example: update attribute value

Native Storage – Manipulation

UPDATE bookSET content.modify('replace value of (//publisher[. = "dpunkt"]/@city)[1] with "Zürich"')

UPDATE bookSET content.modify('insert <author>Unknown</author> before (//author)[1]')

Page 56: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-56Lecture "XML and Databases" - Dr. Can Türker

Model-based Storage using EDGE (1)

SQL extension OPENXML transforms XML values to SQL tables

OPENXML without the WITH clause returns an EDGE table (result see next foil)

DECLARE @xmldoctext VARCHAR(8000) = '<?xml version="1.0" encoding="ISO-8859-1"?> <book isbn="3-89864-148-1" year="2003"><author>Meike Klettke</author><author>Holger Meyer</author><title>XML &amp; Datenbanken</title><subtitle>XML-Dokumente effizient speichern und verarbeiten</subtitle><publisher city="Heidelberg">dpunkt</publisher> <description>...</description></book>'

DECLARE @hdoc INT

EXEC sp_xml_preparedocument @hdoc OUTPUT, @xmldoctext

SELECT * FROM OPENXML(@hdoc, '', 0)

EXEC sp_xml_removedocument @hdoc

Page 57: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-57Lecture "XML and Databases" - Dr. Can Türker

Model-based Storage using EDGE (2)

BIGINT BIGINT INT NVARCHAR NVARCHAR NVARCHAR NVARCHAR BIGINT NTEXT

id parentid nodetype localname prefix namespaceuri datatype prev text

0 NULL 1 book NULL NULL NULL 1 NULL1 NULL 7 xml NULL NULL NULL NULL NULL2 1 2 version NULL NULL NULL NULL NULL3 1 2 encoding NULL NULL NULL NULL NULL5 0 2 isbn NULL NULL NULL NULL NULL6 0 2 year NULL NULL NULL NULL NULL7 0 1 author NULL NULL NULL NULL NULL8 0 1 author NULL NULL NULL 7 NULL9 0 1 title NULL NULL NULL 8 NULL10 0 1 subtitle NULL NULL NULL 9 NULL11 0 1 publisher NULL NULL NULL 10 NULL12 11 2 city NULL NULL NULL NULL NULL13 11 3 #text NULL NULL NULL NULL dpunkt14 0 1 description NULL NULL NULL 11 NULL15 2 3 #text NULL NULL NULL NULL 116 3 3 #text NULL NULL NULL NULL ISO-8859-117 5 3 #text NULL NULL NULL NULL 3-89864-148-118 6 3 #text NULL NULL NULL NULL 200319 7 3 #text NULL NULL NULL NULL Meike Klettke20 8 3 #text NULL NULL NULL NULL Holger Meyer21 9 3 #text NULL NULL NULL NULL XML & Datenbanken22 10 3 #text NULL NULL NULL NULL XML-Dokumente ...23 12 3 #text NULL NULL NULL NULL Heidelberg24 14 3 #text NULL NULL NULL NULL ...

Page 58: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-58Lecture "XML and Databases" - Dr. Can Türker

Schema-based Storage using STORED Queries (1)

OPENXML supports custom mappings

OPENXML with the WITH clause creates user-defined table (result see next foil)

DECLARE @xmldoctext VARCHAR(8000) = '<?xml version="1.0" encoding="ISO-8859-1"?> <book isbn="3-89864-148-1" year="2003"><author>Meike Klettke</author><author>Holger Meyer</author><title>XML &amp; Datenbanken</title><subtitle>XML-Dokumente effizient speichern und verarbeiten</subtitle><publisher city="Heidelberg">dpunkt</publisher> <description>...</description></book>'

DECLARE @hdoc INT

EXEC sp_xml_preparedocument @hdoc OUTPUT, @xmldoctext

SELECT * FROM OpenXML(@hdoc, '//book', 0) WITH (title NVARCHAR(3000) './title',isbn NVARCHAR(15) './@isbn',publisher NVARCHAR(200) './publisher')

SELECT * FROM OpenXML(@hdoc, '//author', 0) WITH (name NVARCHAR(200) '.',isbn NVARCHAR(200) '../@isbn')

EXEC sp_xml_removedocument @hdoc

Page 59: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-59Lecture "XML and Databases" - Dr. Can Türker

Schema-based Storage using STORED Queries (2)

Resulting table "book"

Resulting table "author"

title isbn publisher

XML & Datenbanken 3-89864-148-1 dpunkt

name isbn

Meike Klettke 3-89864-148-1

Holger Meyer 3-89864-148-1

Page 60: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-60Lecture "XML and Databases" - Dr. Can Türker

Mapping Databases to XML

Variant 1: Standard mapping using SQL SELECT and FOR XML

– FOR XML AUTO: Generates XML element names and reflects foreign keys in hierarchies

– FOR XML RAW: Maps to ROW-XML elements und XML attributes

– FOR XML PATH: Generates complex XML elements using XPath

– FOR XML EXPLICIT: User controls XML mapping (EDGE)

Variant 2: User-defined XML view

– Use XML schema

– Annotate the schema with information over tables and columns

– Access XML view via IIS functionality or ADO (ActiveX Data Objects)

Page 61: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-61Lecture "XML and Databases" - Dr. Can Türker

FOR XML AUTO

SELECT name, title, publisher, book.isbnFROM author JOIN book ON (author.isbn = book.isbn)FOR XML AUTO

<author name="Meike Klettke"><book title="XML &amp; Datenbanken" publisher="dpunkt" isbn="3-89864-148-1" /></author><author name="Holger Meyer"><book title="XML &amp; Datenbanken" publisher="dpunkt" isbn="3-89864-148-1" /></author>

XML_XXX: NTEXT (CLOB)

SELECT name, title, publisher, book.isbnFROM author JOIN book ON (author.isbn = book.isbn)FOR XML AUTO, ELEMENTS

<author><name>Meike Klettke</name><book><title>XML &amp; Datenbanken</title> <publisher>dpunkt</publisher><isbn>3-89864-148-1</isbn></book></author><author><name>Holger Meyer</name><book><title>XML &amp; Datenbanken</title> <publisher>dpunkt</publisher><isbn>3-89864-148-1</isbn></book></author>

XML_XXX: NTEXT (CLOB)

Option XMLSCHEMA ('uri') generates XML Schema

Page 62: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-62Lecture "XML and Databases" - Dr. Can Türker

FOR XML RAW

SELECT name, title, publisher, book.isbnFROM author JOIN book ON (author.isbn = book.isbn)FOR XML RAW

<row name="Meike Klettke" title="XML &amp; Datenbanken" publisher="dpunkt" isbn="3-89864-148-1" /><row name="Holger Meyer" title="XML &amp; Datenbanken" publisher="dpunkt" isbn="3-89864-148-1" />

XML_XXX: NTEXT (CLOB)

SELECT name, title, publisher, book.isbnFROM author JOIN book ON (author.isbn = book.isbn)FOR XML AUTO, ELEMENTS

<row><name>Meike Klettke</name><title>XML &amp; Datenbanken</title> <publisher>dpunkt</publisher><isbn>3-89864-148-1</isbn></row><row><name>Holger Meyer</name><title>XML &amp; Datenbanken</title><publisher>dpunkt</publisher><isbn>3-89864-148-1</isbn></row>

XML_XXX: NTEXT (CLOB)

Page 63: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-63Lecture "XML and Databases" - Dr. Can Türker

FOR XML PATH

SELECT book.isbn AS "@isbn", name AS "author", publisher AS "title/@publisher", title AS "title/text()"FROM author JOIN book ON (author.isbn = book.isbn)FOR XML PATH

<row isbn="3-89864-148-1"><author>Meike Klettke</author><title publisher="dpunkt">XML &amp; Datenbanken</title></row><row isbn="3-89864-148-1"><author>Holger Meyer</author><title publisher="dpunkt">XML &amp; Datenbanken</title></row>

XML_XXXX: NTEXT (CLOB)

SELECT book.isbn AS "@isbn", name AS "author", publisher AS "title/@publisher", title AS "title/text()"FROM author JOIN book ON (author.isbn = book.isbn)FOR XML PATH ('books'), ROOT ('bookstore')

<bookstore><books isbn="3-89864-148-1"><author>Meike Klettke</author><title publisher="dpunkt">XML &amp; Datenbanken</title></books><books isbn="3-89864-148-1"><author>Holger Meyer</author><title publisher="dpunkt">XML &amp; Datenbanken</title></books></bookstore>

XML_XXXX: NTEXT (CLOB)

Page 64: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-64Lecture "XML and Databases" - Dr. Can Türker

User-Defined XML Views

Annotated XML schema

– Namespace: xmlns:sql="urn:schemas-microsoft-com:mapping-schema"

– Annotation sql:relation: Relationship between XML and a table

– Annotation sql:field: Relationship between XML and a table column

– Annotation sql:relationship: Foreign key relationship

XML view access via XPath

– Some XPath construct are not supported

– XPath transformed to corresponding SQL

Page 65: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-65Lecture "XML and Databases" - Dr. Can Türker

User-Defined XML Views (Example)

Book Publisher Title isbn

Thomson Objektdatenbanken 3-8266-0258-7

<book><title> Objektdatenbanken </title><isbn>3-8266-00258-7 </isbn><publisher> Thomson </publisher>

</book>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema"> <xsd:element name= "book" sql:relation= "book" type= "booktype"/> <xsd:complexType name= "booktype" >

<xsd:sequence> <xsd:element name= "title" sql:field= "Title"/><xsd:element name= "isbn" sql:field= "isbn"/><xsd:element name= "publisher" sql:field= "Publisher"/>

</xsd:sequence> </xsd:complexType> </xsd:schema>

Definition ofXML structure

Definition of table columns

Page 66: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-66Lecture "XML and Databases" - Dr. Can Türker

Manipulation using Updategrams

Schema-based mapping required

– Updates formulated in an XML document

– New namespace: xmlns:updg="urn:schemas-microsoft-com:xml-updategram" Element before: Definition of the state to be updated

Element after: Definition of the new state

– Update operations Insert: empty <before> element

Delete: empty <after> element

Update: both elements are non-empty

Example: <ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram"> <updg:sync>

<updg:before> <publisher>Thomson</publisher>

</updg:before> <updg:after>

<publisher>International Thomson Publishing</publisher></updg:after>

</updg:sync> </ROOT>

Page 67: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-67Lecture "XML and Databases" - Dr. Can Türker

Summary: SQL Server

XML Storage

Model relational

Schema inline DTD or XML schema

Storage Type native: XML column text-based: CLOB columnmodel-based: OPENXMLuser-defined, schema-based: OPENXML

SQL-XML Mapping automatic: FOR XMLuser-defined: XSD annotations

XML Data Type available

Indexing

Value Index available

Full-text Index available (if Fulltext Search Feature is installed)

Path Index available

Queries and Manipulation

Queries SQL extensions (query und value are not SQL/XML compatible), XQuery

Full-text Search supported

Manipulation No XQuery Update Facility support but specific XML method modify as well as XML updategram

Page 68: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-68Lecture "XML and Databases" - Dr. Can Türker

PostgreSQL - Architecture

Database

PostgreSQL Server

XML DocumentsApplication

Page 69: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-69Lecture "XML and Databases" - Dr. Can Türker

Storage and Queries

XML data type

– Text-based storage

– No schema validation

SQL/XML functions

– XMLEXISTS, XMLELEMENT, XMLFOREST, XMLCONCAT, XMLAGG

– XMLPARSE, XMLSERIALIZE, XMLCOMMENT, XMLPI

– Plus XMLROOT

– No support for XMLQUERY und XMTABLE

– XPath instead XQUERY support

Full-text search

Page 70: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-70Lecture "XML and Databases" - Dr. Can Türker

XML

creates XML value

Example:

XML(<string-value-expression> )

Joe

Jim

2000

3500

name salaryemployee

SELECT XML('<employee name="' || name || '"><salary>' || 12 * salary || '</salary></employee>') AS worker

FROM employee;

<employee name="Joe"><salary>24000</salary></employee>

<employee name="Jim"><salary>42000</salary></employee>

worker

Page 71: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-71Lecture "XML and Databases" - Dr. Can Türker

XPATH (1)

extracts part(s) of an XML value using an XPath expression. Resulting XML sequence represented by a value of the SQL type SET OF.

Example:

XPATH(<xpath-expression>, <xml-value-expression>)

{"<author>Gunter Saake</author>"}

{"<author>Can Türker</author>"}

{"<author>Meike Klettke</author>"}

firstauthor

{"<author>Gunter Saake</author>"}

<author>Gunter Saake</author>

<author>Can Türker</author>

<author>Meike Klettke</author>

firstauthorunnested

<author>Meike Klettke</author>

SELECT XPATH('//author[1]', content) AS firstauthor, TEXT(UNNEST(XPATH('//author[1]', content))) AS firstauthorunnested

FROM book;

Page 72: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-72Lecture "XML and Databases" - Dr. Can Türker

XPATH (2)

<authors>{"<author>Meike Klettke</author>""<author>Holger Meyer</author>"

}</authors>

authors

<authors>{"<author>Gunter Saake</author>""<author>Ingo Schmitt</author>""<author>Can Türker</author>"}</authors>

<authors>{"<author>Can Türker</author>"}</authors>

<authors>{"<author>Gunter Saake</author> ""<author>Kai-Uwe Sattler</author>"}</authors>

SELECT XML('<authors>' || TEXT(XPATH('//author', content))|| '</authors>') AS authorsFROM book b;

Page 73: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-73Lecture "XML and Databases" - Dr. Can Türker

XPATH (3)

<authors><element><author>Meike Klettke</author></element><element><author>Holger Meyer</author></element>

</authors>

authors

<authors><element><author>Gunter Saake</author></element><element><author>Ingo Schmitt</author></element><element><author>Can Türker</author></element></authors>

<authors><element><author>Can Türker</author></element></authors>

<authors><element><author>Gunter Saake</author></element><element><author>Kai-Uwe Sattler</author></element></authors>

SELECT XMLELEMENT(NAME "authors", XPATH('//author', content)) AS authorsFROM book b;

Page 74: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-74Lecture "XML and Databases" - Dr. Can Türker

XMLTABLE

creates SQL table from an XPath expression. Columns option allows user-defined mapping to the table columns. Without this option, an XML table is created.

Example:

3-8266-0258-7

3-89864-219-4

3-89864-148-1

isbn

3-89864-228-3

Objektdatenbanken

SQL:1999 & SQL:2003

XML & Datenbanken

booktitle

Datenbanken & Java

SELECT t.*FROM book b, XMLTABLE('/book' PASSING content COLUMNS

isbn VARCHAR(30) PATH '@isbn', booktitle VARCHAR(50) PATH 'title') AS t;

XMLTABLE ([<namespace-clause>] <row-expression> PASSING [BY REF] <document-expression> PASSING [BY REF] COLUMNS <xml-table-column-list>])

<xml-table-column> := <column-name> <column-type> [PATH <xpath-expression>] [DEFAULT<value-expression>] [[NOT] NULL | FOR ORDINALITY]

Page 75: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

returns true if the XQuery expression yields a non-null value

Example:

XMLEXISTS(<xpath-expression> PASSING [BY REF] <xml-value-expression>)

7-75Lecture "XML and Databases" - Dr. Can Türker

XMLEXISTS

{Objektdatenbanken}

{Datenbanken &amp; Java}

booktitle

SELECT XPATH('//title/text()', content) AS booktitleFROM bookWHERE XMLEXISTS('//author[text() = "Gunter Saake"]' PASSING content);

Page 76: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-76Lecture "XML and Databases" - Dr. Can Türker

XMLELEMENT

creates XML element (XMLBINARY defines encoding)

Example:

<worker salary="2000">Joe</worker>

<worker salary="3500">Jim</worker>

worker

Joe

Jim

2000

3500

name salaryemployee SELECT XMLELEMENT(NAME "worker", XMLATTRIBUTES(salary), name) AS worker

FROM employee;

XMLELEMENT(NAME <element-name>[, XMLATTRIBUTES(<xml-attribute-list>)][, <value-expression-list>])

<xml-attribute> := <value-expression> [AS <attribute-name>]

Page 77: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-77Lecture "XML and Databases" - Dr. Can Türker

XMLFOREST

creates forest of XML elements

Example:

XMLFOREST(<xml-forest-element-list>)

<xml-forest-element>:=<value-expression> [AS <element-name>]

Joe

Jim

2000

3500

name salaryemployee SELECT XMLFOREST(name, XMLELEMENT(NAME "euro", 12*salary) AS income) AS worker

FROM employee;

<name>Joe</name><income><euro>24000</euro></income>

<name>Jim</name><income><euro>42000</euro></income>

worker

Page 78: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-78Lecture "XML and Databases" - Dr. Can Türker

XMLCONCAT

concatenates XML elements to a forest

Example:

XMLCONCAT(<xml-value-expression-list>)

Joe

Jim

2000

3500

name salaryemployee

<employee>Joe</employee><euro>24000</euro>

<employee>Jim</employee><euro>42000</euro>

worker

SELECT XMLCONCAT(XMLELEMENT(NAME "employee", name), XMLELEMENT(NAME "euro", 12*salary))

AS workerFROM employee;

Page 79: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-79Lecture "XML and Databases" - Dr. Can Türker

XMLAGG

aggregates XML elements

Example:

XMLAGG(<xml-value-expression>[ORDER BY <order-expression-list>] )

SELECT name, XMLAGG(XMLELEMENT(NAME "salary", salary))AS income

FROM employeeGROUP BY name;

Joe

Jim

2000

3500

name salaryemployee

Jim 5000

<salary>2000</salary>

<salary>3500</salary><salary>5000</salary>

Joe

Jim

name income

Page 80: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-80Lecture "XML and Databases" - Dr. Can Türker

XMLPARSE, XMLSERIALIZE, XMLCOMMENT, XMLPI

creates XML value

converts XML value to SQL string

creates XML comment node

creates XML processing instruction node

XMLCOMMENT (<string-value-expression>)

XMLPI (NAME <Identifier> [, <string-value-expression>]))

XMLPARSE ({DOCUMENT|CONTENT} <string-value-expression>)

XMLSERIALIZE({DOCUMENT|CONTENT} <xml-value-expression> AS <string-data-type>)

Page 81: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

XMLROOT(<xml-value-expression>, VERSION {<number-text>|NO VALUE} [, STANDALONE {YES|NO [VALUE]}])

7-81Lecture "XML and Databases" - Dr. Can Türker

XMLROOT

creates XML node (if available XML attribute version and standalone are set too)

returns true if the xml-value-expression yields a proper XML document

<xml-value-expression> IS DOCUMENT

Page 82: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-82Lecture "XML and Databases" - Dr. Can Türker

Indexing

Index support

– Functional Index

(Value index)

– Full-text index

Query using the functional index

Query using full-text index

CREATE INDEX xmlfunctionalidx ON book USING BTREE (((XPATH('//author/text()', content))[1]::TEXT));

SELECT * FROM bookWHERE (XPATH('//author/text()', content))[1]::TEXT = 'Gunter Saake';

CREATE INDEX xmlfulltextidx ON book USING GIN(to_tsvector('german', content::TEXT));

SELECT *FROM bookWHERE to_tsvector(content::TEXT) @@ plainto_tsquery('german','Gunter');

Page 83: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-83Lecture "XML and Databases" - Dr. Can Türker

Export Database to XML

Standard mapping of SQL to XML using the following functions

Variant (xxx for table, query, schema, database):

– xxx_to_xmlschema returns corresponding XML schema

– xxx_to_xml_and_xmlschema returns XML value and corresponding XML schema

table_to_xml(tablename text, nulls boolean, tableforest boolean, targetns text)

query_to_xml(query text, nulls boolean, tableforest boolean, targetns text)

schema_to_xml(schemaname text, nulls boolean, tableforest boolean, targetns text)

database_to_xml(nulls boolean, tableforest boolean, targetns text)

Page 84: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-84Lecture "XML and Databases" - Dr. Can Türker

TABLE_TO_XML (1)

exports table to XML

1) null values are mapped to empty XML elements or are ignored

2) document: false maps row to XML sequence; true to an XML document element

3) optional namespace

Example:

TABLE_TO_XML(<table-name>, <null-value>, <document>, <namespace>)

<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><name>Joe</name><salary>2000.00</salary>

</employee><employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><name>Jim</name><salary>3500.00</salary>

</employee>

table_to_xml

SELECT TABLE_TO_XML('employee', true, true, '');

Page 85: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-85Lecture "XML and Databases" - Dr. Can Türker

TABLE_TO_XML (2)

Example:

<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><row>

<name>Joe</name><salary>2000.00</salary>

</row><row>

<name>Jim</name><salary>3500.00</salary>

</row></employee>

table_to_xml

SELECT TABLE_TO_XML('employee', true, false, '');

Page 86: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-86Lecture "XML and Databases" - Dr. Can Türker

TABLE_TO_XMLSCHEMA

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:simpleType name="VARCHAR">

<xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType> <xsd:simpleType name="NUMERIC"></xsd:simpleType><xsd:complexType name="RowType.postgres.public.employee">

<xsd:sequence><xsd:element name="name" type="VARCHAR" nillable="true"></xsd:element> <xsd:element name="salary" type="NUMERIC" nillable="true"></xsd:element>

</xsd:sequence></xsd:complexType><xsd:complexType name="TableType.postgres.public.employee">

<xsd:sequence> <xsd:element name="row" type="RowType.postgres.public.employee"

minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence>

</xsd:complexType><xsd:element name="employee" type="TableType.postgres.public.employee"/>

</xsd:schema>

table_to_xmlschema

SELECT TABLE_TO_XMLSCHEMA('employee', true, false, '');

Page 87: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-87Lecture "XML and Databases" - Dr. Can Türker

QUERY_TO_XML

Example:

<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><row>

<name>Joe</name><annualsalary>24000.00</annualsalary>

</row><row>

<name>Jim</name><annualsalary>42000.00</annualsalary>

</row></table>

query_to_xml

SELECT QUERY_TO_XML('SELECT name, 12*salary AS annualsalary FROM employee', true, false, '')

Page 88: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

11-88Lecture "XML and Databases" - Dr. Can Türker

Summary: PostgreSQL

XML Storage

Model extensible, object-relational

Schema not supported

Storage Type text-based

SQL-XML Mapping using SQL/XML functions

XML Data Type available

Indexing

Value Index available

Full-text Index using tsearch package

Path Index not supported

Queries and Manipulation

Queries SQL/XML with XPath and XMLTABLE but no XQUERY support

Full-text Search using tsearch package

Manipulation No XQuery Update Facility support

Page 89: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

4-89Lecture "XML and Databases" - Dr. Can Türker

Comparison – SQL/XML FunctionsTyp SQL:2003 Oracle DB2 MSSQL PostgreSQL

XML (XMLTYPE)

XMLPARSE — (CAST)

XMLSERIALIZE — (CAST)

XMLCOMMENT —

XMLPI —

XMLDOCUMENT — — —

XMLTEXT — — —

XMLVALIDATE — — —

XMLQUERY — (query) — (XPATH)

XMLTABLE —

XMLEXISTS — (exist)

XMLCAST — (CAST) —

XMLELEMENT —

XMLFOREST —

XMLCONCAT —

XMLAGG —

XMLROOT — — —

XMLCDATA — — — —

XMLCOLATTVAL — — — —

XMLROW — — — —

XMLGROUP — — — —

FORXML — — — —

OPENXML — — — —

Page 90: New XML und Datenbankenbfc5dd38-320a-4b3c-b082... · 2019. 5. 20. · Lecture "XML and Databases" - Dr. Can Türker 11-7 Mapping XML to Databases Approaches – XML column approach:

7-90Lecture "XML and Databases" - Dr. Can Türker

Conclusions

We have seen that most of the theoretical concepts discussed in this lecture are supported in at least one of the leading database systems

– None system is providing all features

– Each system provides its favorite storage models

SQL/XML is supported largely by the leading database vendors

– Even with some extensions regarding XML storage and indexing

Choose the “appropriate” database system for your application domain!