I am using oracle 10g and the query below results in following error:
ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
How can we extract all elements in case of multiple items in XML ? Basically I am interested to separate all underlier instrument wise.
SELECT *
FROM xmltable(
xmlnamespaces ('http://www.cool.com/totem/1.1' AS "n1"), '/n1:totem/n1:results'
PASSING xmltype.createxml(('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<totem xmlns="http://www.cool.com/totem/1.1">
<results>
<valuationDate>2014-07-31</valuationDate>
<clientID>220</clientID>
<energy>
<underlier>
<name>CO2 CER</name>
<group>European Emissions</group>
<units>EUR / MT</units>
<pricingTime>LDN 17:00</pricingTime>
<instrument>
<period>Month</period>
<startDate>2014-12-01</startDate>
<endDate>2014-12-31</endDate>
<type>Forward</type>
<price>0.25852</price>
<priceOut>r</priceOut>
<contributors>15</contributors>
</instrument>
</underlier>
<underlier>
<name>CO2 CER SPOT</name>
<group>European Emissions</group>
<units>EUR / MT</units>
<pricingTime>LDN 17:00</pricingTime>
<instrument>
<period>Month</period>
<startDate>2014-07-31</startDate>
<endDate>2014-07-31</endDate>
<type>Spot</type>
<price>0.17</price>
<consensusPrice>0.165988</consensusPrice>
<compositePrice>0.165988</compositePrice>
<priceRange>0.01</priceRange>
<priceStddev>0.004685</priceStddev>
<contributors>10</contributors>
</instrument>
</underlier>
<underlier>
<name>CO2 EUA</name>
<group>European Emissions</group>
<units>EUR / MT</units>
<pricingTime>LDN 17:00</pricingTime>
<instrument>
<period>Month</period>
<startDate>2014-12-01</startDate>
<endDate>2014-12-31</endDate>
<type>Forward</type>
<price>6.251277</price>
<consensusPrice>6.220854</consensusPrice>
<compositePrice>6.220854</compositePrice>
<priceOut>*</priceOut>
<priceRange>0.044779</priceRange>
<priceStddev>0.014507</priceStddev>
<contributors>18</contributors>
</instrument>
<instrument>
<period>Month</period>
<startDate>2015-12-01</startDate>
<endDate>2015-12-31</endDate>
<type>Forward</type>
<price>6.399921</price>
<consensusPrice>6.382712</consensusPrice>
<compositePrice>6.382092</compositePrice>
<priceOut>s</priceOut>
<priceRange>0.05</priceRange>
<priceStddev>0.014458</priceStddev>
<contributors>18</contributors>
</instrument>
<instrument>
<period>Month</period>
<startDate>2016-12-01</startDate>
<endDate>2016-12-31</endDate>
<type>Forward</type>
<price>6.590721</price>
<consensusPrice>6.563745</consensusPrice>
<compositePrice>6.563749</compositePrice>
<priceOut>s</priceOut>
<priceRange>0.070721</priceRange>
<priceStddev>0.017205</priceStddev>
<contributors>18</contributors>
</instrument>
</underlier>
<underlier>
<name>CO2 EUA SPOT</name>
<group>European Emissions</group>
<units>EUR / MT</units>
<pricingTime>LDN 17:00</pricingTime>
<instrument>
<period>Month</period>
<startDate>2014-07-31</startDate>
<endDate>2014-07-31</endDate>
<type>Spot</type>
<price>6.19</price>
<consensusPrice>6.186412</consensusPrice>
<compositePrice>6.186412</compositePrice>
<priceRange>0.0113</priceRange>
<priceStddev>0.004984</priceStddev>
<contributors>10</contributors>
</instrument>
</underlier>
</energy>
</results>
</totem>'
))
COLUMNS valuationDate varchar2(500) PATH 'n1:valuationDate',
clientID varchar2(500) PATH 'n1:clientID',
name varchar2(500) PATH 'n1:energy/n1:underlier/n1:name',
group1 varchar2(500) PATH 'n1:energy/n1:underlier/n1:group',
units varchar2(500) PATH 'n1:energy/n1:underlier/n1:units',
pricingTime varchar2(500) PATH 'n1:energy/n1:underlier/n1:pricingTime',
period varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:period',
startDate varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:startDate',
endDate varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:endDate',
type varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:type',
price varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:price',
priceOut varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:priceOut',
contributors varchar2(500) PATH 'n1:energy/n1:underlier/n1:instrument/n1:contributors'
) AS instrument ;
In your xml, the data is at multiple levels. There are multiple
underlier
tags and each can have more than oneinstrument
tags. So, you should break it down in stages. (Your XML was too long for varchar datatype, so I shortened it.)Query:
Result: