SlideShare une entreprise Scribd logo
1  sur  77
Télécharger pour lire hors ligne
SQL/XML on Oracle
Kristian Torp
Department of Computer Science
Aalborg University
people.cs.aau.dk/˜torp
torp@cs.aau.dk
November 26, 2015
daisy.aau.dk
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 1 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 2 / 73
Learning Goals
Goals
Overview of SQL/XML
Extract relational information as XML
Introduction querying XML on PostgreSQL
Note
SQL/XML is part of the SQL standard
SQL/XML is being supported by the major DBMS vendors
It has nothing to do with SQLXML from Microsoft
Standard
Concepts are general
Code is sometimes DBMS specific
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 3 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 4 / 73
Overall Picture
The Main Idea
SQL/XML query <stuff></stuff>
Note
SQL/XML is an SQL extension
Added in 2003, extended in 2008 and 2011 versions of SQL
SQL/XML is bidirectional from tables to XML
Started as tables to XML
Extended with XML to tables
The major DBMS vendors do not agree on the SQL/XML syntax!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 5 / 73
XPath and XQuery vs. SQL/XML
XPath and XQuery
XPath and XQuery are XML
centric
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73
XPath and XQuery vs. SQL/XML
XPath and XQuery
XPath and XQuery are XML
centric
SQL/XML
SQL/XML is SQL centric
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 7 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 8 / 73
Create Relational Schema
Example (Create the Table)
create table course (
id i n t primary key ,
name varchar2 (50) not null ,
semester i n t not null ,
descr varchar2 (255) not n u l l
) ;
Example (Load the Data)
i n s e r t i n t o course values
(4 , ’OOP’ , 3 , ’ Object−oriented programming ’ ) ;
i n s e r t i n t o course values
(2 , ’DB ’ , 7 , ’ Databases including SQL ’ ) ;
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 9 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 10 / 73
Introduction: SQL/XML
Steps
Main purpose to map between XML and SQL
A wrapper layer
A standard defined by the ISO/ANSI SQL committee
Part 14 of of SQL 2003
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 11 / 73
Get XML Out, First Try
Example (Query One)
select XMLElement ( ” course ” ,
XMLAttributes ( id as ” id ” ) ,
XMLElement ( ”name” , name) ,
XMLElement ( ” semester ” , semester ) ,
XMLElement ( ” desc ” , descr ) )
from course ;
Note
It is a select statement!
The SQL/XMLpublishing functions XMLElement and XMLAttributes.
It is fairly easy to guess what it does!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 12 / 73
First Result
Example (Result Query One)
<course id=” 4 ”>
<name>OOP</name>
<semester>3</ semester>
<desc>Object−oriented programming</ desc>
</ course>
<course id=” 2 ”>
<name>DB</name>
<semester>7</ semester>
<desc>Databases including SQL</ desc>
</ course>
Note
Not a well-formed XML document! Why?
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 13 / 73
Get XML Out, Adding a Root Element
Example (Query Two)
select XMLElement ( ” coursecatalog ” ,
XMLAgg( XMLElement ( ” course ” ,
XMLAttributes ( id as ” id ” ) ,
XMLElement ( ”name” , name) ,
XMLElement ( ” semester ” , semester ) ,
XMLElement ( ” desc ” , descr ) ) ) )
from course ;
Note
The XMLAgg another publishing function
XMLAgg is an aggregate function like min, max, and avg
XMLAgg has optional order by clause
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 14 / 73
Second Result
Example (Result Query Two)
<coursecatalog>
<course id=” 4 ”>
<name>OOP</name>
<semester>3</ semester>
<desc>Object−oriented programming</ desc>
</ course>
<course id=” 2 ”>
<name>DB</name>
<semester>7</ semester>
<desc>Databases including SQL</ desc>
</ course>
</ coursecatalog>
Note
Now the XML document has a root element
Still missing the processing instructions at the top
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 15 / 73
Get XML Out, Adding XML Definition
Example (Query Three)
select XMLRoot(
( select XMLElement ( ” coursecatalog ” ,
XMLAgg( XMLElement ( ” course ” ,
XMLAttributes ( id as ” id ” ) ,
XMLElement ( ”name” , name) ,
XMLElement ( ” semester ” , semester ) ,
XMLElement ( ” desc ” , descr ) ) ) )
from course ) , VERSION ’ 1.0 ’ )
from dual ;
Note
It is now a nested statement
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 16 / 73
Third Result
Example (Result Query Three)
<?xml version=” 1.0 ” standalone=” yes ” ?>
<coursecatalog>
<course id=” 4 ”>
<name>OOP</name>
<semester>3</ semester>
<desc>Object−oriented programming</ desc>
</ course>
<course id=” 2 ”>
<name>DB</name>
<semester>7</ semester>
<desc>Databases including SQL</ desc>
</ course>
</ coursecatalog>
Note
Got the XML header
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 17 / 73
Get XML Out, Adding an XSLT
Example (Query Four)
select XMLRoot(
XMLConcat
(
XMLPI(NAME ” xml−stylesheet ” , ’ type =” t e x t / xsl ”
href = ” . . / x s l t / coursecatalog . x s l t ” ’ ) ,
(
select XMLElement ( ” coursecatalog ” ,
XMLAgg( XMLElement ( ” course ” ,
XMLAttributes ( id as ” id ” ) ,
XMLElement ( ”name” , name) ,
XMLElement ( ” semester ” , semester ) ,
XMLElement ( ” desc ” , descr ) ) ) )
from course
)
) ,
VERSION ’ 1.0 ’ ) from dual ;
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 18 / 73
Final Result
Example (Result Query Four)
<?xml version=” 1.0 ” ?>
<?xml−stylesheet type=” t e x t / xsl ”
href=” . . / x s l t / coursecatalog . x s l t ” ?>
<coursecatalog>
<course id=” 4 ”>
<name>OOP</name>
<semester>3</ semester>
<desc>Object−oriented programming</ desc>
</ course>
<course id=” 2 ”>
<name>DB</name>
<semester>7</ semester>
<desc>Databases including SQL</ desc>
</ course>
</ coursecatalog>
Note
A nice valid XML document!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 19 / 73
SQL/XML Publishing Functions
Overview
Function Description
XMLRoot Creates a root node
XMLElement Creates an XML element
XMLAttributes Creates attributes on elements
XMLAgg Aggregates XML fragments
XMLForest Creates a forest of elements
XMLConcat Appends elements
XMLPI Create processing instructions
XMLComment Create comments
Note
These are all standard publishing functions
The uses/misuse of abbreviations
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 20 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 21 / 73
Quick and Dirty
Example (Shortest Possible)
select XMLForest ( id , name, semester , descr )
from course
Example (The Result)
<ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER><DESCR> . . . </DESCR>
<ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER><DESCR> . . . </DESCR>
Note
Two rows are returned
Element names are in upper case (Oracle/SQL default)
No container element for each row
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 22 / 73
Quick and Dirty
Example (Shortest Possible)
select XMLForest ( ∗ )
from course
Note
Not allowed on Oracle
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73
Quick and Dirty
Example (Shortest Possible)
select XMLForest ( ∗ )
from course
Note
Not allowed on Oracle
Example (Shortest Possible)
select XMLForest ( co . ∗ )
from course co
Note
Not allowed either!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73
Rename Columns
Example (Rename Columns)
select XMLForest ( ID as ” id ” ,
name as ” course−name” ,
semester as ” semester ” ,
descr as ” description ” )
from course
Example (The Result)
<id>4</ id><course−name>OOP</ course−name><semester>3</ semester> . . .
<id>2</ id><course−name>DB</ course−name><semester>7</ semester> . . .
Note
Must use double quote ” and not single quote
As is optional if scalar value
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 24 / 73
Added Outer Element
Example (A Tag for Each Row)
select XMLElement (
” course ” ,
XMLForest ( ID as ” id ” ,
name as ” course−name” ,
semester as ” semester ” ,
descr as ” description ” ) )
from course
Example (The Result)
<course><id>4</ id><course−name>OOP</ course−name> . . . </ course>
<course><id>2</ id><course−name>DB</ course−name> . . . </ course>
Note
Still two rows being returned
The wrapping of each row
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 25 / 73
Quick and Dirty, cont
Example (With Projection)
select XMLForest ( id , name, semester )
from course
Example (The Result)
<ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER>
<ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73
Quick and Dirty, cont
Example (With Projection)
select XMLForest ( id , name, semester )
from course
Example (The Result)
<ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER>
<ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER>
Example (With Projection and Renaming)
select XMLForest ( id as ” cid ” , name ” coursename ” )
from course
Example (The Result)
<cid>4</ cid><coursename>OOP</ coursename>
<cid>2</ cid><coursename>DB</ coursename>
Note
Filtering (selection) is possible in the where clause
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73
Quick and Dirty, cont I
Example (With Element per Row)
select XMLElement (name ” course ” ,
XMLForest ( id as ” cid ” , name as ”cname” ) )
from course
Example (The Result)
<course><cid>4</ cid><cname>OOP</ cname></ course>
<course><cid>2</ cid><cname>DB</ cname></ course>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 27 / 73
Quick and Dirty, cont II
Example (With Root Element)
select XMLElement ( ” coursecatalog ” ,
XMLAgg(
XMLElement ( ” course ” ,
XMLForest ( id as ” cid ” , name as ”cname” ) ) ) )
from course
Example (The Result)
<coursecatalog>
<course><cid>4</ cid><cname>OOP</ cname></ course>
<course><cid>2</ cid><cname>DB</ cname></ course>
</ coursecatalog>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 28 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 29 / 73
XMLType Views
Example (Create an XML view on an existing table)
create or replace view course xml of XMLType with object id
( e x tract
( object value , ’ / coursecatalog / course / @id ’ ) . getnumberval ( ) )
as select XMLElement ( ” coursecatalog ” ,
XMLAgg( XMLElement ( ” course ” ,
XMLAttributes ( id as ” id ” ) ,
XMLElement ( ”name” , name) ,
XMLElement ( ” semester ” , semester ) ,
XMLElement ( ” desc ” , descr ) ) ) )
from course ;
Note
A create view statement
Use the SQL/XML publishing functions
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 30 / 73
XQuery on Oracle I
Example (Get info on the OOP course)
XQUERY
f o r $c in ora : view ( ” course xml ” ) / coursecatalog / course
where $c /name=”OOP”
return $c
Example (Result)
column value
<course id=” 4 ”>
<name>OOP</name>
<semester>3</ semester>
<desc>Object−oriented Programming</ desc>
</ course>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 31 / 73
XQuery on Oracle II
Example (Count the number of courses)
XQUERY
f o r $c in ora : view ( ” course xml ” ) / coursecatalog
l e t $cour := count ( $c / course )
return $cour
Example (Result)
column value
2
Note
Remember ’/’ to execute from command line
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 32 / 73
Summary: Tables to XML
Summary
SQL/XML is an ISO/ANSI standard not part of W3C
Oracle has made vendor specific extensions to SQL/XML
SQL/XML is good for mapping SQL to XML
It is often very convenient to be able to do the mapping in plain SQL
Start building SQL/XML queries from the inside and out
Alternatives to SQL/XML
Store XML in native format in the database
DBMS vendor specific extension, e.g. DBMS XMLGEN
Do SQL to XML in programming language
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 33 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 34 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 35 / 73
Introduction
Example (The Best of Both Worlds)
Id Txt
1 <course id=’22’><name>OOP</name></course>
2 <course id=’11’><name>DB</name></course>
3 <course id=’33’><name>SQL</name></course>
SQL and XML
Store XML as any other data type in a table
Retain possiblity to use SQL for querying data
Be able to query the XML data using XPath and XQuery
Conversion of XML to SQL
Retain pure SQL view on all data
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 36 / 73
The coursecat Table
Example
id dsc exercises
11
<course cid=” 11 ”>
<name>Database</name>
</ course>
22
<course cid=” 22 ”>
<name>OOP</name>
</ course>
<exercises>
<exercise eid=” 1 ”>
<desc>What i s OOP?</ desc>
<answer> I t . . . </ answer>
</ exercise>
</ exercises>
23
<course cid=” 33 ”>
<name>SQL</name>
</ course>
<exercises>
<exercise eid=” 1 ”>
<desc>What i s SQL?</ desc>
<answer> I t . . . </ answer>
</ exercise>
<exercise eid=” 11 ”>
<desc>What i s a query?</ desc>
<answer> I t . . . </ answer>
</ exercise>
</ exercises>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 37 / 73
Create Relational Schema
Example (Create the Table)
create table coursecat (
id i n t primary key ,
dsc xmltype not null ,
exercises xmltype
) ;
Example (Load the Data)
i n s e r t i n t o coursecat values (
22 ,
’<course cid =”22”>
<name>OOP</name>
</course> ’ ,
’<exercises >
<exercise eid =”1”>
<desc>What i s OOP?</desc>
<answer> I t . . . < / answer>
</ exercise >
</ exercises > ’
) ;
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 38 / 73
Say Hello
Example (Hello, World!)
select XMLQuery( ’
l e t $s := ” Hello , World ! ”
return $s ’ returning content ) as o
from dual ;
Example (Result)
o
Hello, World!
Note
The XMLQuery function
The XQuery in a string
The returning content is required
The alias (o) is optional (otherwise ugly column name)
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 39 / 73
Simple XPath
Example (Use SQL and XPath)
select id , xmlquery ( ’ / / exercise ’
passing exercises
returning content ) as res
from coursecat
Example (Result)
ID RES
11
22 (XMLTYPE)
23 (XMLTYPE)
Note
Passing by value exercises
Return value is of the XMLType data type
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 40 / 73
Extracting Values
Example (extractvalue Function)
select id ,
extractvalue ( dsc , ’ / / course /name/ t e x t ( ) ’ ) as course name
from coursecat
Example (Result)
ID COURSE NAME
11 Database
22 OOP
23 SQL
Note
The extracevalue function is Oracle specific
//course/name will do the same
//course will fail (more than one node)!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 41 / 73
Extracting Multiple Values
Example (XML to SQL)
select id ,
extractvalue ( dsc , ’ / / course / @cid ’ ) as course id ,
extractvalue ( dsc , ’ / / course /name/ t e x t ( ) ’ ) as course name
from coursecat
Example (Result)
ID COURSE ID COURSE NAME
11 11 Database
22 22 OOP
23 33 SQL
Note
Attributes and elements extract in the same manner
Now have a pure relational access to data
Can filter on COURSE ID in a where clause
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 42 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 43 / 73
Extracting Content Directly from XML
Example (XMLQuery Function)
select
xmlquery ( ’ f o r $doc in
fn : c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/DSC” )
return $doc ’ returning content )
from dual
Example (Result)
XMLQUERY(. . .)
<course cid=”11”><name>Database</name>... </course>
Note
All in upper case
PUBLIC if table is available to current user
ROW is required in the XPath
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 44 / 73
From SQL Directly
Example (XQUERY SQL∗Plus Keyword)
XQUERY
f o r $doc in c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/EXERCISES” )
where $doc / / exercise / @eid = 1
return $doc
Example (Result)
COLUMN VALUE
<exercises><exercise eid=”1”><desc>What is OOP?</desc>...
Note
XQUERY not XMLQuery
The collection is the fn:collection method
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 45 / 73
Summary: XML Data Type
Summary
XMLQuery is part of the SQL standard
Allows full XQuery
In princip XQuery on XML file in Oracle!
Oracle as a pure XQuery engine
Supported by DB2 and Oracle
Data type other name (xml) on SQL Server
Oracle made extensions to support SQL∗Plus
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 46 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 47 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 48 / 73
The Overall Idea
The Flow
XMLTable query <stuff></stuff>
Note
XMLTable is a part of SQL/XML
XMLTable returns a rowset
It is a table function
Overview SQL/XML Functions
SQL Clause SQL/XML Function
select XMLQuery
from XMLTable
where XMLExists
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 49 / 73
The coursecat Table
Example
id dsc exercises
11
<course cid=” 11 ”>
<name>Database</name>
</ course>
22
<course cid=” 22 ”>
<name>OOP</name>
</ course>
<exercises>
<exercise eid=” 1 ”>
<desc>What i s OOP?</ desc>
<answer> I t . . . </ answer>
</ exercise>
</ exercises>
23
<course cid=” 33 ”>
<name>SQL</name>
</ course>
<exercises>
<exercise eid=” 1 ”>
<desc>What i s SQL?</ desc>
<answer> I t . . . </ answer>
</ exercise>
<exercise eid=” 11 ”>
<desc>What i s a query?</ desc>
<answer> I t . . . </ answer>
</ exercise>
</ exercises>
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 50 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 51 / 73
From XML to SQL
Example (Get the Course Name)
select id , course name
from coursecat ,
XMLTable ( ’ / course ’ passing dsc
columns course name varchar2 (10) path ’name ’ )
Example (Result)
ID COURSE NAME
11 Database
22 OOP
23 SQL
Note
The passing, columns, and path keywords
/course is the row pattern, here XPath full XQuery supported
name is the column pattern, here XPath full XQuery supported
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 52 / 73
More Advanced XPath
Example (Multiple XML Columns)
select id , course name , no exercises
from coursecat ,
XMLTable ( ’ / course ’ passing dsc
columns course name varchar2 (10) path ’name ’ ) ,
XMLTable ( ’ / exercises ’ passing exercises
columns no exercises i n t path ’ count ( exercise ) ’ )
Example (Result)
ID COURSE NAME NO EXERCISES
22 OOP 1
23 SQL 2
Note
Multiple XMLTable calls
No join condition, recall implicitely joined
Advanced path expressions count(exercise)
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 53 / 73
Using in the Where Clause
Example (Find where IDs are not Matching)
select id , course id , course name
from coursecat ,
XMLTable ( ’ / course ’ passing dsc
columns course id i n t path ’@cid ’ ,
course name varchar2 (10) path ’name ’ )
where id != course id
Example (Result)
ID COURSE ID COURSE NAME
23 33 SQL
Note
Attribute used in column pattern: @cid
Element used in column pattern: name
Comparison in where clause
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 54 / 73
Ordinality and Default Value
Example (Find where IDs are not Matching)
select coursecat . id , num, exercise id , author
from coursecat ,
XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
num f o r o r d i n a l i t y ,
exercise id i n t path ’@eid ’ ,
author varchar2 (30) path ’ author ’ default ’ Ib ’
Example (Result)
ID NUM EXERCISE ID AUTHOR
22 1 1 Ib
23 1 1 Ib
23 2 11 Ib
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 55 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 56 / 73
Information on Course ID and Name I
Example
select coursecat . id , x . course name
from coursecat ,
XMLTable ( ’ / course ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / course /name ’ ) x
Example (Result)
id course name
11 Database
22 OOP
23 SQL
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 57 / 73
Information on Course ID and Name II
Example
select coursecat . id , x . course name
from coursecat ,
XMLTable ( ’ / course ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / course /name ’ ) x
Note
Displays relation data along side XML data!
Implicit join
Columns explicitely named and typed
XMLTable alias (x) is optional
The XPath expression in columns can only return one item (per row)!
/course can be simplified to (/)
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 58 / 73
Information on Course ID, Name, and Exercises I
Example
select coursecat . id , course name , exercise id
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc −− table −name. column−name
columns
course name varchar2 (30) path ’ / / name ’ ) ,
XMLTable ( ’ / ’
passing exercises −− only column name
columns
exercise id i n t path ’ / exercises / exercise [ 1 ] / @eid ’ )
Example (Result)
id course name exercise id
22 OOP 1
23 SQL 1
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 59 / 73
Information on Course ID, Name, and Exercises II
Example
select coursecat . id , course name , exercise id
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc −− table −name. column−name
columns
course name varchar2 (30) path ’ / / name ’ ) ,
XMLTable ( ’ / ’
passing exercises −− only column name
columns
exercise id i n t path ’ / exercises / exercise [ 1 ] / @eid ’ )
Note
Multiple XMLTable functions
Both elements and attributes are converted to tabular
The use of absolute and relative paths
Must select first exercise otherwise error
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 60 / 73
Information on Course ID, Name, and Exercises, cont I
Example
select coursecat . id , course name , exercise id , descr , answer
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / / name ’ ) ,
XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ )
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 61 / 73
Information on Course ID, Name, and Exercises, cont II
Example (Result)
id course name exercise id descr answer
22 OOP 1 What is OOP? It ...
23 SQL 1 What is SQL? It ...
23 SQL 11 What is a query? It ...
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 62 / 73
Information on Course ID, Name, and Exercises, cont III
Example
select coursecat . id , course name , exercise id , descr , answer
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / / name ’ ) ,
XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ )
Note
Change the XPath for exercises to a lower level
Missing information on one course
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 63 / 73
Information on Course ID, Name, and Exercises, cont I
Example
select coursecat . id , course name , exercise id , descr , answer
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / / name ’ )
l e f t outer j o i n
XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ ) on 1=1
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 64 / 73
Information on Course ID, Name, and Exercises, cont II
Example (Result)
id course name exercise id descr answer
11 Database
22 OOP 1 What is OOP? It ...
23 SQL 1 What is SQL? It ...
23 SQL 11 What is a query? It ...
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 65 / 73
Information on Course ID, Name, and Exercises, cont III
Example
select coursecat . id , course name , exercise id , descr , answer
from coursecat ,
XMLTable ( ’ / ’
passing coursecat . dsc
columns
course name varchar2 (30) path ’ / / name ’ )
l e f t outer j o i n
XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ ) on 1=1
Note
The left outer join between the two XMLTable function calls
The on clause on 1=1
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 66 / 73
Filtering Based on XML Content I
Example
select x .∗
from coursecat , XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ ) x
Example (Result)
exercise id descr answer
1 What is OOP? It ...
1 What is SQL? It ...
11 What is a query? It ...
Note
Too much information, i.e., no filtering
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 67 / 73
Filtering Based on XML Content II
Example
select x .∗
from coursecat , XMLTable ( ’ / exercises / exercise ’
passing exercises
columns
exercise id i n t path ’@eid ’ ,
descr varchar2 (30) path ’ desc ’ ,
answer varchar2 (30) path ’ answer ’ ) x
where XMLExists ( ’ / exercises / exercise [ @eid=11] ’ passing exercises )
Example (Result)
exercise id descr answer
1 What is SQL? It ...
11 What is a query? It ...
Note
The XMLExist for filtering in XML content
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 68 / 73
Summary: XML to Tables
Summary
The core function is XMLTable
Supported on most DBMS
Explicit naming
Cannot guess SQL column names element/attributes
Explicit typing
Cannot guess SQL data types from XML document
Result of XMLTable can be joined like other tables!
Literature
XMLTABLE by example, Part 1
Walk-through on how to handle multiple rows (five diff. ways)
www.ibm.com/developerworks/data/library/techarticle/
dm-0708nicola/
XMLTABLE by example, Part 2
On scredding large XML documents plus insert relational from XML
www.ibm.com/developerworks/data/library/techarticle/
dm-0709nicola/Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 69 / 73
Outline
1 Introduction
2 From Tables to XML
Introduction
SQL/XML Publishing Functions
Quick and Dirty Publishing Functions
XML View on Relational Data
3 The XML Data Type
Introduction
oradb: Protocol
4 From XML to Tables
Introduction
A Simple Example
A Longer Example
5 Summary
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 70 / 73
SQL/XML vs. XPath/XQuery
SQL/XML
SQL centric
Small extension to SQL
null well understood
No implicit order
Bad support hierarchies
XPath/XQuery
XML centric
New programming languages
Handling of null be aware!
Ordering (a sequence)
Excellent support hierarchies
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73
SQL/XML vs. XPath/XQuery
SQL/XML
SQL centric
Small extension to SQL
null well understood
No implicit order
Bad support hierarchies
XPath/XQuery
XML centric
New programming languages
Handling of null be aware!
Ordering (a sequence)
Excellent support hierarchies
Note
SQL/XML and XQuery serve different purposes
Are not competing technologies!
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73
Summary
Main Points
SQL/XML is an ISO/ANSI standard
SQL/XML example of wrapper technology
Make table look like XML documents
Well integrated into PostgreSQL, Oracle, and other DBMSs
XML data type is much smarter than a CLOB
Must look elsewhere for XML to tables
Standards
SQL/XML publishing functions added in SQL/XML:2003
XMLExists, XMLQuery, and XMLTable added in SQL/XML:2006
SQL/XML standardization faster than overall SQL standardization
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 72 / 73
Additional Information
Web Sites
Good SQL/XML tutorial
www.stylusstudio.com/sqlxml_tutorial.html
Advancements in SQL/XML www.sigmod.org/sigmod/record/
issues/0409/11.JimMelton.pdf
Jim Melton in SIGMOD Record
PostgreSQL and XML http:
//www.slideshare.net/petereisentraut/postgresql-and-xml
Slightly outdated, but good
Oracle’s XML Technology Center
www.oracle.com/technology/tech/xml/index.html
Get off to a fast start with DB2 9 pureXML
http://www.ibm.com/developerworks/data/library/
techarticle/dm-0603saracco2/
Part of series of papers on XML support on DB2
Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 73 / 73

