how to get data from different levels in a xmltable? - sql

I'm trying to get the values of two attributes from table MVR_DTL in column VENDOR_XML. VENDOR_XML is of datatype clob and contains an xml that looks like this
<MVRCHPINFF_1.0>
<Routing ReplyToQMgr="PQ21" ReplyToQ="A4218QA.BIZTALK.REPLY.REPORT.PROD" CorelId="712393102361590" MsgType="8" Expiry="-1" MsgID="201904051632015"></Routing>
<MVRRecLoop>
<CLoop>
<CRec>
<C_MVRNumberAddr>ROMAN GENERAL</C_MVRNumberAddr>
</CRec>
<CRec>
<C_MVRNumberAddr>ROMAN ST</C_MVRNumberAddr>
</CRec>
<CRec>
<C_MVRNumberAddr>ROMAN CITY, ROME 111111</C_MVRNumberAddr>
</CRec>
</CLoop>
<HIJLoop>
<JRec>
<J_SVCDesc>MVR RECORD CLEAR</J_SVCDesc>
</JRec>
</HIJLoop>
</MVRRecLoop>
</MVRCHPINFF_1.0>
I tried running
SELECT c.J_SVCDesc, c.XMLDetails from MVR_DTL M,
XMLTABLE(
'string-join(/MVRCHPINFF_1.0/MVRRecLoop/CLoop/CRec/C_MVRNumberAddr, "|")'
passing XMLTYPE(M.VENDOR_XML)
columns XMLDetails varchar2(200) PATH '.',
J_SVCDesc varchar2(50) PATH './../../../HIJLoop/JRec/J_SVCDesc') c;
and i get this error
Error during Execute
S1000(19112)[Oracle][ODBC][Ora]ORA-19112: error raised during evaluation:
XVM-01020: [XPTY0020] The path step context item is not a node
I also tried
SELECT x1.J_SVCDesc, x2.XMLDetails from MVR_DTL M,
XMLTABLE('/MVRCHPINFF_1.0/MVRRecLoop'
passing XMLTYPE(M.VENDOR_XML)
columns
Address XMLTYPE path './CLoop/CRec/C_MVRNumberAddr',
J_SVCDesc varchar(50) PATH './HIJLoop/JRec/J_SVCDesc') x1
CROSS JOIN XMLTable(
'string-join(., "|")'
PASSING x1.Address
COLUMNS XMLDetails varchar2(200) PATH '.') x2;
but errored out with
Error during Execute
S1000(19279)[Oracle][ODBC][Ora]ORA-19279: XPTY0004 - XQuery dynamic type mismatch:
expected singleton sequence - got multi-item sequence
I'm trying to get
J_SVCDESC XMLDETAILS
MVR RECORD CLEAR ROMAN GENERAL|ROMAN ST|ROMAN CITY, ROME 111111
Could someone help me figure out what I'm missing.

You can move the string-join down to the columns clause:
select x.j_svcdesc, x.xmldetails
from mvr_dtl m
cross join xmltable (
'/MVRCHPINFF_1.0/MVRRecLoop'
passing xmltype(m.vendor_xml)
columns J_SVCDesc varchar2(50) path 'HIJLoop/JRec/J_SVCDesc',
xmldetails varchar2(200) path 'string-join(CLoop/CRec/C_MVRNumberAddr, "|")'
) x
Demo with your sample data in a CTE:
with mvr_dtl (vendor_xml) as (
select to_clob('<MVRCHPINFF_1.0>
<Routing ReplyToQMgr="PQ21" ReplyToQ="A4218QA.BIZTALK.REPLY.REPORT.PROD" CorelId="712393102361590" MsgType="8" Expiry="-1" MsgID="201904051632015"></Routing>
<MVRRecLoop>
<CLoop>
<CRec>
<C_MVRNumberAddr>ROMAN GENERAL</C_MVRNumberAddr>
</CRec>
<CRec>
<C_MVRNumberAddr>ROMAN ST</C_MVRNumberAddr>
</CRec>
<CRec>
<C_MVRNumberAddr>ROMAN CITY, ROME 111111</C_MVRNumberAddr>
</CRec>
</CLoop>
<HIJLoop>
<JRec>
<J_SVCDesc>MVR RECORD CLEAR</J_SVCDesc>
</JRec>
</HIJLoop>
</MVRRecLoop>
</MVRCHPINFF_1.0>')
from dual
)
select x.j_svcdesc, x.xmldetails
from mvr_dtl m
cross join xmltable (
'/MVRCHPINFF_1.0/MVRRecLoop'
passing xmltype(m.vendor_xml)
columns J_SVCDesc varchar2(50) path 'HIJLoop/JRec/J_SVCDesc',
xmldetails varchar2(200) path 'string-join(CLoop/CRec/C_MVRNumberAddr, "|")'
) x;
J_SVCDESC XMLDETAILS
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MVR RECORD CLEAR ROMAN GENERAL|ROMAN ST|ROMAN CITY, ROME 111111
If the HIJLoop node name suggests there could be multiple JRec values too then you could concatenate those too:
columns J_SVCDesc varchar2(50) path 'string-join(HIJLoop/JRec/J_SVCDesc, "|")',
xmldetails varchar2(200) path 'string-join(CLoop/CRec/C_MVRNumberAddr, "|")'
which makes no difference to the output with the sample XML.
db<>fiddle
Incidentally, your second attempt sort of works; it doesn't error but doesn't get the right result either. The address you pass is an XML fragment with just sibling nodes, and the string-join only sees one value, consisting of the text() from those (I think... something like that). If you pass the CLoop down instead and expand the second XPath then it works:
select x1.j_svcdesc, x2.xmldetails
from mvr_dtl m
cross join xmltable (
'/MVRCHPINFF_1.0/MVRRecLoop'
passing xmltype(m.vendor_xml)
columns J_SVCDesc varchar(50) path 'HIJLoop/JRec/J_SVCDesc',
HIJLoop xmltype path 'CLoop'
) x1
cross join xmltable (
'string-join(CLoop/CRec/C_MVRNumberAddr, "|")'
passing x1.HIJLoop
columns xmldetails varchar2(200) path '.'
) x2;
db<>fiddle
But if you were actually getting "ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence" then I suspect you have data that does actually have multiple JRec nodes; in which case see my second query above.
db<>fiddle showing that issue with this approach and my first query; and it working with my second query. So you probably need to use that:
select x.j_svcdesc, x.xmldetails
from mvr_dtl m
cross join xmltable (
'/MVRCHPINFF_1.0/MVRRecLoop'
passing xmltype(m.vendor_xml)
columns J_SVCDesc varchar2(50) path 'string-join(HIJLoop/JRec/J_SVCDesc, "|")',
xmldetails varchar2(200) path 'string-join(CLoop/CRec/C_MVRNumberAddr, "|")'
) x;
You might need to increase the returned column sizes, too.

Related

How Extract from XMLTYPE column, corresponding label for value in a tag within a value/label list from same XML

I got a sql from an Oracle table with a XMLTYPE column.
The Xml has a tag with a value '2' and in the same xml there is the list with value/label pairs like this:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<characteristics>
<motor>
<fuel>2</fuel>
</motor>
</characteristics>
<ValuesList>
<CarValues>
<CarValue>
<value>1</value>
<label>Diesel</label>
</CarValue>
<CarValue>
<value>2</value>
<label>Unleaded petrol</label>
</CarValue>
</CarValues>
</ValuesList>
</Root>
With extract() or extractValue() functions it's easy to get the value for tag with any of these staments a, b
SELECT extract(t.xmlColumn, '//fuel/text()').getStringVal() a,
extractValue(t.xmlColumn, '//fuel') b
FROM Table t
The problem is I want to get the label text for value '2' from Valueslist 'Unleaded petrol'
I try to get that value with a XPATH expresion like this:
extractValue(t.xmlColumn, '//CarValue/label[../value=//fuel]')
The XPATH has been evaluated with Notepad XML Tools and it works, but there's no way to get any result. it's always null.
Any idea how to achive this? I don't know how to use XMLTABLE or any other solution to this case.
You need to include the /text() part in your attempt:
SELECT
extract(t.xmlColumn,'//fuel/text()').getStringVal() a
,extractValue(t.xmlColumn,'//fuel') b
,extractValue(t.xmlColumn, '//CarValue/label[../value=//fuel/text()]') c
FROM your_table t
A
B
C
2
2
Unleaded petrol
In your version you're trying to match a label with a value of the node <fuel>2</fuel>, not the text within than node, 2.
Both extract() and extractValue() are deprecated, so you could use XMLQuery instead:
SELECT
XMLQuery('//fuel/text()' passing xmlColumn returning content) a
,XMLQuery('//CarValue/label[../value=//fuel/text()]/text()' passing xmlColumn returning content) a
FROM your_table t
A
A
2
Unleaded petrol
Or with XMLTable();
SELECT x.a, x.b
FROM your_table t
CROSS APPLY XMLTable(
'/'
passing t.xmlColumn
columns a number path '//fuel',
b varchar2(30) path '//CarValue/label[../value=//fuel/text()]'
) x
A
B
2
Unleaded petrol
fiddle
If your version of Oracle isn't working with the node traversal using ../ then you could do it the hard way by getting the fuel value and all the id/label pairs with separate XMLTable calls, and then filtering those that match:
SELECT x1.fuel, x2.label
FROM your_table t
CROSS JOIN XMLTable(
'//fuel'
passing t.xmlColumn
columns fuel number path '.'
) x1
JOIN XMLTable(
'//CarValue'
passing t.xmlColumn
columns value number path 'value',
label varchar2(30) path 'label'
) x2 ON x2.value = x1.fuel
FUEL
LABEL
2
Unleaded petrol
fiddle
All solutions Alex Poole gave are accepted solutions for the question case title where "a value/label list within same XML" is present.
Just for information, in case "a value/label list is not present in the same XML", solution is create an auxiliary XMLTable with a XMLTYPE field and the value/label list XML in it.
SELECT x1.fuel, x2.labelFROM your_table t
CROSS JOIN XMLTable(
'//fuel'
passing t.xmlColumn
columns fuel number path '.'
) x1
LEFT JOIN XMLTable(
'//CarValue'
passing
(SELECT XMLTYPE('
<ValuesList>
<CarValues>
<CarValue>
<value>1</value>
<label>Diesel</label>
</CarValue>
<CarValue>
<value>2</value>
<label>Unleaded petrol</label>
</CarValue>
</CarValues>
</ValuesList>
') FROM dual)
columns value number path 'value',
label varchar2(30) path 'label'
) x2 ON x2.value = x1.fuel

XMLQuery to get data

I have the following SQL :
with q1 (Tdata) as (
SELECT XMLtype(transportdata, nls_charset_id('AL32UTF8'))
from bph_owner.paymentinterchange pint
where --PINT.INCOMING = 'T' and
PINT.TRANSPORTTIME >= to_date('2022-08-10', 'yyyy-mm-dd')
and pint.fileformat = 'pain.001.001.03'
)
--select XMLQuery('//*:GrpHdr/*:InitgPty/Nm/text()'
select tdata, XMLQuery('//*:GrpHdr/*:CtrlSum/text()'
passing Tdata
returning content).getstringval()
,
XMLQuery('//*:GrpHdr/*:MsgId/text()'
passing Tdata
returning content).getstringval()
from q1;
This works but for the InitgPty/Nm/ it doesn't - anybody know how I can extract this information?
Please be gentle as I don't work with XML much.
Thanks
Based on sample data from a previous question and other relating to this XML schema, it appears you just need to wildcard the namespace for Nm node, as well as its parents:
'//*:GrpHdr/*:InitgPty/*:Nm/text()'
so the query could become:
with q1 (Tdata) as (
SELECT XMLtype(transportdata, nls_charset_id('AL32UTF8'))
from paymentinterchange pint
where --PINT.INCOMING = 'T' and
PINT.TRANSPORTTIME >= to_date('2022-08-10', 'yyyy-mm-dd')
and pint.fileformat = 'pain.001.001.03'
)
select
XMLQuery('//*:GrpHdr/*:InitgPty/*:Nm/text()'
passing Tdata
returning content).getstringval() as nm
,
XMLQuery('//*:GrpHdr/*:CtrlSum/text()'
passing Tdata
returning content).getstringval() as ctrlsum
,
XMLQuery('//*:GrpHdr/*:MsgId/text()'
passing Tdata
returning content).getstringval() as msgig
from q1;
But if you're pulling multiple values out of the same document then it's simpler to use XMLTable rather than multiple XMLQuery calls:
select x.nm, x.ctrlsum, x.msgid
from paymentinterchange pint
cross apply XMLTable(
'//*:GrpHdr'
passing XMLtype(pint.transportdata, nls_charset_id('AL32UTF8'))
columns nm varchar2(50) path '*:InitgPty/*:Nm',
ctrlsum number path '*:CtrlSum',
msgid varchar2(50) path '*:MsgId'
) x
where pint.transporttime >= date '2022-08-10'
and pint.fileformat = 'pain.001.001.03';
db<>fiddle with data adapted from a previous question.
Before Oracle 12c you can use cross join instead of cross apply. I don't think it would make a difference in later versions either, that just seems to be my default now...
select x.nm, x.ctrlsum, x.msgid
from paymentinterchange pint
cross join XMLTable(
'//*:GrpHdr'
passing XMLtype(pint.transportdata, nls_charset_id('AL32UTF8'))
columns nm varchar2(50) path '//*:InitgPty/*:Nm',
ctrlsum number path '//*:CtrlSum',
msgid varchar2(50) path '//*:MsgId'
) x
where pint.transporttime >= date '2022-08-12'
and pint.fileformat = 'pain.001.001.03';
db<>fiddle showing it in 11.2.0.2, where it seems to need the // at the start of all the paths, including in the columns clause - those aren't needed in later versions, but here it returns nulls without them.

How to return values in xml with different xpath

I am trying to parse xml in my oracle DB to return individual rows. I have different xpath the ,downPaymentSourceType INT PATH '/downPaymentSource/downPaymentSourceTypeDd' and ,DownpaymentAmount INT PATH '/downPaymentSource/amount' are returning nulls. How do I handle this to provide accurate results. Thanks
WITH t( xml ) AS
(
SELECT XMLType('<loanApplication xmlns="http://www.abcdef.com/Schema/FCX/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<deal>
<applicationDate>2017-01-10T16:15:00-05:00</applicationDate>
<applicationId>QA-8899</applicationId>
<combinedGds>0.13055</combinedGds>
<combinedLtv>1.8</combinedLtv>
<combinedTds>10.2714</combinedTds>
<dealPurposeDd>2</dealPurposeDd>
<dealTypeDd>0</dealTypeDd>
<downPaymentSource>
<amount>25000.0</amount>
<downPaymentSourceTypeDd>1</downPaymentSourceTypeDd>
<description>aasdadsf</description>
</downPaymentSource>
<downPaymentSource>
<amount>25000.0</amount>
<downPaymentSourceTypeDd>6</downPaymentSourceTypeDd>
<description>wrewertwerewrt</description>
</downPaymentSource>
<downPaymentSource>
<amount>50000.0</amount>
<downPaymentSourceTypeDd>10</downPaymentSourceTypeDd>
<description>second and thirs</description>
</downPaymentSource>
<estimatedClosingDate>2017-12-05T00:00:00-05:00</estimatedClosingDate>
<financingWaiverDate>2017-01-04T00:00:00-05:00</financingWaiverDate>
<refiImprovementAmount>10000.0</refiImprovementAmount>
<refiImprovementsDesc>roof</refiImprovementsDesc>
<refiImprovementsFlag>Y</refiImprovementsFlag>
<refiPurpose>refi purpose</refiPurpose>
<taxPayorDd>1</taxPayorDd>
<additionalData dataType="String" name="firmLicenseRegistrationNumber" xmlns:t="http://www.abcdef.com/Schema/FCX/1">C000312345</additionalData>
</deal>
<applicantGroup>
<applicantGroupTypeDd>0</applicantGroupTypeDd>
<applicant>
<employmentHistory>
<employerName>employer name</employerName>
<employmentHistoryStatusDd>0</employmentHistoryStatusDd>
<employmentHistoryTypeDd>0</employmentHistoryTypeDd>
<income>
<annualIncomeAmount>200000.0</annualIncomeAmount>
<incomeAmount>200000.0</incomeAmount>
<incomePeriodDd>0</incomePeriodDd>
<incomeTypeDd>6</incomeTypeDd>
</income>
</employmentHistory>
<asset>
<assetDescription>qwerqwer</assetDescription>
<assetTypeDd>0</assetTypeDd>
<assetValue>1111.0</assetValue>
</asset>
<liability>
<broker>
<creditLimit>9283.0</creditLimit>
<liabilityAmount>9999999.8</liabilityAmount>
<liabilityMonthlyPayment>299999.99</liabilityMonthlyPayment>
<liabilityTypeDd>2</liabilityTypeDd>
</broker>
</liability>
</applicant>
<applicant>
<employmentHistory>
<income>
<annualIncomeAmount>150000.0</annualIncomeAmount>
<incomeAmount>150000.0</incomeAmount>
<incomeDescription>income description</incomeDescription>
<incomePeriodDd>0</incomePeriodDd>
<incomeTypeDd>6</incomeTypeDd>
</income>
</employmentHistory>
</applicant>
</applicantGroup>
</loanApplication>')
FROM dual
)
SELECT
JSON_OBJECT (
KEY 'dealClosingDate' value x.DealClosingDate
,KEY 'dealPurpose' value x.dealPurpose
,KEY 'combinedGds' value x.combinedGds
,KEY 'combinedTds' value x.combinedTds
,KEY 'combinedLtv' value x.combinedLtv
,KEY 'downpaymentAmount' value x.DownpaymentAmount
,KEY 'combinedAssets' value y.CombinedAssets
,KEY 'combinedLiabilities' value y.CombinedLiabilities
,KEY 'combinedIncome' value y.CombinedIncome
,KEY 'downPaymentSourceType' value x.downPaymentSourceType
,KEY 'sourceAmount' value x.DownpaymentAmount
) Deal
FROM t,
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/deal'
PASSING xml
COLUMNS
DealClosingDate timestamp with time zone PATH 'estimatedClosingDate'
,dealPurpose INT PATH 'dealPurposeDd'
,combinedGds INT PATH 'combinedGds'
,combinedTds INT PATH 'combinedTds'
,combinedLtv INT PATH 'combinedLtv'
,downPaymentSourceType INT PATH '/downPaymentSource/downPaymentSourceTypeDd'
,DownpaymentAmount INT PATH '/downPaymentSource/amount'
) x,
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/applicantGroup/applicant'
PASSING xml
COLUMNS
CombinedLiabilities INT PATH 'liability/broker/liabilityAmount'
,CombinedIncome INT PATH 'employmentHistory/income/incomeAmount'
,CombinedAssets INT PATH 'asset/assetValue'
) y
You are cross-joining the base table and the two XMLTable calls. Your second XMLTable looks for applicantGroup nodes, and there aren't any in this XML document, so that gets now rows; and therefore the query overall returns no rows either.
If you change the second (implicit) cross join to outer apply then you'll still see the data found by the first XMLtable. It's unclear if there will always be a deal node, but you can make the first join an outer apply too.
...
FROM t
OUTER APPLY
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/deal'
PASSING xml
COLUMNS
DealClosingDate timestamp with time zone PATH 'estimatedClosingDate'
,dealPurpose INT PATH 'dealPurposeDd'
,combinedGds INT PATH 'combinedGds'
,combinedTds INT PATH 'combinedTds'
,combinedLtv INT PATH 'combinedLtv'
,downPaymentSourceType INT PATH '/downPaymentSource/downPaymentSourceTypeDd'
,DownpaymentAmount INT PATH '/downPaymentSource/amount'
) x
OUTER APPLY
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/applicantGroup/applicant'
PASSING xml
COLUMNS
CombinedLiabilities INT PATH 'liability/broker/liabilityAmount'
,CombinedIncome INT PATH 'employmentHistory/income/incomeAmount'
,CombinedAssets INT PATH 'asset/assetValue'
) y
DEAL
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":null,"combinedAssets":null,"combinedLiabilities":null,"combinedIncome":null,"downPaymentSourceType":null,"sourceAmount":null}
db<>fiddle
(I changed the dealClosingDate data type because... well, I could, and it didn't really look right as a string; but as it ends up as a string in the JSON anyway it's a bit moot.)
Depending on whether you can have multiple (or zero) deal nodes and/or multiple (or zero) applicantGroup nodes - presumably groups can have multiple child nodes - you might be able to structure the XMLTables a bit differently, but it depends on the data and what you want to see.
from your result, you can see that downPaymentSourceType and downpaymentAmount are retuning nulls which is not the case in the XML
That's because the path you are using for those starts with /downPaymentSource/... which doesn't exist within the XPath; and if you did ./downPaymentSource/... or just /downPaymentSource/... you would get a "expected singleton sequence - got multi-item sequence" error.
You can extract the collection of those downPaymentSource nodes as an XMLType, and then use another XMLTable to expand those too:
FROM t
OUTER APPLY
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/deal'
PASSING xml
COLUMNS
DealClosingDate timestamp with time zone PATH 'estimatedClosingDate'
,dealPurpose INT PATH 'dealPurposeDd'
,combinedGds INT PATH 'combinedGds'
,combinedTds INT PATH 'combinedTds'
,combinedLtv INT PATH 'combinedLtv'
,downPayments XMLType PATH 'downPaymentSource'
-- ,downPaymentSourceType INT PATH '/downPaymentSource/downPaymentSourceTypeDd'
-- ,DownpaymentAmount INT PATH '/downPaymentSource/amount'
) x
OUTER APPLY
XMLTABLE(
XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/downPaymentSource'
PASSING x.downPayments
COLUMNS
downPaymentSourceType INT PATH 'downPaymentSourceTypeDd'
,DownpaymentAmount INT PATH 'amount'
) y
OUTER APPLY
XMLTABLE(XMLNAMESPACES(DEFAULT 'http://www.abcdef.com/Schema/FCX/1'), '/loanApplication/applicantGroup/applicant'
PASSING xml
COLUMNS
CombinedLiabilities INT PATH 'liability/broker/liabilityAmount'
,CombinedIncome INT PATH 'employmentHistory/income/incomeAmount'
,CombinedAssets INT PATH 'asset/assetValue'
) z
DEAL
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":25000,"combinedAssets":1111,"combinedLiabilities":10000000,"combinedIncome":200000,"downPaymentSourceType":1,"sourceAmount":25000}
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":25000,"combinedAssets":null,"combinedLiabilities":null,"combinedIncome":150000,"downPaymentSourceType":1,"sourceAmount":25000}
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":25000,"combinedAssets":1111,"combinedLiabilities":10000000,"combinedIncome":200000,"downPaymentSourceType":6,"sourceAmount":25000}
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":25000,"combinedAssets":null,"combinedLiabilities":null,"combinedIncome":150000,"downPaymentSourceType":6,"sourceAmount":25000}
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":50000,"combinedAssets":1111,"combinedLiabilities":10000000,"combinedIncome":200000,"downPaymentSourceType":10,"sourceAmount":50000}
{"dealClosingDate":"2017-12-05T00:00:00.000000-05:00","dealPurpose":2,"combinedGds":0,"combinedTds":10,"combinedLtv":2,"downpaymentAmount":50000,"combinedAssets":null,"combinedLiabilities":null,"combinedIncome":150000,"downPaymentSourceType":10,"sourceAmount":50000}
db<>fiddle
That still might not be what you want though; you might be trying to get a JSON array of those values, and maybe of the applicants too - depends if you're expecting 1, 2, or 6 JSON docs from this example XML doc. However, if you do want different JSON and can't figure out how to get there then you would need to ask a new question about that.

