query from xml not return rows.
I running this query but not return rows.
The my xml is :
<?xml version="1.0" encoding="UTF-8"?>
<ns0:testata xmlns:ns0="http://siete">
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>1</ns0:PERIOD>
</ns0:product>
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>2</ns0:PERIOD>
</ns0:product>
</ns0:testata>
My query is
FROM XMLTABLE('/testata/product'
PASSING
(select xmltype(t.XML1) doc
from tb_test t)
COLUMNS
name varchar2(4) PATH './YEAR'
) xmlt
0 rows
please help me
Your XML document has a namespace, so you either need to wildcard the nodes in your XMLTable call, or - preferably - supply the same namespace information and prefixes:
-- CTE for sample data
with tb_test (xml1) as (select '<?xml version="1.0" encoding="UTF-8"?>
<ns0:testata xmlns:ns0="http://siete">
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>1</ns0:PERIOD>
</ns0:product>
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>2</ns0:PERIOD>
</ns0:product>
</ns0:testata>' from dual
)
-- actual query
select x.year
from tb_test t
cross join xmltable(
xmlnamespaces('http://siete' as "ns0"),
'/ns0:testata/ns0:product'
passing xmltype(t.xml1)
columns year number path 'ns0:YEAR'
) x;
YEAR
----------
2019
2019
Related
I have created a query to pull data from XML column of table however I am having issue when XML has list of data i.e.
Cola Brand has multiple code inside
See My query
select extract(cola_info, '/ns2:cola/coke/bottleCode', 'xmlns:ns2="http://myColaStaticInfo.com"') as op;
Output is something like
I would like to have highlighted content one after another and not together in one column.
Any idea!!!
MY XML look like this
<?xml version="1.0" encoding="ISO-8859-15" standalone="yes"?>
<ns2:cola xmlns:ns2="http://myColaStaticInfo.com">
<coke>
<code>Bottle_2829</code>
<label>Mirinda</label>
</coke>
<coke>
<code>Bottle_2830</code>
<label>Mirinda</label>
</coke>
<coke>
<code>Bottle_2831</code>
<label>Mirinda</label>
</coke>
</ns2:cola>
XMLTable is probably your best choice for this. Especially since extract has been deprecated.
with example_data as (select xmltype('<?xml version="1.0" encoding="ISO-8859-15" standalone="yes"?>
<ns2:cola xmlns:ns2="http://myColaStaticInfo.com">
<coke>
<code>Bottle_2829</code>
<label>Mirinda</label>
</coke>
<coke>
<code>Bottle_2830</code>
<label>Mirinda</label>
</coke>
<coke>
<code>Bottle_2831</code>
<label>Mirinda</label>
</coke>
</ns2:cola>') as xml from dual)
-- query
select bottle_coke
from example_data e
cross join XMLTABLE(XMLNAMESPACES('http://myColaStaticInfo.com' as "ns2"),
'/ns2:cola/coke'
PASSING e.xml
COLUMNS
bottle_coke XMLTYPE PATH 'code'
) xt;
Output:
<code>Bottle_2829</code>
<code>Bottle_2830</code>
<code>Bottle_2831</code>
Given the following XML sample below, how can I extract all <table_no> values from top to bottom using Oracle XML extraction.
Based on below, I would expect to see the following individual rows from my select:
1
4
2
11
Table: tickets
Column holding XML: ticket_col
XML code:
<xml>
<ticket_order>
<table_no>1<table_no/>
<waiter>Jack<waiter/>
<total_people>12<total_people/>
</ticket_order>
<ticket_order>
<table_no>4<table_no/>
<waiter>Jackie<waiter/>
<total_people>3<total_people/>
</ticket_order>
<ticket_order>
<table_no>2<table_no/>
<waiter>Sally<waiter/>
<total_people>2<total_people/>
</ticket_order>
<ticket_order>
<table_no>11<table_no/>
<waiter>Mike<waiter/>
<total_people>6<total_people/>
</ticket_order>
</xml>
You can use XMLTable(); with (fixed) sample XML as a string in-line:
select x.*
from xmltable(
'/xml/ticket_order'
passing xmltype('<xml>
<ticket_order>
<table_no>1</table_no>
<waiter>Jack</waiter>
<total_people>12</total_people>
</ticket_order>
<ticket_order>
<table_no>4</table_no>
<waiter>Jackie</waiter>
<total_people>3</total_people>
</ticket_order>
<ticket_order>
<table_no>2</table_no>
<waiter>Sally</waiter>
<total_people>2</total_people>
</ticket_order>
<ticket_order>
<table_no>11</table_no>
<waiter>Mike</waiter>
<total_people>6</total_people>
</ticket_order>
</xml>')
columns table_no number path 'table_no'
) x;
TABLE_NO
----------
1
4
2
11
If the XML is a string (VARCHAR2 or CLOB) in a table you would pass it in via a cross join:
select x.*
from your_table t
cross join xmltable(
'/xml/ticket_order'
passing xmltype(t.xml_string)
columns table_no number path 'table_no'
) x;
If it's already am XMLType in the table you woudl skip that conversion:
select x.*
from your_table t
cross join xmltable(
'/xml/ticket_order'
passing t.xml
columns table_no number path 'table_no'
) x;
You can get multiple columns at once; and the generated relatinal column name doesn't have to be the same as the node name:
select x.*
from your_table t
cross join xmltable(
'/xml/ticket_order'
passing t.xml
columns table_number number path 'table_no',
server varchar2(10) path 'waiter',
covers number path 'total_people'
) x;
TABLE_NUMBER SERVER COVERS
------------ ---------- ----------
1 Jack 12
4 Jackie 3
2 Sally 2
11 Mike 6
Read more.
I'm trying to get the values of the attributes from table MVR_DTL in column VENDOR_XML. VENDOR_XML is of datatype clob and contains an xml that looks like this
<?xml version="1.0" encoding="UTF-8"?>
<MVRCHPINFF_1.0>
<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>
</MVRRecLoop>
</MVRCHPINFF_1.0>
I tried running
SELECT c.Address
from MVR_DTL M, XMLTABLE('/MVRCHPINFF_1.0/MVRRecLoop/CLoop/CRec'
passing XMLTYPE(M.VENDOR_XML)
columns Address XMLTYPE PATH './C_MVRNumberAddr') c;
I'm expecting something like
ROMAN GENERAL ROMAN ST ROMAN CITY, ROME 111111
but i only get 'Statement has failed, however your database does not return any error information.'
Oracle version 12.2.0.1.0
SELECT c.Address from MVR_DTL M,
XMLTABLE(
'string-join(/MVRCHPINFF_1.0/MVRRecLoop/CLoop/CRec/C_MVRNumberAddr, " ")'
passing XMLTYPE(M.VENDOR_XML)
columns Address varchar2(200) PATH '.') c;
I am trying to update an element which is inside a clob column in oracle DB.
First challenge I am facing is that my clob xml has 2 namespaces and I am not able to get that working .
<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>false</finalElement>
</test-elemen>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>
select x.* from cc_messagehistory y
cross join xmltable(
xmlnamespaces('http://www.test.com/esb/message/1.0' as "esbmsg",
'http://test.com ' ),
'/esbmsg:EsbMessage'
passing xmltype.createxml(y.payload)
factext varchar2(10) path '/esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement'
) x;
ORA-19102: XQuery string literal expected
19102. 00000 - "XQuery string literal expected"
*Cause: The string literal containing the XQuery expression was missing.
*Action: Specify the XQuery expression as a string literal. Error at Line: 64 Column: 99
The immediate cause of the ORA-01902 is that you missed the default keyword:
xmlnamespaces('http://www.test.com/esb/message/1.0' as "esbmsg",
default 'http://test.com'),
I've removed the extra space at the end of the URI, which would cause problems later. But you are also missing the columns keyword, and you can simplify the conversion of the CLOB value to an XMLType.
Putting that together, and with a CTE to supply your (corrected) sample XML:
-- CTE for sample data
with cc_messagehistory(payload) as (
select to_clob('<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>false</finalElement>
</test-element>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>') from dual
)
-- actual query
select x.*
from cc_messagehistory y
cross join xmltable (
xmlnamespaces (
'http://www.test.com/esb/message/1.0' as "esbmsg",
default 'http://test.com'
),
'/esbmsg:EsbMessage'
passing xmltype(y.payload)
columns factext varchar2(10)
path '/esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement'
) x;
FACTEXT
----------
false
For the update you could do something like:
update cc_messagehistory y
set payload = XMLSerialize(document
XMLQuery('declare default element namespace "http://test.com"; (: :)
declare namespace esbmsg="http://www.test.com/esb/message/1.0"; (: :)
copy $i := $xml modify (
for $j in $i//esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement
return replace value of node $j with $new
)
return $i'
passing xmltype(y.payload) as "xml",
'true' AS "new"
returning content
)
indent size=2
)
where xmlexists('declare default element namespace "http://test.com"; (: :)
declare namespace esbmsg="http://www.test.com/esb/message/1.0"; (: :)
$xml//esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement[text()="false"]'
passing xmltype(y.payload) as "xml");
which transforms that source CLOB into:
<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>true</finalElement>
</test-element>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>
db<>fiddle (works on 18c; errors on 11gR2, but patch levels may make a difference; also tested successfully elsewhere on 12cR1)
For example, my clob contains xml content as shown below
<?xml version="1.0"?>
<content>
<foo>bar</foo>
<bar>foo</bar>
</content>
When i print it using loop, struct of content is not well-formed.
Use XMLSerialize function:
with test_data as (
select
'<?xml version="1.0"?><content>
<foo>bar</foo><bar>foo</bar></content>' as x_data
from dual
)
select
xmlserialize(document xmltype(x_data) as clob indent size = 2) x_text
from test_data
SQLFiddle