Skip to content
Advertisement

How to select values between a XML tag in SQL Query

I have a table with CLOB column storing a XML. The structure of XML is unreadable. I want to get values between few tags like <DOMAINID>; sample is shown below.

XML:

I am using this:

Expected result:

Actual XML:

Advertisement

Answer

Do not use regular expressions to parse XML; use a proper XML parser.

However, what you have is not properly formed XML as it is missing a root element and you are missing the / in all of the closing tags; so you first need to fix your XML and give it a root element and then you can parse it using an XML parser.

Which, for the sample data:

Outputs:

DOMAIN | DOMAINID | GP   | RSN 
:----- | :------- | :--- | :---
IND    | 112AC    | ASIA | GOOD

If you just want a single value then you can use XMLQUERY:

Which outputs:

| DOMAINID |
| :------- |
| 112AC    |

db<>fiddle here


Update

I am going to assume that your XML also defines the xsi and busHdr namespaces (if it doesn’t then Oracle will fail to parse the XML as it does not know what those namespaces are); that would give you this sample data:

Then, you just need to add the namespace that you are using and update the paths to the new (case-sensitive) locations:

Outputs:

DOMAIN   | DOMAINID  | VERSION | GROUPID | REASON | DT       
:------- | :-------- | ------: | :------ | :----- | :--------
ssdsgdsg | 123456ACC |       1 | 3424234 | MANUAL | 01-FEB-21

db<>fiddle here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement