Top Banner
SQL/XML on Oracle Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp [email protected] November 26, 2015 daisy.aau.dk Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 1 / 73
77

SQL/XML on Oracle

Jan 26, 2017

Download

Technology

torp42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL/XML on Oracle

SQL/XML on Oracle

Kristian Torp

Department of Computer ScienceAalborg University

people.cs.aau.dk/˜[email protected]

November 26, 2015

daisy.aau.dk

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 1 / 73

Page 2: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 2 / 73

Page 3: SQL/XML on Oracle

Learning Goals

GoalsOverview of SQL/XML

Extract relational information as XML

Introduction querying XML on PostgreSQL

NoteSQL/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

StandardConcepts are general

Code is sometimes DBMS specific

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 3 / 73

Page 4: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 4 / 73

Page 5: SQL/XML on Oracle

Overall Picture

The Main Idea

SQL/XML query <stuff></stuff>

NoteSQL/XML is an SQL extension

Added in 2003, extended in 2008 and 2011 versions of SQL

SQL/XML is bidirectional from tables to XMLStarted as tables to XMLExtended 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

Page 6: SQL/XML on Oracle

XPath and XQuery vs. SQL/XML

XPath and XQuery

XPath and XQuery are XMLcentric

SQL/XML

SQL/XML is SQL centric

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73

Page 7: SQL/XML on Oracle

XPath and XQuery vs. SQL/XML

XPath and XQuery

XPath and XQuery are XMLcentric

SQL/XML

SQL/XML is SQL centric

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 6 / 73

Page 8: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 7 / 73

Page 9: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 8 / 73

Page 10: SQL/XML on Oracle

Create Relational Schema

Example (Create the Table)create tab l e course (

i d i n t pr imary key ,name varchar2 (50) not n u l l ,semester i n t not n u l l ,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−or ien ted programming ’ ) ;i n s e r t i n t o course values

(2 , ’DB ’ , 7 , ’ Databases i n c l u d i n g SQL ’ ) ;

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 9 / 73

Page 11: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 10 / 73

Page 12: SQL/XML on Oracle

Introduction: SQL/XML

StepsMain purpose to map between XML and SQL

A wrapper layer

A standard defined by the ISO/ANSI SQL committeePart 14 of of SQL 2003

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 11 / 73

Page 13: SQL/XML on Oracle

Get XML Out, First Try

Example (Query One)s e l e c t XMLElement ( ” course ” ,

XMLAtt r ibutes ( i d as ” i d ” ) ,XMLElement ( ”name” , name) ,XMLElement ( ” semester ” , semester ) ,XMLElement ( ” desc ” , descr ) )

from course ;

NoteIt 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

Page 14: SQL/XML on Oracle

First Result

Example (Result Query One)<course i d = ” 4 ”><name>OOP< / name><semester>3< / semester><desc>Object−or iented programming< / desc>

< / course><course i d = ” 2 ”><name>DB< / name><semester>7< / semester><desc>Databases i n c l u d i n g SQL< / desc>

< / course>

NoteNot a well-formed XML document! Why?

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 13 / 73

Page 15: SQL/XML on Oracle

Get XML Out, Adding a Root Element

Example (Query Two)s e l e c t XMLElement ( ” coursecata log ” ,

XMLAgg( XMLElement ( ” course ” ,XMLAtt r ibutes ( i d as ” i d ” ) ,

XMLElement ( ”name” , name) ,XMLElement ( ” semester ” , semester ) ,XMLElement ( ” desc ” , descr ) ) ) )

from course ;

NoteThe 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

Page 16: SQL/XML on Oracle

Second Result

Example (Result Query Two)<coursecata log><course i d = ” 4 ”><name>OOP< / name><semester>3< / semester><desc>Object−or iented programming< / desc>

< / course><course i d = ” 2 ”><name>DB< / name><semester>7< / semester><desc>Databases i n c l u d i n g SQL< / desc>

< / course>< / coursecata log>

NoteNow 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

Page 17: SQL/XML on Oracle

Get XML Out, Adding XML Definition

Example (Query Three)s e l e c t XMLRoot (

( s e l e c t XMLElement ( ” coursecata log ” ,XMLAgg( XMLElement ( ” course ” ,

XMLAtt r ibutes ( i d as ” i d ” ) ,XMLElement ( ”name” , name) ,XMLElement ( ” semester ” , semester ) ,XMLElement ( ” desc ” , descr ) ) ) )

from course ) , VERSION ’ 1.0 ’ )from dual ;

