Igor's Blog
Programming, DIY, Games, Hacks, and Tech

I really like the Oracle XMLTABLE feature, which allows you to treat an XML string as if it were a real table. I've been using this feature quite a bit lately and had some difficulties initially around the default namespace so I thought I'd share my findings.

Lets say I had an XML document that looked like this...
 XML
<parent id="123" xmlns="http://igorkromin.net/">
<child id="abc">
<anotherElement>some text</anotherElement>
</child>
</parent>


Now for the purposes of this article I will simply select this from dual like below, however the same approach will work when the data is stored in a real table.
 SQL
select
123 as my_id,
'<ik:parent id="123" xmlns:ik="http://igorkromin.net/">' ||
' <ik:child id="abc">' ||
' <ik:anotherElement>some text</ik:anotherElement>' ||
' </ik:child>' ||
'</ik:parent>' as xml_data
from dual;


The result looks like this...
xmltable1.png




Now lets say I wanted my result to look like this instead...
xmltable2.png


With XMLTABLE you can. The trick is to set the default namespace, otherwise no data will be returned. This only seems to be the case when the XML query string is a non-root node e.g. '/parent' in my case. I found when selecting the root node e.g. '/' the default namespace doesn't matter.

The query below essentially converts values in the XML document to columns that can be used with the rest of the SQL.
 SQL
SELECT
t.my_id,
x.xml_col1,
x.xml_col2,
x.xml_col3
FROM
(
select
123 as my_id,
'<ik:parent id="123" xmlns:ik="http://igorkromin.net/">' ||
' <ik:child id="abc">' ||
' <ik:anotherElement>some text</ik:anotherElement>' ||
' </ik:child>' ||
'</ik:parent>' as xml_data
from dual
) t,
XMLTABLE(
xmlnamespaces(default 'http://igorkromin.net/'),
'/parent'
PASSING XMLType(t.xml_data)
COLUMNS xml_col1 varchar2(100) PATH [email protected]',
xml_col2 varchar2(100) PATH [email protected]',
xml_col3 varchar2(100) PATH 'child/anotherElement/text()'
) x
;


Incidentally if the XML was specifying the default xmlns namespace instead of the xmlns:ik namespace, the same XMLTABLE query would work too.

-i

comments powered by Disqus
Other posts you may like...

Recent Blog Posts

A hack to create an uber jar with the Maven Shade Plugin using local jar files

Is it worth it? Apple USB-C Digital Multi AV adapter vs a cheap eBay clone

Running X11 graphical applications after changing to another user using 'sudo su'

How to stop Facebook using ad images as post sharing thumbnails

Picking lists for Atari Lynx capacitor replacement kits

Performance - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Look and feel - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Using math to work out the diameter of hard to reach water cooling pipes

How to get the unreachable shard in the Hinterlands in DragonAge Inquisition

Measy RC12 wireless 2.4Ghz keyboard touchpad review

Recent Galleries

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Don't forget to
me for more great articles!
Don't show this again