I have a table with one column as XML Clob. I want to extract the XML attributes as columns

I have below XML as data stored in 1 of the columns in my table. I want to extract all the data under VestingInfo, each source data should be a single column. I tried the below but it give not a valid identifier error
select plans.* from cth01.tablename pl, XMLTABLE(XMLNAMESPACES
('http://...' as "ns3", 'http:/...' as "ns4"),
'/CTHClob/ns3:OtherData/ns4:Participant/ns4:Plans' PASSING
xmltype(pl.rqst_info) COLUMNSNumber varchar2(20) path
'ns4:Number', Pin varchar2(20) path 'ns4:Pin') plans where
pl.prty_rqst_id = '';
XML looks like below
<?xml version="1.0"?><CTHClobType xmlns:ns2="http://... xmlns:ns4....>
--namespaces removed
<ns3:OtherData>
<ns4:Participant>
<ns4:Name>ALEXA MAKAILA JAVAVILLAGE</ns4:Name>
<ns4:PIN>4159505</ns4:PIN>
<ns4:PlanBalance>3497.15</ns4:PlanBalance>
<ns4:ForcedDistributionAmount>3497.15</ns4:ForcedDistributionAmount>
<ns4:ParticipantListType>Critical</ns4:ParticipantListType>
<ns4:Plans>
<ns4:Plan>
<ns4:Number>100178</ns4:Number>
<ns4:Name>CHILDRENS HOSPITAL OF PHIL. RETIREMENT SAVINGS PLAN - CSA</ns4:Name>
<ns4:Balance>3497.15</ns4:Balance>
<ns4:VestingInfo>
<ns4:DelayedVestingLite>false</ns4:DelayedVestingLite>
<ns4:DelayedVestingFull>true</ns4:DelayedVestingFull>
<ns4:VestingSourcesOnSingleSchedule>false</ns4:VestingSourcesOnSingleSchedule>
--The above 3 should be repeted for each source
<ns4:Sources>
<ns4:Source>
<ns4:SourceID>T</ns4:SourceID>
<ns4:SourceName>EMPLOYER MATCH</ns4:SourceName>
<ns4:VestedPercentage>50</ns4:VestedPercentage>
<ns4:VestedAmount>5647.94</ns4:VestedAmount>
<ns4:UnAdjustedVestedPercent>50</ns4:UnAdjustedVestedPercent>
<ns4:TIAAContractNumber>330292F5</ns4:TIAAContractNumber>
<ns4:CREFContractNumber>430292F3</ns4:CREFContractNumber>
<ns4:ContractName>GRA</ns4:ContractName>
</ns4:Source>
<ns4:Source>
...
</ns4:Source>
</ns4:Sources>
</ns4:VestingInfo>
Above xml is partial as completely it a big one so removed unrelated tags.
The resulting elements should be as below
ReslutingColumns
Found the solution. Below works perfectly for query like above
SELECT prty_rqst_id, vstng_src_on_sngl_sch, OMNI_PLN_NBR, sources.*
FROM cth01.rqst_payload pl,
XMLTABLE(xmlnamespaces (<namespaces>),
'//pathToXMLTagToBeExtracted/.../'
passing xmltype(pl.rqst_info)
COLUMNS
OMNI_PLN_NBR VARCHAR2(6) path 'ns4:Number'),
XMLTABLE(xmlnamespaces (<namespaces>),
'//pathToXMLTagToBeExtracted/...//ns4:VestingInfo'
passing xmltype(pl.rqst_info)
COLUMNS
vstng_src_on_sngl_sch VARCHAR2(50) path 'ns4:VestingSourcesOnSingleSchedule'),
XMLTABLE(xmlnamespaces (<namespaces>) ,
'//pathToXMLTagToBeExtracted/...//ns4:VestingInfo/ns4:Sources/ns4:Source'
passing xmltype(pl.rqst_info)
COLUMNS
src_id VARCHAR2(20) path 'ns4:SourceID',
src_nm VARCHAR2(50) path 'ns4:SourceName',
SUB_PLN_NBR VARCHAR2(16) path 'ns4:SubplanNumber',
vstd_pct NUMBER(20, 2) path 'ns4:VestedPercentage',
vstd_amt NUMBER(20, 2) path 'ns4:VestedAmount',
UPD_UNADJ_VSTD_PCT NUMBER(15,2) path 'ns4:UpdatedUnAdjustedVestedPercent',
unadj_vstd_pct NUMBER(20, 2) path 'ns4:UnAdjustedVestedPercent',
tiaa_cntrct_nbr VARCHAR2(50) path 'ns4:TIAAContractNumber',
cref_cntrct_nbr VARCHAR2(50) path 'ns4:CREFContractNumber',
cntrct_nm VARCHAR2(50) path 'ns4:ContractName' ) sources
WHERE pl.prty_rqst_id = '123')

