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

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, and Tech

Recent Blog Posts

Converting a CSV file containing commas in quotes to a pipe separated file using Awk and Bash

How to get Facybox gallery popup to work for images with dynamic URLs

jQuery UI Photoboxr plugin updated to version 0.2

Learn How to Add Disclaimer to All Outgoing Emails in Office 365

How to securely host your web site for free with Google Cloud and CloudFlare (part 3)

How to securely host your web site for free with Google Cloud and CloudFlare (part 2)

How to securely host your web site for free with Google Cloud and CloudFlare (part 1)

Beware of the subtle difference in date format strings between MySQL and SQLite

Postman reporting Unexpected 'N' error in response body

Adding extra shelves to the Ikea Detolf display cabinet

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

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
Please follow me for more great content!
     
Don't show this again