Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL/XML on Oracle

528 views

Published on

Converting tables to XML documents and vice versa on the Oracle RDBMS:

Published in: Technology
  • Be the first to comment

  • Be the first to like this

SQL/XML on Oracle

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

×