Problem Query XML TABLE with attributes [message #478874] |
Wed, 13 October 2010 02:44 |
czinsou
Messages: 23 Registered: August 2009
|
Junior Member |
|
|
Hi, I have some troubles to extract values from xml file that I load in XMLType like this :
INSERT INTO loadxml VALUES
(XMLType('
<?xml version='1.0'?>
<InvoiceTransmission>
...
<Invoice>
.....
</Invoice>
...
<InvoiceTransmission>'
));
It's OK for that.
The values that I'm trying to extract are stored in xml like that:
<Invoice>
...
<LineItem>
<LineItemNumber>1</LineItemNumber>
<ChargeCode>P</ChargeCode>
<ChargeAmount Name="Weight">340.000</ChargeAmount>
<ChargeAmount Name="Valuation">999.000</ChargeAmount>
<Tax>
<TaxType>VAT</TaxType>
<TaxAmount>16.000</TaxAmount>
</Tax>
<AddOnCharges>
<AddOnChargeName>ISCAllowed</AddOnChargeName>
<AddOnChargeAmount>-23.000</AddOnChargeAmount>
</AddOnCharges>
<TotalNetAmount>333.000</TotalNetAmount>
<DetailCount>2</DetailCount>
</LineItem>
...
</Invoice>
To get all information run this SQL statement :
SELECT a.LineItemNumber,a.ChargeCode,a.WeightCharge,a.Valuation,a.VATAmount,a.ISCAllowed,a.OtherChargesAllowed,a.TotalNetAmount,a.DetailCount
FROM LOADXML,
XMLTABLE('/InvoiceTransmission/Invoice[InvoiceHeader/InvoiceNumber="ABX1234567"]/LineItem'
PASSING LOADXML.data COLUMNS
LineItemNumber NUMBER(6) PATH '//LineItemNumber',
ChargeCode VARCHAR2(1) PATH '//ChargeCode',
WeightCharge NUMBER(18,3) PATH '//ChargeAmount[Name="Weight"]/.',
Valuation NUMBER(18,3) PATH '//ChargeAmount[Name="Valuation"]/.',
VATAmount NUMBER(18,3) PATH '//Tax/TaxAmount',
ISCAllowed NUMBER(18,3) PATH '//AddOnCharges[AddOnChargeName="ISCAllowed"]/AddOnChargeAmount',
OtherChargesAllowed NUMBER(18,3) PATH '//AddOnCharges[AddOnChargeName="OtherChargesAllowed"]/AddOnChargeAmount',
TotalNetAmount NUMBER(18,3) PATH '//TotalNetAmount',
DetailCount NUMBER(6) PATH '//DetailCount'
)a;
I have all the present values in the result but not values got by attributes (ChargeAmount[Name="Weight"] and ChargeAmount[Name="Valuation"]).
Can someone help me to solve it ? I have a lot a values in the same problem ...
Thanks !
|
|
|
|
|
|
Re: Problem Query XML TABLE with attributes [message #478893 is a reply to message #478881] |
Wed, 13 October 2010 05:28 |
czinsou
Messages: 23 Registered: August 2009
|
Junior Member |
|
|
Hi, I have another problem , I try to run that SQL statement :
SELECT a.BatchSequenceNumber,a.RecordSequenceWithinBatch,a.WeightBilled
FROM LOADXML,
XMLTABLE('/InvoiceTransmission/Invoice[InvoiceHeader/InvoiceNumber="ABX1234567"]/LineItemDetail[LineItemNumber=3 and RecordSequenceWithinBatch=4]'
PASSING LOADXML.data COLUMNS
BatchSequenceNumber NUMBER(5) PATH '//BatchSequenceNumber',
RecordSequenceWithinBatch NUMBER(6) PATH '//RecordSequenceWithinBatch', WeightBilled NUMBER(18,3) PATH '//ChargeAmount[@Name="WeightBilled"]'
)a;
The xml part is :
...
<LineItemDetail>
<DetailNumber>7</DetailNumber>
<LineItemNumber>3</LineItemNumber>
<BatchSequenceNumber>3</BatchSequenceNumber>
<RecordSequenceWithinBatch>3</RecordSequenceWithinBatch>
<ChargeAmount Name="WeightBilled">400.000</ChargeAmount>
</LineItemDetail>
<LineItemDetail>
<DetailNumber>8</DetailNumber>
<LineItemNumber>3</LineItemNumber>
<BatchSequenceNumber>3</BatchSequenceNumber>
<RecordSequenceWithinBatch>4</RecordSequenceWithinBatch>
<ChargeAmount Name="WeightBilled">550.000</ChargeAmount>
</LineItemDetail>
...
Although I have well precised the element ([LineItemNumber=3 and RecordSequenceWithinBatch=4) in my SQL statement,
I have this message :
Error: ORA-19279: XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
An idea to solve the problem ?
[Updated on: Wed, 13 October 2010 05:36] Report message to a moderator
|
|
|
|
|
|
Re: Problem Query XML TABLE with attributes [message #478900 is a reply to message #478899] |
Wed, 13 October 2010 06:29 |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
Your example works for me fine after adapting (my) NLS and wellforming the XML:
WITH loadxml AS
(select xmltype ('
<xml>
<LineItemDetail>
<DetailNumber>7</DetailNumber>
<LineItemNumber>3</LineItemNumber>
<BatchSequenceNumber>3</BatchSequenceNumber>
<RecordSequenceWithinBatch>3</RecordSequenceWithinBatch>
<ChargeAmount Name="WeightBilled">400,000</ChargeAmount>
</LineItemDetail>
<LineItemDetail>
<DetailNumber>8</DetailNumber>
<LineItemNumber>3</LineItemNumber>
<BatchSequenceNumber>3</BatchSequenceNumber>
<RecordSequenceWithinBatch>4</RecordSequenceWithinBatch>
<ChargeAmount Name="WeightBilled">550,000</ChargeAmount>
</LineItemDetail>
</xml>') data from dual)
SELECT
a.BatchSequenceNumber,a.RecordSequenceWithinBatch,a.WeightBilled
FROM loadxml,
XMLTABLE('//LineItemDetail[LineItemNumber=3 and RecordSequenceWithinBatch=4]'
PASSING loadxml.data COLUMNS
BatchSequenceNumber NUMBER(5) PATH '//BatchSequenceNumber',
RecordSequenceWithinBatch NUMBER(6) PATH '//RecordSequenceWithinBatch',
WeightBilled NUMBER(18,3) PATH '//ChargeAmount[@Name="WeightBilled"]')a;
BATCHSEQUENCENUMBER RECORDSEQUENCEWITHINBATCH WEIGHTBILLED
------------------- ------------------------- ------------
3 4 550
1 row selected.
|
|
|
|