Sql query for the unexpected scenario [message #628012] |
Wed, 19 November 2014 01:31 |
|
asbh123
Messages: 26 Registered: November 2014 Location: mumbai
|
Junior Member |
|
|
hi all,
I have one table in that only one column having data in xmlformat and remaing normal oracle datatypes.for example column name is xml_data.I need sql to get values from that xml data output data highlighted for reference in below xml data.
Sample xmldata for one of the record is like below
<?xml version="1.0" encoding="utf-8"?>
<ns0:finalvalue xmlns :ns0="http://amagdgdg.com">
<ns0:valuableitems>
<ns0:valuesfortheitems>dsddd</ns0:valuesfortheitems>
<ns0:valuesid>d21</ns0:valuesid>
<ns0:items>
<ns0:itemid>1a>/ns0:itemid>
<ns0:itemdfg>2a>/ns0:itemdfg>
<ns0:itecart>11a>/ns0:itecart>
</ns0:itesms>
<ns0:items>
<ns0:itemid>56a>/ns0:itemid>
<ns0:itemdfg>2145a>/ns0:itemdfg>
<ns0:itecart>1361a>/ns0:itecart>
</ns0:itesms>
<ns0:items>
<ns0:itemid>1a>/iwwsb0:itemid>
<ns0:itemdfg>2a>/wens0:itemdfg>
<ns0:itecart>11a>/dns0:itecart>
</ns0:itesms>
</ns0:valuableitems>
<ns0:valuableitems>
<ns0:valuesfortheitems>qqqd</ns0:valuesfortheitems>
<ns0:valuesid>dqw21</ns0:valuesid>
<ns0:items>
<ns0:itemid>1wwa>/ns0:itemid>
<ns0:itemdfg>2qa>/ns0:itemdfg>
<ns0:itecart>1ww1a>/ns0:itecart>
</ns0:itesms>
<ns0:items>
<ns0:itemid>56wwa>/ns0:itemid>
<ns0:itemdfg>21dd45a>/ns0:itemdfg>
<ns0:itecart>13a61a>/ns0:itecart>
</ns0:itesms>
<ns0:items>
<ns0:itemid>1addqa>/iwwsb0:itemid>
<ns0:itemdfg>2wa>/wens0:itemdfg>
<ns0:itecart>1aa1a>/dns0:itecart>
</ns0:itesms>
</ns0:valuableitems>
</ns0:finalvalue>
Requiredoutput:
xml_data
dsddd
qqqd
[Edit MC: remove empty lines]
[Updated on: Wed, 19 November 2014 01:40] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Sql query for the unexpected scenario [message #628176 is a reply to message #628098] |
Thu, 20 November 2014 16:03 |
|
Barbara Boehmer
Messages: 9103 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
What I posted, I tested in both 11g and 12c. You must have run something slightly different, which is why you need to copy and paste a run of your query, complete with the results. If you left out something as simple as the x at the end of the last line, then that would produce the error. Please see the example below that first reproduces the error without the x, then runs correctly with the x.
-- version:
SCOTT@orcl> SELECT banner FROM v$version
2 /
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
5 rows selected.
-- reproduction of error by removing x from last line of query:
SCOTT@orcl> SELECT t.seqid,
2 x.xml_data
3 FROM your_table_name t,
4 XMLTABLE
5 (XMLNAMESPACES ('http://amagdgdg.com' AS "ns0"),
6 '//ns0:valuesfortheitems'
7 PASSING t.xml_data
8 COLUMNS
9 xml_data PATH '//ns0:valuesfortheitems')
10 /
x.xml_data
*
ERROR at line 2:
ORA-00904: "X"."XML_DATA": invalid identifier
-- correct query with x at the end of the last line:
SCOTT@orcl> SELECT t.seqid,
2 x.xml_data
3 FROM your_table_name t,
4 XMLTABLE
5 (XMLNAMESPACES ('http://amagdgdg.com' AS "ns0"),
6 '//ns0:valuesfortheitems'
7 PASSING t.xml_data
8 COLUMNS
9 xml_data PATH '//ns0:valuesfortheitems') x
10 /
SEQID XML_DATA
---------- ------------------------------------------------------------
1 dsddd
1 qqqd
2 DSDDD
2 QQQD
4 rows selected.
|
|
|
Re: Sql query for the unexpected scenario [message #628191 is a reply to message #628176] |
Fri, 21 November 2014 01:37 |
|
asbh123
Messages: 26 Registered: November 2014 Location: mumbai
|
Junior Member |
|
|
Hi,
Thanks for the code.but i dont know why the same code is givinng error for me.I applied above code for my table by changing to my table columnnames.my sid columns is similar to seqid column and valid_xml is similar to xml_data column in above script.
SELECT t.sid,
2 x.valid_xml
3 FROM section t,
4 XMLTABLE
5 (XMLNAMESPACES ('http://amagdgdg.com' AS "ns0"),
6 '//ns0:valuesfortheitems'
7 PASSING t.valid_xml
8 COLUMNS
9 xml_data PATH '//ns0:valuesfortheitems') x
10 /
Erroe showing again at line 2 ora-00923 from keyword not found where expected.
what is the use of XMLTABLE in the above script?
[Updated on: Fri, 21 November 2014 01:37] Report message to a moderator
|
|
|
|
|
|
|
Re: Sql query for the unexpected scenario [message #630896 is a reply to message #628196] |
Tue, 06 January 2015 03:07 |
|
asbh123
Messages: 26 Registered: November 2014 Location: mumbai
|
Junior Member |
|
|
Hi Everyone,
Thanks for your Suggestions and scripts.Barbara i used your script works fine.
SELECT t.sid,
x.xml_data
FROM section t,
XMLTABLE
(XMLNAMESPACES ('http://amagdgdg.com' AS "ns0"),
'//ns0:valuesfortheitems'
PASSING t.valid_xml
COLUMNS
xml_data PATH '//ns0:valuesfortheitems') x
/
|
|
|