Sql query with cross join XMLTABLE

Help needed to extract the data below from XML messages. I have table which contains the xml message in clob data type. I am trying using below query but it is not returning any data . I need to extract all the values from xml message.
<iORDERS:iORDERS xmlns:iORDERS="urn:iORDERS-abcdonline-com:Integration:v1">
<ORDER_NOTIFY>
<MESSAGE_DATETIME>2017-06-13T12:20:51+10:00</MESSAGE_DATETIME>
<MESSAGE_SEQ>1</MESSAGE_SEQ>
<MESSAGE_TYPE>PLACED</MESSAGE_TYPE>
<ORDER_HEAD>
<ORDER_ID>1111</ORDER_ID>
<DROP_SHIP_ORDER_NO></DROP_SHIP_ORDER_NO>
<CUSTOMER_ORDER_NO>22222</CUSTOMER_ORDER_NO>
<DISPATCH_LOCATION>
<SKU>2323234</SKU>
<UPC>4549432533626</UPC>
<REQUESTED_QTY>1</REQUESTED_QTY>
<DISPATCH_ASSIGNMENT>7777</DISPATCH_ASSIGNMENT>
<PROVIDER_ID>100</PROVIDER_ID>
<PKG_TYPE>SAT</PKG_TYPE>
</DISPATCH_LOCATION>
</ORDER_HEAD>
</ORDER_NOTIFY>
</iORDERS:iORDERS>
query :
select wor.batch_no,wor.web_service_no,x.*
from web_orders wo
cross join XMLTABLE (
XMLNAMESPACES(DEFAULT 'urn:iORDERS-abcdonline-com:Integration:v1'),
'iORDERS/ORDER_NOTIFY/ORDER_HEAD/DISPATCH_LOCATION'
passing xmltype(wo.xml_message)
columns
MESSAGE_TYPE varchar(120) path './../../../MESSAGE_TYPE') x;
You need to provide the named namespace identifier rather than a defealt, and your column path is going up one too many levels:
select wo.batch_no,wo.web_service_no,x.*
from web_orders wo
cross join XMLTABLE (
XMLNAMESPACES('urn:iORDERS-abcdonline-com:Integration:v1' as "iORDERS"),
'iORDERS:iORDERS/ORDER_NOTIFY/ORDER_HEAD/DISPATCH_LOCATION'
passing xmltype(wo.xml_message)
columns
MESSAGE_TYPE varchar(120) path './../../MESSAGE_TYPE') x;
BATCH_NO WEB_SERVICE_NO MESSAGE_TYPE
---------- -------------- ------------------------------------------------------------------------------------------------------------------------
1 2 PLACED
Presumably you're planning on getting for information that that from the XML, and/or expect to have multiple nodes; otherwise, to just get the message type you could simplify to:
select wo.batch_no,wo.web_service_no,x.*
from web_orders wo
cross join XMLTABLE (
XMLNAMESPACES('urn:iORDERS-abcdonline-com:Integration:v1' as "iORDERS"),
'iORDERS:iORDERS/ORDER_NOTIFY'
passing xmltype(wo.xml_message)
columns
MESSAGE_TYPE varchar(120) path 'MESSAGE_TYPE') x;
or even, with a single node:
select wo.batch_no,wo.web_service_no,XMLQuery(
'declare namespace iORDERS="urn:iORDERS-abcdonline-com:Integration:v1"; (: :)
iORDERS:iORDERS/ORDER_NOTIFY/MESSAGE_TYPE/text()'
passing xmltype(wo.xml_message)
returning content).getStringVal() as message_type
from web_orders wo;