Contenu connexe

Tendances

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
SPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx ConstraintsSPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx ConstraintsAbdullah Abbas
 
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Rathod Shukar
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...GeeksLab Odessa
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
XPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept ListingXPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept ListingIndrajeet Verma
 
Sedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationSedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationIvan Shcheklein
 

Tendances (15)

Xpath
XpathXpath
Xpath
 
Session 4
Session 4Session 4
Session 4
 
XML and XPath details
XML and XPath detailsXML and XPath details
XML and XPath details
 
X FILES
X FILESX FILES
X FILES
 
XPATH
XPATHXPATH
XPATH
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
XPath
XPathXPath
XPath
 
Object Class
Object Class Object Class
Object Class
 
SPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx ConstraintsSPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx Constraints
 
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
XPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept ListingXPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept Listing
 
Sedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationSedna XML Database System: Internal Representation
Sedna XML Database System: Internal Representation
 

Similaire à SQL/XML on Oracle

Introduction to XML and Databases
Introduction to XML and DatabasesIntroduction to XML and Databases
Introduction to XML and Databasestorp42
 
Introduction to DTD
Introduction to DTDIntroduction to DTD
Introduction to DTDtorp42
 
ibm_research_aug_8_03
ibm_research_aug_8_03ibm_research_aug_8_03
ibm_research_aug_8_03Attila Barta
 
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...Marco Gralike
 
