Oracle XMLTable
Version 12.1.0.2

General Information
Library Note Morgan's Library Page Header
The Library is currently in the process of being upgraded from Oracle Database Version to 12.2.0.1 and version 18.0 is going to be available soon. Demos are being upgraded to reflect the new Container paradigm as well as EBR (Edition Based Redefinition) and may contain references to CDBs, PDBs, and other objects you may not be familiar with such as CDB_OBJECTS_AE: Welcome to 12c and beyond.

Have you viewed the Library's main Table of Contents? If not ... [Click Here] ... scroll down the page and you will find links to the largest collection of Oracle Database demos on the net.
Purpose XMLQuery returns query results as XML. XMLTable returns results as relational data.
 
Create Schema
Create Demo Table conn uwclass/uwclass@pdbdev

CREATE TABLE person_data (
person_id   NUMBER(3),
person_data sys.XMLTYPE);

desc person_data
Load Demo Data INSERT INTO person_data
(person_id, person_data)
VALUES
(1, XMLTYPE('
 <PDRecord>
   <PDName>Daniel Morgan</PDName>
   <PDDOB>12/1/1951</PDDOB>
   <PDEmail>damorgan@u.washington.edu</PDEmail>
 </PDRecord>'
)
);

INSERT INTO person_data
(person_id, person_data)
VALUES
(2, XMLTYPE('
 <PDRecord>
   <PDName>Julian Dyke</PDName>
   <PDDOB>5/17/1949</PDDOB>
   <PDEmail>damorgan@u.washington.edu</PDEmail>
 </PDRecord>')
);

INSERT INTO person_data
(person_id, person_data)
VALUES
(3, XMLTYPE('
 <PDRecord>
   <PDName>Caleb Small</PDName>
   <PDDOB>1/1/1960</PDDOB>
   <PDEmail>damorgan@u.washington.edu</PDEmail>
 </PDRecord>')
);

COMMIT;

set long 100000
col person_data format a60

SELECT * FROM person_data;
Simple XMLTABLE Query (with equals) SELECT <column_list>, XMLQuery (
'for $i IN <record_end_tag>
 where $i<item_end_tag> = <value>
 order by $i<item_end_tag>
 return $i<item_end_tag>
PASSING BY VALUE <xml_record_column>
RETURNING CONTENTS) <returning_column_alias>
FROM <table_name>;


Note: What is within the parentheses is case sensitive and you can not use Upper Case or InitCap for commands.
col column_value format a50

SELECT t.column_value
FROM person_data a,
  XMLTABLE (
    'for $root in $date
     where $root/PDRecord/PDName = "Daniel Morgan"
     return $root/PDRecord/PDDOB/text()'
  passing a.person_data AS "date") t;

Related Topics
DBMS_XMLGEN
XML Functions
XMLQuery
XML Tables
What's New In 12cR1
What's New In 12cR2

Morgan's Library Page Footer
This site is maintained by Dan Morgan. Last Updated: This site is protected by copyright and trademark laws under U.S. and International law. © 1998-2017 Daniel A. Morgan All Rights Reserved