hope this is something simple and I am just missing it, but consider this example:
with et as(
SELECT
xmlType('<Invoice>
<InvoiceInformation>
<Number>123456</Number>
</InvoiceInformation>
<InvoiceLines>
<InvoiceLine>
<Detail>
<Amount>100</Amount>
<Line>1</Line>
</Detail>
<Type>
<CheesyPotato>
<Instructions>
<CookTime>120</CookTime>
<CookTimeUnits>Minutes</CookTimeUnits>
<CookTemperature>450</CookTemperature>
</Instructions>
</CheesyPotato>
</Type>
</InvoiceLine>
<InvoiceLine>
<Detail>
<Amount>10000</Amount>
<Line>2</Line>
</Detail>
<Type>
<DeathStar>
<Instructions>
<CookTime>4</CookTime>
<CookTimeUnits>5 "parsecs"</CookTimeUnits>
<CookTemperature>1000000</CookTemperature>
</Instructions>
</DeathStar>
</Type>
</InvoiceLine>
<InvoiceLine>
<Detail>
<Amount>250</Amount>
<Line>3</Line>
</Detail>
<Type>
<Quiche>
<Instructions>
<CookTime>75</CookTime>
<CookTimeUnits>Minutes</CookTimeUnits>
<CookTemperature>350</CookTemperature>
</Instructions>
</Quiche>
</Type>
</InvoiceLine>
</InvoiceLines>
</Invoice>
') xt
from dual
)
SELECT
ext.*
FROM
et,
XMLTABLE(
'for $Invoice in $INV/Invoice
for $InvoiceItem in $Invoice/InvoiceLines/InvoiceLine
return <row>
{
$Invoice
,$InvoiceItem
}
</row>'
PASSING et.xt as INV
COLUMNS
INVOICENUMBER VARCHAR2 (6) PATH 'Invoice/InvoiceInformation/Number'
,InvoiceLineNumber VARCHAR2 (5) PATH 'InvoiceLine/Detail/Line'
,Amount VARCHAR2 (5) PATH 'InvoiceLine/Detail/Amount'
,CookTime VARCHAR2 (5) PATH 'InvoiceLine/Type//Instructions/CookTime'
,CookTimeUnits VARCHAR2 (15) PATH 'InvoiceLine/Type//Instructions/CookTimeUnits'
,CookTemperature VARCHAR2 (10) PATH 'InvoiceLine/Type//Instructions/CookTemperature'
) ext
/
–Give me these results
INVOICENUMBER INVOICELINENUMBER AMOUNT COOKTIME COOKTIMEUNITS COOKTEMPERATURE
------------- ----------------- ------ -------- --------------- ---------------
123456 1 100 120 Minutes 450
123456 2 10000 4 5 "parsecs" 1000000
123456 3 250 75 Minutes 350
However I would like to, in this query, get the Type value. Thus How do I get the child node of Type’s “Name” so my results would be as such?
TypeChild INVOICENUMBER INVOICELINENUMBER AMOUNT COOKTIME COOKTIMEUNITS COOKTEMPERATURE
------------------- ------------- ----------------- ------ -------- --------------- ---------------
CheesyPotato 123456 1 100 120 Minutes 450
DeathStar 123456 2 10000 4 5 "parsecs" 1000000
Quiche 123456 3 250 75 Minutes 350
I have tried various methods; along the lines of “,$InvoiceItem/Type/child/name” to no avail, any help would be appreciated, thanks
Or am I simply going at this the incorrect way? (what cannot be changed, however, is that I will need to be able to consume this XML into a query!)
1 Answer