Database management system chapter5
Database management system chapter5Database management system chapter5
Database management system chapter5Pranab Dasgupta
 
SQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML DataSQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML DataMarek Maśko
 
Sql Summit Clr, Service Broker And Xml
Sql Summit   Clr, Service Broker And XmlSql Summit   Clr, Service Broker And Xml
Sql Summit Clr, Service Broker And XmlDavid Truxall
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1Marco Gralike
 
Boost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco GralikeBoost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco GralikeMarco Gralike
 
aotc_120805_mwagner
aotc_120805_mwagneraotc_120805_mwagner
aotc_120805_mwagnerMary Wagner
 
Sedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing RewriterSedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing RewriterIvan Shcheklein
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For XmlLars Trieloff
 
SPARQLing Services
SPARQLing ServicesSPARQLing Services
SPARQLing ServicesLeigh Dodds
 
Oracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docxOracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docxChandan Kumar
 

Similaire à SQL/XML on Oracle (20)

Introduction to XML and Databases
Introduction to XML and DatabasesIntroduction to XML and Databases
Introduction to XML and Databases
 
Introduction to DTD
Introduction to DTDIntroduction to DTD
Introduction to DTD
 
ibm_research_aug_8_03
ibm_research_aug_8_03ibm_research_aug_8_03
ibm_research_aug_8_03
 
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
 
