Leviter XML DB
Leviter XML DB
Leviter XML DB
XML DB
with 11gR2 and SQL Developer 3.2.2
Coleman Leviter, OCP
Arrow Electronics
IT Software Systems Engineer
cleviter@ieee.org
NYOUG
Good Housekeeping
Cell phones AUDIBLE
Conversation INSIDE
Enjoy the presentation
NYOUG
Wanted
Speakers
NYOUG
CV
-
NYOUG
XPATH Terminology
VALUE takes as its argument a correlation variable (table alias)
associated with a row of an object table and returns object
instances stored in the object table. The type of the object
instances is the same type as the object table.
NYOUG
XPATH Terminology
XMLSEQUENCE - XMLSEQUENCE(XMLTYPE_instance)
XMLSEQUENCE operator is used to split multi-value results from
XMLTYPE queries into multiple rows.
NYOUG
Namespaces Definition
Why Namespaces http://www.w3.org/TR/REC-xml-names/
Such documents, containing multiple markup vocabularies, pose problems of
recognition and collision. Software modules need to be able to recognize the
elements and attributes which they are designed to process, even in the face
of "collisions" occurring when markup intended for some other software
package uses the same element name or attribute name.
These considerations require that document constructs should have names
constructed so as to avoid clashes between names from different markup
vocabularies. This specification describes a mechanism, XML namespaces,
which accomplishes this by assigning expanded names to elements and
attributes.
So, in simple terms, its a technique to distinguish two elements with the
same names from each other.
The technique allows one to import (or define) another fully qualified XML
document into an existing one without causing an element name collision.
NYOUG
10
NYOUG
11
NYOUG
12
(1_1)
QUANTITY PARENT
--------------- -----------2
1
3
1
1
1
4
2
1
2
6
2
6 rows selected.
NYOUG
13
(1_2)
14
(1_3)
15
Prettyprint or Indenting
16
NYOUG
17
NYOUG
18
NYOUG
19
CLOB; lcl_obj2
(1_5)
BEGIN
SELECT XMLTYPE.getclobval(XMLELEMENT("obj1:object",
xmlagg(xmlelement("obj1:thing",obj.things) )) )
INTO lcl_obj1 FROM myobject obj WHERE obj.parent = '1';
SELECT XMLTYPE.getclobval(XMLELEMENT("obj2:object",
XMLATTRIBUTES ('http://www.w3.org/2001/XMLSchema/obj_2' AS "xmlns:obj2"), /* obj2 */
XMLAGG(xmlelement("obj2:thing",obj.things) ) ) )
INTO lcl_obj2 FROM myobject obj WHERE obj.parent = '2';
SELECT ( '<?xml version="1.0" encoding="UTF-8"?>' ||
'<ns1:object_group
xmlns:ns1="http://www.w3.org/2001/XMLSchema/sample_namespace_1"
xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">'||
lcl_obj1|| lcl_obj2|| '</ns1:object_group>' )
dbms_output.put_line(lcl_full_xml);
END;
NYOUG
INTO lcl_full_xml
/* obj1 */
FROM dual;
20
(1_6_1)
Table: my_xml_table;
Column: xmlcol
XMLTYPE
Column: ref_id
NUMBER
<?xml version="1.0" encoding="UTF-8"?>
<ns1:object_group xmlns:ns1="http://www.w3.org/2001/XMLSchema/sample_namespace_1"
xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">
<obj1:object>
<obj1:thing>ball</obj1:thing>
<obj1:thing>key</obj1:thing>
<obj1:thing>table</obj1:thing>
</obj1:object>
<obj2:object xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2">
<obj2:thing>frisbee</obj2:thing>
<obj2:thing>bbq</obj2:thing>
<obj2:thing>switch</obj2:thing>
</obj2:object>
</ns1:object_group>
NYOUG
21
(1_7)
DECLARE
-- Cursor for parsing objects_group XML
CURSOR obj_cur
IS
SELECT EXTRACT (VALUE
(entire_things),'//*','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getstringval() AS lcl_thing
FROM my_xml_table mxt,
TABLE(xmlsequence(extract(mxt.xmlcol,
'//obj1:thing','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"'))) entire_things
WHERE mxt.ref_id = 1;
BEGIN
FOR obj_row IN obj_cur
LOOP
dbms_output.put_line('each element thing '|| obj_row.lcl_thing );
END LOOP;
END anonymous_block ;
each element thing <obj1:thing xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">ball</obj1:thing>
each element thing <obj1:thing xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">key</obj1:thing>
each element thing <obj1:thing xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">table</obj1:thing>
PL/SQL procedure successfully completed.
NYOUG
22
(1_8)
DECLARE
-- Cursor for parsing objects_group XML
CURSOR obj_cur
IS
SELECT EXTRACT (VALUE (entire_things),'//*/text()').getstringval () AS
lcl_thing
FROM my_xml_table mxt,
table(xmlsequence(extract(mxt.xmlcol, '*'))) entire_things
WHERE mxt.ref_id = 1;
BEGIN
FOR obj_row IN obj_cur LOOP
dbms_output.put_line('each element thing '|| obj_row.lcl_thing );
END LOOP;
END anonymous_block ;
SQL> /
each element thing ballkeytablefrisbeebbqswitch
PL/SQL procedure successfully completed.
NYOUG
23
(1_9)
XML Document
<objects>
<thing>ball</thing>
<thing>key</thing>
<thing>table</thing>
</objects>
<thing>key</thing>
<thing>table</thing>
NYOUG
24
(1_10)
XML Document
<objects>
<thing>ball</thing>
========================================
Results
This Column
---------------------------------------------------------------------key
NYOUG
25
(1_11)
SQL> select
value(tab).extract('//obj2:thing/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchem
a/obj_1"').getStringVal() "This_Column",
value(tab).extract('//obj1:thing/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchem
a/obj_1"').getStringVal() "That_Column"
26
(1_11)
value(tab).extract('//obj2:thing/text()',
'xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringVal()
"This_Column",
*
ERROR at line 1:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
27
(1_12)
1 select
value(tab).extract('//obj1:thing/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringVal()
"This_Column",
value(tab).extract('//obj1:thing/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringVal()
"That_Column"
3 from table ( XMLSequence(extract
4 (XMLType('<?xml version="1.0" encoding="UTF-8"?><ns1:object_group
xmlns:ns1="http://www.w3.org/2001/XMLSchema/sample_namespace_1"
xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">
5 <obj1:object><obj1:thing>ball</obj1:thing><obj1:thing>key</obj1:thing>
6 <obj1:thing>table</obj1:thing></obj1:object>
7 <obj2:object xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2">
8 <obj2:thing>frisbee</obj2:thing>
9 <obj2:thing>bbq</obj2:thing>
10 <obj2:thing>switch</obj2:thing>
11 </obj2:object>
12* </ns1:object_group>'),'*')
NYOUG
) ) tab
28
(1_12)
That_Column
-------------------- -------------------ballkeytable
NYOUG
ballkeytable
29
XML Example 1
(1_13)
select value(tab).extract('//obj1:thing/text()',
'xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringVal()
"This_Column",
value(tab).extract('//obj2:thing/text()',
'xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2"').getStringVal()
"That_Column"
4
from
table ( XMLSequence(extract
<obj1:thing>table</obj1:thing></obj1:object>
<obj2:object xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2">
<obj2:thing>frisbee</obj2:thing>
10
<obj2:thing>bbq</obj2:thing>
11
<obj2:thing>switch</obj2:thing>
12
</obj2:object>
13* </ns1:object_group>'),'*')
NYOUG
) tab
30
XML Example 2
(1_13)
SQL> /
This_Column
That_Column
-----------------
--------------------
ballkeytable
frisbeebbqswitch
NYOUG
31
(1_14_2)
SQL> select
value(tab).extract('//*/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringV
al() "This_Column",
value(tab).extract('//obj2:thing/text()','xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2"').g
etStringVal() "That_Column"
3 from table ( XMLSequence(extract
13 </ns1:object_group>'),'/*')
NYOUG
) ) tab;
32
(1_14_2)
That_Column
NYOUG
33
(1_15)
SQL> select
value(tab).extract('//obj1:thing[2]/text()','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getSt
ringVal()"This_Column",
value(tab).extract('//obj2:thing/text()','xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2"').getStrin
gVal() "That_Column"
3 from table ( XMLSequence(extract
4 (XMLType('<?xml version="1.0" encoding="UTF-8"?>
5 <ns1:object_group xmlns:ns1="http://www.w3.org/2001/XMLSchema/sample_namespace_1"
xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1">
6 <obj1:object><obj1:thing>ball</obj1:thing><obj1:thing>key</obj1:thing>
7 <obj1:thing>table</obj1:thing></obj1:object>
8 <obj2:object xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2">
9 <obj2:thing>frisbee</obj2:thing>
10 <obj2:thing>bbq</obj2:thing>
11 <obj2:thing>switch</obj2:thing>
12 </obj2:object>
13 </ns1:object_group>'),'/*')
NYOUG
) ) tab;
34
(1_15)
That_Column
---------------------------------------- ---------------------------------------key
NYOUG
frisbeebbqswitch
35
(1_16_3)
OBJ_EXAMP
---------------------------------------ballkeytablefrisbeebbqswitch
NYOUG
36
(1_16_3)
37
(1_17)
SELECT extract(value(t),'//obj1:thing/text()',
'xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"').getStringV
al() AS obj_example
FROM my_xml_table,
table(xmlsequence(extract(xmlcol, '*'))) t
WHERE ref_id = 1
OBJ_EXAMPLE
---------------------ballkeytable
NYOUG
38
(1_18)
DECLARE
39
(1_18)
BEGIN
END LOOP;
END anonymous_block ;
each element thing ball
NYOUG
40
UPDATEXML 1st
(1_19_2)
NYOUG
41
UPDATEXML 1 2nd
(1_19_2)
-- CHANGE TO CAPS
NYOUG
42
UPDATEXML 2 2nd
(1_19_2)
NYOUG
43
(1_20)
-- Anonymous Block
DECLARE
NYOUG
44
(1_20)
<obj1:object>
<obj1:thing>ball</obj1:thing>
<obj1:thing>key</obj1:thing>
<obj1:thing>table</obj1:thing>
</obj1:object>
<obj2:object xmlns:obj2="http://www.w3.org/2001/XMLSchema/obj_2">
<obj2:thing>frisbee</obj2:thing>
<obj2:thing>bbq</obj2:thing>
<obj2:thing>switch</obj2:thing>
</obj2:object>
</ns1:object_group>'),
'//obj1:thing','xmlns:obj1="http://www.w3.org/2001/XMLSchema/obj_1"' )
entire_things;
NYOUG
) )
45
(1_20)
BEGIN
46
(1_21)
Definitions
contains operator is used when index is of type
context
Demo
NYOUG
47
DBMS_XML.CONVERT
(1_22_3)
48
DBMS_XMLDOM
(1_23)
NYOUG
49
DBMS_XMLDOM
(1_24) (contd)
NYOUG
50
XML Header
CLOB
XML Header
pl/sql
CLOB
XML Header
CLOB
XML Header
CLOB
NYOUG
51
XML Header
CLOB
XML Header
CLOB
XML Header
pl/sql
CLOB
XML Header
CLOB
XML Header
CLOB
NYOUG
52
XML Packet 2
pl/sql
Limit
Data
Per
Packet
XML Packet 3
XML Packet 4
XML Packet n
NYOUG
53
`
XML Packet 1
XML Packet 2
XML Packet 3
pl/sql
XML Packet 4
XML Packet 5
NYOUG
54
XQUERY
(example)
}</result>'
returning content) aa from dual
NYOUG
55
Bonus
This presentation
LibreOffice The Document Foundation
Vers 4.0.0.3
Website: http://www.libreoffice.org/#0
NYOUG
56
References 1
Design Tools: XMLSPY 30 day free trial
http://www.altova.com/
Oracle XML DB Docs: full implementation plus extensions of XML
http://www.oracle.com/technetwork/database/features/xmldb/index.html
57
References 2
W3.org
W3schools.com
Oracle Database 10g XML & SQL: Design, Build, & Manage XML
Applications in Java, C, C++, & PL/SQL (Osborne ORACLE Press
Series) (Paperback) by Mark Scardina (Author), Ben Chang
(Author), Jinyu Wang (Author)
http://docs.oracle.com/javase/tutorial/reallybigindex.html
iso.org
Sql Developer 3.2.2 - 12c Database Support
http://www.oracle.com/technetwork/developer-tools/sqldeveloper/sqldev-newfeatures32-1731840.html
NYOUG
58
Please Note
Speaker: Coleman Leviter
Topic: A HitchhikersGuide Integrating Oracle XML DB with
11gR2 and SQL Developer 3.2.2
Contact:
cleviter@ieee.org
NYOUG
59
Questions
NYOUG
60