NoteIt is now a nested statement

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 16 / 73

Page 18: SQL/XML on Oracle

Third Result

Example (Result Query Three)<?xml vers ion= ” 1.0 ” standalone= ” yes ” ?><coursecata log><course i d = ” 4 ”><name>OOP< / name><semester>3< / semester><desc>Object−or iented programming< / desc>

< / course><course i d = ” 2 ”><name>DB< / name><semester>7< / semester><desc>Databases i n c l u d i n g SQL< / desc>

< / course>< / coursecata log>

NoteGot the XML header

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 17 / 73

Page 19: SQL/XML on Oracle

Get XML Out, Adding an XSLT

Example (Query Four)s e l e c t XMLRoot (

XMLConcat(

XMLPI (NAME ” xml−s t y l eshee t ” , ’ type =” t e x t / x s l ”h re f = ” . . / x s l t / coursecata log . x s l t ” ’ ) ,

(s e l e c t XMLElement ( ” coursecata log ” ,

XMLAgg( XMLElement ( ” course ” ,XMLAtt r ibutes ( i d as ” i d ” ) ,

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

Page 20: SQL/XML on Oracle

Final Result

Example (Result Query Four)<?xml vers ion= ” 1.0 ” ?><?xml−sty lesheet type= ” t e x t / x s l ”

h re f = ” . . / x s l t / coursecata log . x s l t ” ?><coursecata log><course i d = ” 4 ”><name>OOP< / name><semester>3< / semester><desc>Object−or iented programming< / desc>

< / course><course i d = ” 2 ”><name>DB< / name><semester>7< / semester><desc>Databases i n c l u d i n g SQL< / desc>

< / course>< / coursecata log>

NoteA nice valid XML document!

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 19 / 73

Page 21: SQL/XML on Oracle

SQL/XML Publishing Functions

Overview

Function Description

XMLRoot Creates a root nodeXMLElement Creates an XML elementXMLAttributes Creates attributes on elementsXMLAgg Aggregates XML fragmentsXMLForest Creates a forest of elementsXMLConcat Appends elementsXMLPI Create processing instructionsXMLComment Create comments

NoteThese are all standard publishing functions

The uses/misuse of abbreviations

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 20 / 73

Page 22: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 21 / 73

Page 23: SQL/XML on Oracle

Quick and Dirty

Example (Shortest Possible)s e l e c t 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>

NoteTwo 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

Page 24: SQL/XML on Oracle

Quick and Dirty

Example (Shortest Possible)s e l e c t XMLForest ( ∗ )from course

NoteNot allowed on Oracle

Example (Shortest Possible)s e l e c t XMLForest ( co . ∗ )from course co

NoteNot allowed either!

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73

Page 25: SQL/XML on Oracle

Quick and Dirty

Example (Shortest Possible)s e l e c t XMLForest ( ∗ )from course

NoteNot allowed on Oracle

Example (Shortest Possible)s e l e c t XMLForest ( co . ∗ )from course co

NoteNot allowed either!

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 23 / 73

Page 26: SQL/XML on Oracle

Rename Columns

Example (Rename Columns)s e l e c t XMLForest ( ID as ” i d ” ,

name as ” course−name” ,semester as ” semester ” ,descr as ” d e s c r i p t i o n ” )

from course

Example (The Result)< i d>4< / i d><course−name>OOP< / course−name><semester>3< / semester> . . .< i d>2< / i d><course−name>DB< / course−name><semester>7< / semester> . . .

NoteMust 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

Page 27: SQL/XML on Oracle

Added Outer Element

Example (A Tag for Each Row)s e l e c t XMLElement (

” course ” ,XMLForest ( ID as ” i d ” ,

name as ” course−name” ,semester as ” semester ” ,descr as ” d e s c r i p t i o n ” ) )

from course

Example (The Result)<course>< i d>4< / i d><course−name>OOP< / course−name> . . .< / course><course>< i d>2< / i d><course−name>DB< / course−name> . . .< / course>

NoteStill two rows being returned

The wrapping of each row

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 25 / 73

Page 28: SQL/XML on Oracle

Quick and Dirty, cont

Example (With Projection)s e l e c t 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)s e l e c t XMLForest ( i d as ” c id ” , name ” coursename ” )from course

Example (The Result)<c id>4< / c i d><coursename>OOP< / coursename><c id>2< / c i d><coursename>DB< / coursename>

NoteFiltering (selection) is possible in the where clause

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73

Page 29: SQL/XML on Oracle

Quick and Dirty, cont

Example (With Projection)s e l e c t 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)s e l e c t XMLForest ( i d as ” c id ” , name ” coursename ” )from course

Example (The Result)<c id>4< / c i d><coursename>OOP< / coursename><c id>2< / c i d><coursename>DB< / coursename>

NoteFiltering (selection) is possible in the where clause

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 26 / 73

Page 30: SQL/XML on Oracle

Quick and Dirty, cont I

Example (With Element per Row)s e l e c t XMLElement (name ” course ” ,

XMLForest ( i d as ” c id ” , name as ”cname” ) )from course

Example (The Result)<course><c id>4< / c i d><cname>OOP< / cname>< / course><course><c id>2< / c i d><cname>DB< / cname>< / course>

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 27 / 73

Page 31: SQL/XML on Oracle

Quick and Dirty, cont II

Example (With Root Element)s e l e c t XMLElement ( ” coursecata log ” ,

XMLAgg(XMLElement ( ” course ” ,

XMLForest ( i d as ” c id ” , name as ”cname” ) ) ) )from course

Example (The Result)<coursecata log>

<course><c id>4< / c i d><cname>OOP< / cname>< / course><course><c id>2< / c i d><cname>DB< / cname>< / course>

< / coursecata log>

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 28 / 73

Page 32: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 29 / 73

Page 33: SQL/XML on Oracle

XMLType Views

Example (Create an XML view on an existing table)create or rep lace view course xml o f XMLType wi th ob jec t i d( e x t r a c t

( ob jec t va lue , ’ / coursecata log / course / @id ’ ) . getnumberval ( ) )as s e l e c t XMLElement ( ” coursecata log ” ,

XMLAgg( XMLElement ( ” course ” ,XMLAtt r ibutes ( i d as ” i d ” ) ,

XMLElement ( ”name” , name) ,XMLElement ( ” semester ” , semester ) ,XMLElement ( ” desc ” , descr ) ) ) )

from course ;

NoteA create view statement

Use the SQL/XML publishing functions

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 30 / 73

Page 34: SQL/XML on Oracle

XQuery on Oracle I

Example (Get info on the OOP course)

XQUERYf o r $c i n ora : view ( ” course xml ” ) / coursecata log / coursewhere $c / name= ”OOP”r e t u r n $c

Example (Result)

column value<course i d = ” 4 ”><name>OOP< / name><semester>3< / semester><desc>Object−or ien ted Programming< / desc>

< / course>

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 31 / 73

Page 35: SQL/XML on Oracle

XQuery on Oracle II

Example (Count the number of courses)

XQUERYf o r $c i n ora : view ( ” course xml ” ) / coursecata logl e t $cour := count ( $c / course )r e t u r n $cour

Example (Result)

column value

2

NoteRemember ’/’ to execute from command line

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 32 / 73

Page 36: SQL/XML on Oracle

Summary: Tables to XML

SummarySQL/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/XMLStore 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

Page 37: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 34 / 73

Page 38: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 35 / 73

Page 39: SQL/XML on Oracle

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 XMLStore 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 XQueryConversion of XML to SQL

Retain pure SQL view on all data

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 36 / 73

Page 40: SQL/XML on Oracle

The coursecat Table

Example

id dsc exercises

11<course c id= ” 11 ”>

<name>Database< / name>< / course>

22<course c id= ” 22 ”>

<name>OOP< / name>< / course>

<exerc ises><exerc ise e id= ” 1 ”>

<desc>What i s OOP?< / desc><answer> I t . . .< / answer>

< / exerc ise>< / exerc ises>

23<course c id= ” 33 ”>

<name>SQL< / name>< / course>

<exerc ises><exerc ise e id= ” 1 ”>

<desc>What i s SQL?< / desc><answer> I t . . .< / answer>

< / exerc ise><exerc ise e id= ” 11 ”>

<desc>What i s a query?< / desc><answer> I t . . .< / answer>

< / exerc ise>< / exerc ises>

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 37 / 73

Page 41: SQL/XML on Oracle

Create Relational Schema

Example (Create the Table)create tab l e coursecat (

i d i n t pr imary key ,dsc xmltype not n u l l ,exerc ises xmltype

) ;

Example (Load the Data)i n s e r t i n t o coursecat values (22 ,’<course c id =”22”><name>OOP< /name>

</ course> ’ ,’<exerc ises><exerc ise e id =”1”>

<desc>What i s OOP?</desc><answer> I t . . . < / answer>

</ exerc ise></ exerc ises> ’) ;

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 38 / 73

Page 42: SQL/XML on Oracle

Say Hello

Example (Hello, World!)s e l e c t XMLQuery ( ’

l e t $s := ” Hel lo , World ! ”r e t u r n $s ’ r e t u r n i n g content ) as o

from dual ;

Example (Result)

o

Hello, World!

NoteThe 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

Page 43: SQL/XML on Oracle

Simple XPath

Example (Use SQL and XPath)s e l e c t id , xmlquery ( ’ / / exerc ise ’

passing exerc isesr e t u r n i n g content ) as res

from coursecat

Example (Result)

ID RES

1122 (XMLTYPE)23 (XMLTYPE)

NotePassing by value exercises

Return value is of the XMLType data type

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 40 / 73

Page 44: SQL/XML on Oracle

Extracting Values

Example (extractvalue Function)s e l e c t id ,

ex t r ac t va l ue ( dsc , ’ / / course / name / t e x t ( ) ’ ) as course namefrom coursecat

Example (Result)

ID COURSE NAME

11 Database22 OOP23 SQL

NoteThe 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

Page 45: SQL/XML on Oracle

Extracting Multiple Values

Example (XML to SQL)s e l e c t id ,

ex t r ac t va l ue ( dsc , ’ / / course / @cid ’ ) as course id ,ex t r ac t va l ue ( dsc , ’ / / course / name / t e x t ( ) ’ ) as course name

from coursecat

Example (Result)

ID COURSE ID COURSE NAME

11 11 Database22 22 OOP23 33 SQL

NoteAttributes and elements extract in the same mannerNow 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

Page 46: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 43 / 73

Page 47: SQL/XML on Oracle

Extracting Content Directly from XML

Example (XMLQuery Function)s e l e c t

xmlquery ( ’ f o r $doc i nfn : c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/DSC” )r e t u r n $doc ’ r e t u r n i n g content )

from dual

Example (Result)

XMLQUERY(. . .)

<course cid=”11”><name>Database</name>... </course>

NoteAll 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

Page 48: SQL/XML on Oracle

From SQL Directly

Example (XQUERY SQL∗Plus Keyword)XQUERYf o r $doc i n c o l l e c t i o n ( ” oradb : / PUBLIC /COURSECAT/ROW/EXERCISES” )where $doc / / exerc ise / @eid = 1r e t u r n $doc

Example (Result)

COLUMN VALUE

<exercises><exercise eid=”1”><desc>What is OOP?</desc>...

NoteXQUERY not XMLQuery

The collection is the fn:collection method

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 45 / 73

Page 49: SQL/XML on Oracle

Summary: XML Data Type

SummaryXMLQuery is part of the SQL standard

Allows full XQueryIn princip XQuery on XML file in Oracle!

Oracle as a pure XQuery engine

Supported by DB2 and OracleData 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

Page 50: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 47 / 73

Page 51: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 48 / 73

Page 52: SQL/XML on Oracle

The Overall IdeaThe Flow

XMLTable query <stuff></stuff>

NoteXMLTable is a part of SQL/XMLXMLTable returns a rowset

It is a table function

Overview SQL/XML Functions

SQL Clause SQL/XML Function

select XMLQueryfrom XMLTablewhere XMLExists

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 49 / 73

Page 53: SQL/XML on Oracle

The coursecat Table

Example

id dsc exercises

11<course c id= ” 11 ”>

<name>Database< / name>< / course>

22<course c id= ” 22 ”>

<name>OOP< / name>< / course>

<exerc ises><exerc ise e id= ” 1 ”>

<desc>What i s OOP?< / desc><answer> I t . . .< / answer>

< / exerc ise>< / exerc ises>

23<course c id= ” 33 ”>

<name>SQL< / name>< / course>

<exerc ises><exerc ise e id= ” 1 ”>

<desc>What i s SQL?< / desc><answer> I t . . .< / answer>

< / exerc ise><exerc ise e id= ” 11 ”>

<desc>What i s a query?< / desc><answer> I t . . .< / answer>

< / exerc ise>< / exerc ises>

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 50 / 73

Page 54: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 51 / 73

Page 55: SQL/XML on Oracle

From XML to SQL

Example (Get the Course Name)s e l e c t id , course namefrom coursecat ,

XMLTable ( ’ / course ’ passing dsccolumns course name varchar2 (10) path ’name ’ )

Example (Result)

ID COURSE NAME

11 Database22 OOP23 SQL

NoteThe 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

Page 56: SQL/XML on Oracle

More Advanced XPath

Example (Multiple XML Columns)s e l e c t id , course name , no exerc isesfrom coursecat ,

XMLTable ( ’ / course ’ passing dsccolumns course name varchar2 (10) path ’name ’ ) ,

XMLTable ( ’ / exerc ises ’ passing exerc isescolumns no exerc ises i n t path ’ count ( exerc ise ) ’ )

Example (Result)

ID COURSE NAME NO EXERCISES

22 OOP 123 SQL 2

NoteMultiple 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

Page 57: SQL/XML on Oracle

Using in the Where Clause

Example (Find where IDs are not Matching)s e l e c t id , course id , course namefrom coursecat ,

XMLTable ( ’ / course ’ passing dsccolumns course id i n t path ’ @cid ’ ,

course name varchar2 (10) path ’name ’ )where i d != course id

Example (Result)

ID COURSE ID COURSE NAME

23 33 SQL

NoteAttribute 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

Page 58: SQL/XML on Oracle

Ordinality and Default Value

Example (Find where IDs are not Matching)s e l e c t coursecat . id , num, exe rc i se id , authorfrom coursecat ,

XMLTable ( ’ / exerc ises / exerc ise ’passing exerc isescolumns

num f o r o r d i n a l i t y ,e x e r c i s e i d i n t path ’@eid ’ ,author varchar2 (30) path ’ author ’ d e f a u l t ’ Ib ’ )

Example (Result)

ID NUM EXERCISE ID AUTHOR

22 1 1 Ib23 1 1 Ib23 2 11 Ib

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 55 / 73

Page 59: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 56 / 73

Page 60: SQL/XML on Oracle

Information on Course ID and Name I

Examples e l e c t coursecat . id , x . course namefrom coursecat ,

XMLTable ( ’ / course ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / course / name ’ ) x

Example (Result)

id course name

11 Database22 OOP23 SQL

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 57 / 73

Page 61: SQL/XML on Oracle

Information on Course ID and Name II

Examples e l e c t coursecat . id , x . course namefrom coursecat ,

XMLTable ( ’ / course ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / course / name ’ ) x

NoteDisplays 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

Page 62: SQL/XML on Oracle

Information on Course ID, Name, and Exercises I

Examples e l e c t coursecat . id , course name , e x e r c i s e i dfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsc −− tab le −name . column−namecolumns

course name varchar2 (30) path ’ / / name ’ ) ,XMLTable ( ’ / ’

passing exerc ises −− only column namecolumns

e x e r c i s e i d i n t path ’ / exerc ises / exerc ise [ 1 ] / @eid ’ )

Example (Result)

id course name exercise id

22 OOP 123 SQL 1

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 59 / 73

Page 63: SQL/XML on Oracle

Information on Course ID, Name, and Exercises II

Examples e l e c t coursecat . id , course name , e x e r c i s e i dfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsc −− tab le −name . column−namecolumns

course name varchar2 (30) path ’ / / name ’ ) ,XMLTable ( ’ / ’

passing exerc ises −− only column namecolumns

e x e r c i s e i d i n t path ’ / exerc ises / exerc ise [ 1 ] / @eid ’ )

NoteMultiple 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

Page 64: SQL/XML on Oracle

Information on Course ID, Name, and Exercises, cont I

Examples e l e c t coursecat . id , course name , exe rc i se id , descr , answerfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / / name ’ ) ,XMLTable ( ’ / exerc ises / exerc ise ’

passing exerc isescolumns

e x e r c i s e i d 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

Page 65: SQL/XML on Oracle

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

Page 66: SQL/XML on Oracle

Information on Course ID, Name, and Exercises, cont III

Examples e l e c t coursecat . id , course name , exe rc i se id , descr , answerfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / / name ’ ) ,XMLTable ( ’ / exerc ises / exerc ise ’

passing exerc isescolumns

e x e r c i s e i d i n t path ’@eid ’ ,descr varchar2 (30) path ’ desc ’ ,answer varchar2 (30) path ’ answer ’ )