Database management system chapter5
Database management system chapter5Database management system chapter5
Database management system chapter5
 
SQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML DataSQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML Data
 
Sql Summit Clr, Service Broker And Xml
Sql Summit   Clr, Service Broker And XmlSql Summit   Clr, Service Broker And Xml
Sql Summit Clr, Service Broker And Xml
 
10053 otw
10053 otw10053 otw
10053 otw
 
PostgreSQL and XML
PostgreSQL and XMLPostgreSQL and XML
PostgreSQL and XML
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
 
Data shapes-test-suite
Data shapes-test-suiteData shapes-test-suite
Data shapes-test-suite
 
Boost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco GralikeBoost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
 
aotc_120805_mwagner
aotc_120805_mwagneraotc_120805_mwagner
aotc_120805_mwagner
 
treeview
treeviewtreeview
treeview
 
treeview
treeviewtreeview
treeview
 
Sedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing RewriterSedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing Rewriter
 
Preface
PrefacePreface
Preface
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For Xml
 
SPARQLing Services
SPARQLing ServicesSPARQLing Services
SPARQLing Services
 
Oracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docxOracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docx
 

Plus de torp42

The DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQLThe DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQLtorp42
 
Spatial Indexing
Spatial IndexingSpatial Indexing
Spatial Indexingtorp42
 
