Igor's Blog

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

, ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
StrFunc() Online
300 George St Blogumentary
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Binding extra variables with PHP PDO returns no results

New Nintendo 3DS XL, R4i SDHC RTS Lite and DSDoom

Older versions of JD-GUI crashing on macOS Sierra

How does Seeed Fusion stack up as a low budget PCB fab

Where to find the last item (Robbie's lost lyrics) in GravityFalls Lake level

Detecting print requests to print contents of a DIV with JavaScript

Using Google DFP with AdSense on responsive pages

How to change the email subject line in Gmail

Force PHP GD library to load JPEG images that have invalid data

How to uninstall Samsung Smart Switch from your Mac

Recent Galleries

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

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again