NoteChange 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

Page 67: SQL/XML on Oracle

Information on Course ID, Name, and Exercises, cont I

Examples e l e c t coursecat . id , course name , exe rc i se id , descr , answerfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / / name ’ )l e f t ou ter j o i n

XMLTable ( ’ / exerc ises / exerc ise ’passing exerc isescolumns

e x e r c i s e i d 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

Page 68: SQL/XML on Oracle

Information on Course ID, Name, and Exercises, cont II

Example (Result)

id course name exercise id descr answer

11 Database22 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

Page 69: SQL/XML on Oracle

Information on Course ID, Name, and Exercises, cont III

Examples e l e c t coursecat . id , course name , exe rc i se id , descr , answerfrom coursecat ,

XMLTable ( ’ / ’passing coursecat . dsccolumns

course name varchar2 (30) path ’ / / name ’ )l e f t ou ter j o i n

XMLTable ( ’ / exerc ises / exerc ise ’passing exerc isescolumns

e x e r c i s e i d i n t path ’@eid ’ ,descr varchar2 (30) path ’ desc ’ ,answer varchar2 (30) path ’ answer ’ ) on 1=1

NoteThe 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

Page 70: SQL/XML on Oracle

Filtering Based on XML Content I

Examples e l e c t x .∗from coursecat , XMLTable ( ’ / exerc ises / exerc ise ’

passing exerc isescolumns

e x e r c i s e i d 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 ...

NoteToo much information, i.e., no filtering

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 67 / 73

Page 71: SQL/XML on Oracle

Filtering Based on XML Content II

Examples e l e c t x .∗from coursecat , XMLTable ( ’ / exerc ises / exerc ise ’

passing exerc isescolumns

e x e r c i s e i d i n t path ’@eid ’ ,descr varchar2 (30) path ’ desc ’ ,answer varchar2 (30) path ’ answer ’ ) x

where XMLExists ( ’ / exerc ises / exerc ise [ @eid=11] ’ passing exerc ises )

Example (Result)

exercise id descr answer

1 What is SQL? It ...11 What is a query? It ...

NoteThe XMLExist for filtering in XML content

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 68 / 73

Page 72: SQL/XML on Oracle

Summary: XML to TablesSummary

The core function is XMLTableSupported on most DBMS

Explicit namingCannot guess SQL column names element/attributes

Explicit typingCannot guess SQL data types from XML document

Result of XMLTable can be joined like other tables!

LiteratureXMLTABLE 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 2On scredding large XML documents plus insert relational from XMLwww.ibm.com/developerworks/data/library/techarticle/

dm-0709nicola/Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 69 / 73

Page 73: SQL/XML on Oracle

Outline

1 Introduction

2 From Tables to XMLIntroductionSQL/XML Publishing FunctionsQuick and Dirty Publishing FunctionsXML View on Relational Data

3 The XML Data TypeIntroductionoradb: Protocol

4 From XML to TablesIntroductionA Simple ExampleA Longer Example

5 Summary

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 70 / 73

Page 74: SQL/XML on Oracle

SQL/XML vs. XPath/XQuery

SQL/XMLSQL centric

Small extension to SQL

null well understood

No implicit order

Bad support hierarchies

XPath/XQueryXML centric

New programming languages

Handling of null be aware!

Ordering (a sequence)

Excellent support hierarchies

NoteSQL/XML and XQuery serve different purposes

Are not competing technologies!

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73

Page 75: SQL/XML on Oracle

SQL/XML vs. XPath/XQuery

SQL/XMLSQL centric

Small extension to SQL

null well understood

No implicit order

Bad support hierarchies

XPath/XQueryXML centric

New programming languages

Handling of null be aware!

Ordering (a sequence)

Excellent support hierarchies

NoteSQL/XML and XQuery serve different purposes

Are not competing technologies!

Kristian Torp (Aalborg University) SQL/XML on Oracle November 26, 2015 71 / 73

Page 76: SQL/XML on Oracle

Summary

Main PointsSQL/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

StandardsSQL/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

Page 77: SQL/XML on Oracle

Additional Information

Web SitesGood SQL/XML tutorialwww.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 Centerwww.oracle.com/technology/tech/xml/index.html

Get off to a fast start with DB2 9 pureXMLhttp://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