Entity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERDEntity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERDtorp42
 
Temporal Databases: Data Models
Temporal Databases: Data ModelsTemporal Databases: Data Models
Temporal Databases: Data Modelstorp42
 
Temporal Databases: Modifications
Temporal Databases: ModificationsTemporal Databases: Modifications
Temporal Databases: Modificationstorp42
 
Temporal Databases: Queries
Temporal Databases: QueriesTemporal Databases: Queries
Temporal Databases: Queriestorp42
 

Plus de torp42 (6)

The DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQLThe DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
 
Spatial Indexing
Spatial IndexingSpatial Indexing
Spatial Indexing
 
Entity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERDEntity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERD
 
Temporal Databases: Data Models
Temporal Databases: Data ModelsTemporal Databases: Data Models
Temporal Databases: Data Models
 
Temporal Databases: Modifications
Temporal Databases: ModificationsTemporal Databases: Modifications
Temporal Databases: Modifications
 
Temporal Databases: Queries
Temporal Databases: QueriesTemporal Databases: Queries
Temporal Databases: Queries
 

Dernier

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Dernier (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

SQL/XML on Oracle

  • 1. SQL/XML on Oracle Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp torp@cs.aau.dk November 26, 2015 daisy.aau.dk Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 1 / 73
  • 2. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 2 / 73
  • 3. Learning Goals Goals Overview of SQL/XML Extract relational information as XML Introduction querying XML on PostgreSQL Note SQL/XML is part of the SQL standard SQL/XML is being supported by the major DBMS vendors It has nothing to do with SQLXML from Microsoft Standard Concepts are general Code is sometimes DBMS specific Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 3 / 73
  • 4. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 4 / 73
  • 5. Overall Picture The Main Idea SQL/XML query <stuff></stuff> Note SQL/XML is an SQL extension Added in 2003, extended in 2008 and 2011 versions of SQL SQL/XML is bidirectional from tables to XML Started as tables to XML Extended with XML to tables The major DBMS vendors do not agree on the SQL/XML syntax! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 5 / 73
  • 6. XPath and XQuery vs. SQL/XML XPath and XQuery XPath and XQuery are XML centric Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73
  • 7. XPath and XQuery vs. SQL/XML XPath and XQuery XPath and XQuery are XML centric SQL/XML SQL/XML is SQL centric Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73
  • 8. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 7 / 73
  • 9. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 8 / 73
  • 10. Create Relational Schema Example (Create the Table) create table course ( id i n t primary key , name varchar2 (50) not null , semester i n t not null , descr varchar2 (255) not n u l l ) ; Example (Load the Data) i n s e r t i n t o course values (4 , ’OOP’ , 3 , ’ Object−oriented programming ’ ) ; i n s e r t i n t o course values (2 , ’DB ’ , 7 , ’ Databases including SQL ’ ) ; Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 9 / 73
  • 11. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 10 / 73
  • 12. Introduction: SQL/XML Steps Main purpose to map between XML and SQL A wrapper layer A standard defined by the ISO/ANSI SQL committee Part 14 of of SQL 2003 Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 11 / 73
  • 13. Get XML Out, First Try Example (Query One) select XMLElement ( ” course ” , XMLAttributes ( id as ” id ” ) , XMLElement ( ”name” , name) , XMLElement ( ” semester ” , semester ) , XMLElement ( ” desc ” , descr ) ) from course ; Note It is a select statement! The SQL/XMLpublishing functions XMLElement and XMLAttributes. It is fairly easy to guess what it does! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 12 / 73
  • 14. First Result Example (Result Query One) <course id=” 4 ”> <name>OOP</name> <semester>3</ semester> <desc>Object−oriented programming</ desc> </ course> <course id=” 2 ”> <name>DB</name> <semester>7</ semester> <desc>Databases including SQL</ desc> </ course> Note Not a well-formed XML document! Why? Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 13 / 73
  • 15. Get XML Out, Adding a Root Element Example (Query Two) select XMLElement ( ” coursecatalog ” , XMLAgg( XMLElement ( ” course ” , XMLAttributes ( id as ” id ” ) , XMLElement ( ”name” , name) , XMLElement ( ” semester ” , semester ) , XMLElement ( ” desc ” , descr ) ) ) ) from course ; Note The XMLAgg another publishing function XMLAgg is an aggregate function like min, max, and avg XMLAgg has optional order by clause Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 14 / 73
  • 16. Second Result Example (Result Query Two) <coursecatalog> <course id=” 4 ”> <name>OOP</name> <semester>3</ semester> <desc>Object−oriented programming</ desc> </ course> <course id=” 2 ”> <name>DB</name> <semester>7</ semester> <desc>Databases including SQL</ desc> </ course> </ coursecatalog> Note Now the XML document has a root element Still missing the processing instructions at the top Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 15 / 73
  • 17. Get XML Out, Adding XML Definition Example (Query Three) select XMLRoot( ( select XMLElement ( ” coursecatalog ” , XMLAgg( XMLElement ( ” course ” , XMLAttributes ( id as ” id ” ) , XMLElement ( ”name” , name) , XMLElement ( ” semester ” , semester ) , XMLElement ( ” desc ” , descr ) ) ) ) from course ) , VERSION ’ 1.0 ’ ) from dual ; Note It is now a nested statement Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 16 / 73
  • 18. Third Result Example (Result Query Three) <?xml version=” 1.0 ” standalone=” yes ” ?> <coursecatalog> <course id=” 4 ”> <name>OOP</name> <semester>3</ semester> <desc>Object−oriented programming</ desc> </ course> <course id=” 2 ”> <name>DB</name> <semester>7</ semester> <desc>Databases including SQL</ desc> </ course> </ coursecatalog> Note Got the XML header Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 17 / 73
  • 19. Get XML Out, Adding an XSLT Example (Query Four) select XMLRoot( XMLConcat ( XMLPI(NAME ” xml−stylesheet ” , ’ type =” t e x t / xsl ” href = ” . . / x s l t / coursecatalog . x s l t ” ’ ) , ( select XMLElement ( ” coursecatalog ” , XMLAgg( XMLElement ( ” course ” , XMLAttributes ( id as ” id ” ) , XMLElement ( ”name” , name) , XMLElement ( ” semester ” , semester ) , XMLElement ( ” desc ” , descr ) ) ) ) from course ) ) , VERSION ’ 1.0 ’ ) from dual ; Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 18 / 73
  • 20. Final Result Example (Result Query Four) <?xml version=” 1.0 ” ?> <?xml−stylesheet type=” t e x t / xsl ” href=” . . / x s l t / coursecatalog . x s l t ” ?> <coursecatalog> <course id=” 4 ”> <name>OOP</name> <semester>3</ semester> <desc>Object−oriented programming</ desc> </ course> <course id=” 2 ”> <name>DB</name> <semester>7</ semester> <desc>Databases including SQL</ desc> </ course> </ coursecatalog> Note A nice valid XML document! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 19 / 73
  • 21. SQL/XML Publishing Functions Overview Function Description XMLRoot Creates a root node XMLElement Creates an XML element XMLAttributes Creates attributes on elements XMLAgg Aggregates XML fragments XMLForest Creates a forest of elements XMLConcat Appends elements XMLPI Create processing instructions XMLComment Create comments Note These are all standard publishing functions The uses/misuse of abbreviations Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 20 / 73
  • 22. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 21 / 73
  • 23. Quick and Dirty Example (Shortest Possible) select XMLForest ( id , name, semester , descr ) from course Example (The Result) <ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER><DESCR> . . . </DESCR> <ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER><DESCR> . . . </DESCR> Note Two rows are returned Element names are in upper case (Oracle/SQL default) No container element for each row Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 22 / 73
  • 24. Quick and Dirty Example (Shortest Possible) select XMLForest ( ∗ ) from course Note Not allowed on Oracle Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73
  • 25. Quick and Dirty Example (Shortest Possible) select XMLForest ( ∗ ) from course Note Not allowed on Oracle Example (Shortest Possible) select XMLForest ( co . ∗ ) from course co Note Not allowed either! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73
  • 26. Rename Columns Example (Rename Columns) select XMLForest ( ID as ” id ” , name as ” course−name” , semester as ” semester ” , descr as ” description ” ) from course Example (The Result) <id>4</ id><course−name>OOP</ course−name><semester>3</ semester> . . . <id>2</ id><course−name>DB</ course−name><semester>7</ semester> . . . Note Must use double quote ” and not single quote As is optional if scalar value Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 24 / 73
  • 27. Added Outer Element Example (A Tag for Each Row) select XMLElement ( ” course ” , XMLForest ( ID as ” id ” , name as ” course−name” , semester as ” semester ” , descr as ” description ” ) ) from course Example (The Result) <course><id>4</ id><course−name>OOP</ course−name> . . . </ course> <course><id>2</ id><course−name>DB</ course−name> . . . </ course> Note Still two rows being returned The wrapping of each row Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 25 / 73
  • 28. Quick and Dirty, cont Example (With Projection) select XMLForest ( id , name, semester ) from course Example (The Result) <ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER> <ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73
  • 29. Quick and Dirty, cont Example (With Projection) select XMLForest ( id , name, semester ) from course Example (The Result) <ID>4</ ID><NAME>OOP</NAME><SEMESTER>3</SEMESTER> <ID>2</ ID><NAME>DB</NAME><SEMESTER>7</SEMESTER> Example (With Projection and Renaming) select XMLForest ( id as ” cid ” , name ” coursename ” ) from course Example (The Result) <cid>4</ cid><coursename>OOP</ coursename> <cid>2</ cid><coursename>DB</ coursename> Note Filtering (selection) is possible in the where clause Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73
  • 30. Quick and Dirty, cont I Example (With Element per Row) select XMLElement (name ” course ” , XMLForest ( id as ” cid ” , name as ”cname” ) ) from course Example (The Result) <course><cid>4</ cid><cname>OOP</ cname></ course> <course><cid>2</ cid><cname>DB</ cname></ course> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 27 / 73
  • 31. Quick and Dirty, cont II Example (With Root Element) select XMLElement ( ” coursecatalog ” , XMLAgg( XMLElement ( ” course ” , XMLForest ( id as ” cid ” , name as ”cname” ) ) ) ) from course Example (The Result) <coursecatalog> <course><cid>4</ cid><cname>OOP</ cname></ course> <course><cid>2</ cid><cname>DB</ cname></ course> </ coursecatalog> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 28 / 73
  • 32. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 29 / 73
  • 33. XMLType Views Example (Create an XML view on an existing table) create or replace view course xml of XMLType with object id ( e x tract ( object value , ’ / coursecatalog / course / @id ’ ) . getnumberval ( ) ) as select XMLElement ( ” coursecatalog ” , XMLAgg( XMLElement ( ” course ” , XMLAttributes ( id as ” id ” ) , XMLElement ( ”name” , name) , XMLElement ( ” semester ” , semester ) , XMLElement ( ” desc ” , descr ) ) ) ) from course ; Note A create view statement Use the SQL/XML publishing functions Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 30 / 73
  • 34. XQuery on Oracle I Example (Get info on the OOP course) XQUERY f o r $c in ora : view ( ” course xml ” ) / coursecatalog / course where $c /name=”OOP” return $c Example (Result) column value <course id=” 4 ”> <name>OOP</name> <semester>3</ semester> <desc>Object−oriented Programming</ desc> </ course> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 31 / 73
  • 35. XQuery on Oracle II Example (Count the number of courses) XQUERY f o r $c in ora : view ( ” course xml ” ) / coursecatalog l e t $cour := count ( $c / course ) return $cour Example (Result) column value 2 Note Remember ’/’ to execute from command line Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 32 / 73
  • 36. Summary: Tables to XML Summary SQL/XML is an ISO/ANSI standard not part of W3C Oracle has made vendor specific extensions to SQL/XML SQL/XML is good for mapping SQL to XML It is often very convenient to be able to do the mapping in plain SQL Start building SQL/XML queries from the inside and out Alternatives to SQL/XML Store XML in native format in the database DBMS vendor specific extension, e.g. DBMS XMLGEN Do SQL to XML in programming language Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 33 / 73
  • 37. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 34 / 73
  • 38. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 35 / 73
  • 39. Introduction Example (The Best of Both Worlds) Id Txt 1 <course id=’22’><name>OOP</name></course> 2 <course id=’11’><name>DB</name></course> 3 <course id=’33’><name>SQL</name></course> SQL and XML Store XML as any other data type in a table Retain possiblity to use SQL for querying data Be able to query the XML data using XPath and XQuery Conversion of XML to SQL Retain pure SQL view on all data Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 36 / 73
  • 40. The coursecat Table Example id dsc exercises 11 <course cid=” 11 ”> <name>Database</name> </ course> 22 <course cid=” 22 ”> <name>OOP</name> </ course> <exercises> <exercise eid=” 1 ”> <desc>What i s OOP?</ desc> <answer> I t . . . </ answer> </ exercise> </ exercises> 23 <course cid=” 33 ”> <name>SQL</name> </ course> <exercises> <exercise eid=” 1 ”> <desc>What i s SQL?</ desc> <answer> I t . . . </ answer> </ exercise> <exercise eid=” 11 ”> <desc>What i s a query?</ desc> <answer> I t . . . </ answer> </ exercise> </ exercises> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 37 / 73
  • 41. Create Relational Schema Example (Create the Table) create table coursecat ( id i n t primary key , dsc xmltype not null , exercises xmltype ) ; Example (Load the Data) i n s e r t i n t o coursecat values ( 22 , ’<course cid =”22”> <name>OOP</name> </course> ’ , ’<exercises > <exercise eid =”1”> <desc>What i s OOP?</desc> <answer> I t . . . < / answer> </ exercise > </ exercises > ’ ) ; Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 38 / 73
  • 42. Say Hello Example (Hello, World!) select XMLQuery( ’ l e t $s := ” Hello , World ! ” return $s ’ returning content ) as o from dual ; Example (Result) o Hello, World! Note The XMLQuery function The XQuery in a string The returning content is required The alias (o) is optional (otherwise ugly column name) Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 39 / 73
  • 43. Simple XPath Example (Use SQL and XPath) select id , xmlquery ( ’ / / exercise ’ passing exercises returning content ) as res from coursecat Example (Result) ID RES 11 22 (XMLTYPE) 23 (XMLTYPE) Note Passing by value exercises Return value is of the XMLType data type Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 40 / 73
  • 44. Extracting Values Example (extractvalue Function) select id , extractvalue ( dsc , ’ / / course /name/ t e x t ( ) ’ ) as course name from coursecat Example (Result) ID COURSE NAME 11 Database 22 OOP 23 SQL Note The extracevalue function is Oracle specific //course/name will do the same //course will fail (more than one node)! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 41 / 73
  • 45. Extracting Multiple Values Example (XML to SQL) select id , extractvalue ( dsc , ’ / / course / @cid ’ ) as course id , extractvalue ( dsc , ’ / / course /name/ t e x t ( ) ’ ) as course name from coursecat Example (Result) ID COURSE ID COURSE NAME 11 11 Database 22 22 OOP 23 33 SQL Note Attributes and elements extract in the same manner Now have a pure relational access to data Can filter on COURSE ID in a where clause Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 42 / 73
  • 46. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 43 / 73
  • 47. Extracting Content Directly from XML Example (XMLQuery Function) select xmlquery ( ’ f o r $doc in fn : c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/DSC” ) return $doc ’ returning content ) from dual Example (Result) XMLQUERY(. . .) <course cid=”11”><name>Database</name>... </course> Note All in upper case PUBLIC if table is available to current user ROW is required in the XPath Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 44 / 73
  • 48. From SQL Directly Example (XQUERY SQL∗Plus Keyword) XQUERY f o r $doc in c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/EXERCISES” ) where $doc / / exercise / @eid = 1 return $doc Example (Result) COLUMN VALUE <exercises><exercise eid=”1”><desc>What is OOP?</desc>... Note XQUERY not XMLQuery The collection is the fn:collection method Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 45 / 73
  • 49. Summary: XML Data Type Summary XMLQuery is part of the SQL standard Allows full XQuery In princip XQuery on XML file in Oracle! Oracle as a pure XQuery engine Supported by DB2 and Oracle Data type other name (xml) on SQL Server Oracle made extensions to support SQL∗Plus Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 46 / 73
  • 50. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 47 / 73
  • 51. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 48 / 73
  • 52. The Overall Idea The Flow XMLTable query <stuff></stuff> Note XMLTable is a part of SQL/XML XMLTable returns a rowset It is a table function Overview SQL/XML Functions SQL Clause SQL/XML Function select XMLQuery from XMLTable where XMLExists Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 49 / 73
  • 53. The coursecat Table Example id dsc exercises 11 <course cid=” 11 ”> <name>Database</name> </ course> 22 <course cid=” 22 ”> <name>OOP</name> </ course> <exercises> <exercise eid=” 1 ”> <desc>What i s OOP?</ desc> <answer> I t . . . </ answer> </ exercise> </ exercises> 23 <course cid=” 33 ”> <name>SQL</name> </ course> <exercises> <exercise eid=” 1 ”> <desc>What i s SQL?</ desc> <answer> I t . . . </ answer> </ exercise> <exercise eid=” 11 ”> <desc>What i s a query?</ desc> <answer> I t . . . </ answer> </ exercise> </ exercises> Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 50 / 73
  • 54. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 51 / 73
  • 55. From XML to SQL Example (Get the Course Name) select id , course name from coursecat , XMLTable ( ’ / course ’ passing dsc columns course name varchar2 (10) path ’name ’ ) Example (Result) ID COURSE NAME 11 Database 22 OOP 23 SQL Note The passing, columns, and path keywords /course is the row pattern, here XPath full XQuery supported name is the column pattern, here XPath full XQuery supported Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 52 / 73
  • 56. More Advanced XPath Example (Multiple XML Columns) select id , course name , no exercises from coursecat , XMLTable ( ’ / course ’ passing dsc columns course name varchar2 (10) path ’name ’ ) , XMLTable ( ’ / exercises ’ passing exercises columns no exercises i n t path ’ count ( exercise ) ’ ) Example (Result) ID COURSE NAME NO EXERCISES 22 OOP 1 23 SQL 2 Note Multiple XMLTable calls No join condition, recall implicitely joined Advanced path expressions count(exercise) Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 53 / 73
  • 57. Using in the Where Clause Example (Find where IDs are not Matching) select id , course id , course name from coursecat , XMLTable ( ’ / course ’ passing dsc columns course id i n t path ’@cid ’ , course name varchar2 (10) path ’name ’ ) where id != course id Example (Result) ID COURSE ID COURSE NAME 23 33 SQL Note Attribute used in column pattern: @cid Element used in column pattern: name Comparison in where clause Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 54 / 73
  • 58. Ordinality and Default Value Example (Find where IDs are not Matching) select coursecat . id , num, exercise id , author from coursecat , XMLTable ( ’ / exercises / exercise ’ passing exercises columns num f o r o r d i n a l i t y , exercise id i n t path ’@eid ’ , author varchar2 (30) path ’ author ’ default ’ Ib ’ Example (Result) ID NUM EXERCISE ID AUTHOR 22 1 1 Ib 23 1 1 Ib 23 2 11 Ib Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 55 / 73
  • 59. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 56 / 73
  • 60. Information on Course ID and Name I Example select coursecat . id , x . course name from coursecat , XMLTable ( ’ / course ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / course /name ’ ) x Example (Result) id course name 11 Database 22 OOP 23 SQL Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 57 / 73
  • 61. Information on Course ID and Name II Example select coursecat . id , x . course name from coursecat , XMLTable ( ’ / course ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / course /name ’ ) x Note Displays relation data along side XML data! Implicit join Columns explicitely named and typed XMLTable alias (x) is optional The XPath expression in columns can only return one item (per row)! /course can be simplified to (/) Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 58 / 73
  • 62. Information on Course ID, Name, and Exercises I Example select coursecat . id , course name , exercise id from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc −− table −name. column−name columns course name varchar2 (30) path ’ / / name ’ ) , XMLTable ( ’ / ’ passing exercises −− only column name columns exercise id i n t path ’ / exercises / exercise [ 1 ] / @eid ’ ) Example (Result) id course name exercise id 22 OOP 1 23 SQL 1 Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 59 / 73
  • 63. Information on Course ID, Name, and Exercises II Example select coursecat . id , course name , exercise id from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc −− table −name. column−name columns course name varchar2 (30) path ’ / / name ’ ) , XMLTable ( ’ / ’ passing exercises −− only column name columns exercise id i n t path ’ / exercises / exercise [ 1 ] / @eid ’ ) Note Multiple XMLTable functions Both elements and attributes are converted to tabular The use of absolute and relative paths Must select first exercise otherwise error Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 60 / 73
  • 64. Information on Course ID, Name, and Exercises, cont I Example select coursecat . id , course name , exercise id , descr , answer from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / / name ’ ) , XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 61 / 73
  • 65. Information on Course ID, Name, and Exercises, cont II Example (Result) id course name exercise id descr answer 22 OOP 1 What is OOP? It ... 23 SQL 1 What is SQL? It ... 23 SQL 11 What is a query? It ... Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 62 / 73
  • 66. Information on Course ID, Name, and Exercises, cont III Example select coursecat . id , course name , exercise id , descr , answer from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / / name ’ ) , XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) Note Change the XPath for exercises to a lower level Missing information on one course Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 63 / 73
  • 67. Information on Course ID, Name, and Exercises, cont I Example select coursecat . id , course name , exercise id , descr , answer from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / / name ’ ) l e f t outer j o i n XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) on 1=1 Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 64 / 73
  • 68. Information on Course ID, Name, and Exercises, cont II Example (Result) id course name exercise id descr answer 11 Database 22 OOP 1 What is OOP? It ... 23 SQL 1 What is SQL? It ... 23 SQL 11 What is a query? It ... Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 65 / 73
  • 69. Information on Course ID, Name, and Exercises, cont III Example select coursecat . id , course name , exercise id , descr , answer from coursecat , XMLTable ( ’ / ’ passing coursecat . dsc columns course name varchar2 (30) path ’ / / name ’ ) l e f t outer j o i n XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) on 1=1 Note The left outer join between the two XMLTable function calls The on clause on 1=1 Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 66 / 73
  • 70. Filtering Based on XML Content I Example select x .∗ from coursecat , XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) x Example (Result) exercise id descr answer 1 What is OOP? It ... 1 What is SQL? It ... 11 What is a query? It ... Note Too much information, i.e., no filtering Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 67 / 73
  • 71. Filtering Based on XML Content II Example select x .∗ from coursecat , XMLTable ( ’ / exercises / exercise ’ passing exercises columns exercise id i n t path ’@eid ’ , descr varchar2 (30) path ’ desc ’ , answer varchar2 (30) path ’ answer ’ ) x where XMLExists ( ’ / exercises / exercise [ @eid=11] ’ passing exercises ) Example (Result) exercise id descr answer 1 What is SQL? It ... 11 What is a query? It ... Note The XMLExist for filtering in XML content Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 68 / 73
  • 72. Summary: XML to Tables Summary The core function is XMLTable Supported on most DBMS Explicit naming Cannot guess SQL column names element/attributes Explicit typing Cannot guess SQL data types from XML document Result of XMLTable can be joined like other tables! Literature XMLTABLE by example, Part 1 Walk-through on how to handle multiple rows (five diff. ways) www.ibm.com/developerworks/data/library/techarticle/ dm-0708nicola/ XMLTABLE by example, Part 2 On scredding large XML documents plus insert relational from XML www.ibm.com/developerworks/data/library/techarticle/ dm-0709nicola/Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 69 / 73
  • 73. Outline 1 Introduction 2 From Tables to XML Introduction SQL/XML Publishing Functions Quick and Dirty Publishing Functions XML View on Relational Data 3 The XML Data Type Introduction oradb: Protocol 4 From XML to Tables Introduction A Simple Example A Longer Example 5 Summary Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 70 / 73
  • 74. SQL/XML vs. XPath/XQuery SQL/XML SQL centric Small extension to SQL null well understood No implicit order Bad support hierarchies XPath/XQuery XML centric New programming languages Handling of null be aware! Ordering (a sequence) Excellent support hierarchies Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73
  • 75. SQL/XML vs. XPath/XQuery SQL/XML SQL centric Small extension to SQL null well understood No implicit order Bad support hierarchies XPath/XQuery XML centric New programming languages Handling of null be aware! Ordering (a sequence) Excellent support hierarchies Note SQL/XML and XQuery serve different purposes Are not competing technologies! Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73
  • 76. Summary Main Points SQL/XML is an ISO/ANSI standard SQL/XML example of wrapper technology Make table look like XML documents Well integrated into PostgreSQL, Oracle, and other DBMSs XML data type is much smarter than a CLOB Must look elsewhere for XML to tables Standards SQL/XML publishing functions added in SQL/XML:2003 XMLExists, XMLQuery, and XMLTable added in SQL/XML:2006 SQL/XML standardization faster than overall SQL standardization Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 72 / 73
  • 77. Additional Information Web Sites Good SQL/XML tutorial www.stylusstudio.com/sqlxml_tutorial.html Advancements in SQL/XML www.sigmod.org/sigmod/record/ issues/0409/11.JimMelton.pdf Jim Melton in SIGMOD Record PostgreSQL and XML http: //www.slideshare.net/petereisentraut/postgresql-and-xml Slightly outdated, but good Oracle’s XML Technology Center www.oracle.com/technology/tech/xml/index.html Get off to a fast start with DB2 9 pureXML http://www.ibm.com/developerworks/data/library/ techarticle/dm-0603saracco2/ Part of series of papers on XML support on DB2 Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 73 / 73