Home » Developer & Programmer » JDeveloper, Java & XML » Problem querying XML in CLOB column (SQL*Plus: Release 9.0.1.3.0 - Unknown OS.)
Problem querying XML in CLOB column [message #500082] |
Thu, 17 March 2011 12:58 ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](//www.gravatar.com/avatar/f8f212b14c183ccb2620ec3410d96a85?s=64&d=mm&r=g) |
aileron
Messages: 3 Registered: March 2011
|
Junior Member |
|
|
Hi,
I'm new to this forum and I'm hoping someone here can help me out.
I have spent days trying to figure this out. I must warn you I'm new to Oracle as my past experience (years ago) was with Mysql and Postgres sql on Linux and I've been out of IT for years. Now I'm a lowly engineer and because of budget cuts I'm forced to do this because our IT department is going away.
So the problem... I have a CLOB column called XML_DATA that has (not-surprisingly) xml data in it that's housed inside a table called HMS_XML_TRANSFER. It has been giving me a headache because I'm unable at this point to use the xml field as a condition to get its TRANS_SEQUENCE number. The where clause doesn't work.
SELECT TRANS_SEQUENCE, XML_DATA
FROM HMS_XML_TRANSFER
WHERE EXTRACTVALUE(XMLTYPE (XML_DATA), '/INTERFACES/INTERFACE/BODY/IFI0057[ACTIVITY_CODE = "2201-020742"]');
The only test that I have been able to get working is the one below.
SELECT TRANS_SEQUENCE, EXTRACTVALUE(XMLTYPE (XML_DATA), '/INTERFACES/INTERFACE/BODY/IFI0057/ACTIVITY_CODE')
FROM HMS_XML_TRANSFER
WHERE TRANS_SEQUENCE = '8191602';
It will give me the ACTIVITY_CODE element so I know I can pull data from the XML but I can't do the reverse in the first example which is what I need because I don't know the TRANS_SEQUENCE number, I just know the ACTIVITY_CODE.
I have over 202 error messages logged in Teradata SQL from my many and varied attempts to get this to work using every example I could find online.
As an example this has not worked either...
WHERE EXISTNODE(XML_DATA, '/INTERFACES/INTERFACE/BODY/IFI0057[ACTIVITY_CODE = "2201-020742"]') = 1;
So the question... How do I properly form my SQL statement so I can use the XML column's ACTIVITY_CODE element to get the TRANS_SEQUENCE column field? Oh and I'd like to see both columns in the result.
Thanks in advance to any Guru's that can help out.
Below is the version of Oracle I'm using, the description of the Table HMS_XML_TRANSFER, and a sample of the XML that comes from XML_DATA. I can't seems to get tabs working sorry. :(
===============
ORACLE VERSION
===============
SQL*Plus: Release 9.0.1.3.0 - Production on Thu Mar 17 08:18:15 2011
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
=========================
TABLE HMS_XML_TRANSFER
=========================
Name Null? Type
--------------- --------- --------------
TRANS_TYPE NOT NULL VARCHAR2(10)
DATE_IN NOT NULL DATE
DATE_PROCESSED DATE
STATUS VARCHAR2(8)
ERROR_MSG VARCHAR2(2000)
TRANS_SEQUENCE NUMBER(38)
SITE_CODE VARCHAR2(20)
COMMAND VARCHAR2(2000)
[b]XML_DATA CLOB[/b]
========================
XML in COLUMN XML_DATA
========================
<INTERFACES>
<INTERFACE>
<HEADER>
<INTERFACE_NAME>CREATE_UPDATE_PLAN_ACTIVITY</INTERFACE_NAME>
<EXTERNAL_APP_NAME>SILVER</EXTERNAL_APP_NAME>
<EXTERNAL_APP_ID>INTFCE</EXTERNAL_APP_ID>
</HEADER>
<BODY>
<IFI0057>
<CUSTOMER_ID>SAP</CUSTOMER_ID>
<ACTIVITY_CODE>2201-020742</ACTIVITY_CODE>
<CODE_DESCRIPTION>BRACKET INSTALL</CODE_DESCRIPTION>
<ACTIVITY_TYPE>Z</ACTIVITY_TYPE>
<ACTIVITY_GROUP>1110</ACTIVITY_GROUP>
<SHORT_DESCRIPTION>BRAC</SHORT_DESCRIPTION>
<LAST_CHG_DATETIME>20110309</LAST_CHG_DATETIME>
<STATUS>COMPLETE</STATUS>
<EST_MATERIAL_COST></EST_MATERIAL_COST>
<EST_LABOR_HOURS>8</EST_LABOR_HOURS>
<EST_DAYS></EST_DAYS>
<USER_REF1>5</USER_REF1>
<USER_REF2>CD-11001-007</USER_REF2>
<USER_REF3></USER_REF3>
<USER_REF4></USER_REF4>
<USER_REF5></USER_REF5>
<USER_REF6></USER_REF6>
<USER_REF7></USER_REF7>
<USER_REF8></USER_REF8>
<PLAN_TYPE>INSTALL</PLAN_TYPE>
<ALT_PLAN> </ALT_PLAN>
<MODEL>SUPERX</MODEL>
<DEPARTMENT>010ZD</DEPARTMENT>
<WBS_CLIN>1.1.2</WBS_CLIN>
<IFI005701>
<CUSTOMER_ID>SAP</CUSTOMER_ID>
<ACTIVITY_CODE></ACTIVITY_CODE>
<MASTER_FORM_CODE></MASTER_FORM_CODE>
<MASTER_FORM_TYPE>WO</MASTER_FORM_TYPE>
</IFI005701>
<IFI005702>
<CUSTOMER_ID>SAP</CUSTOMER_ID>
<DEFAULT_ORDER>2201-020742</DEFAULT_ORDER>
<DEF_TYPE></DEF_TYPE>
<DEF_DESCRIPTION></DEF_DESCRIPTION>
<DEF_JDD_SCENARIO></DEF_JDD_SCENARIO>
<PRIORITY></PRIORITY>
<REPAIR_TYPE></REPAIR_TYPE>
<BUYER></BUYER>
<WHEN_DISCOVERED></WHEN_DISCOVERED>
<HOW_MAL></HOW_MAL>
<TCN></TCN>
<PARTS_REQUIRED>T</PARTS_REQUIRED>
<PART></PART>
<MFG_MIN_QTY></MFG_MIN_QTY>
<MFG_SETUP_TIME></MFG_SETUP_TIME>
<MFG_RUN_TIME></MFG_RUN_TIME>
<MFG_LOT_PREFIX></MFG_LOT_PREFIX>
</IFI005702>
<IFI005703>
<CUSTOMER_ID>SAP</CUSTOMER_ID>
<STRUCTURE_CODE>SUPERX</STRUCTURE_CODE>
<SUB_STRUCTURE_CODE></SUB_STRUCTURE_CODE>
<POS></POS>
<PART></PART>
<REMARKS></REMARKS>
<CREATED_USERID></CREATED_USERID>
<CREATED_DATETIME></CREATED_DATETIME>
<LAST_CHANGED_USERID></LAST_CHANGED_USERID>
<LAST_CHANGED_DATETIME></LAST_CHANGED_DATETIME>
<IFI00570301>
<CUSTOMER_ID>SAP</CUSTOMER_ID>
<EFF_FROM>SX001</EFF_FROM>
<EFF_THRU>SX010</EFF_THRU>
</IFI00570301>
</IFI005703>
</IFI0057>
</BODY>
</INTERFACE>
</INTERFACES>
[Updated on: Thu, 17 March 2011 13:13] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
Re: Problem querying XML in CLOB column [message #500404 is a reply to message #500241] |
Sun, 20 March 2011 22:50 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
The fact that you get the error messages when performing a search that selects from all rows and that you do not get the error message when performing a search that has a filter condition that limits it to one row, indicates that although the xml data in the row that you are searching is well-formed, the data in some other row is not. Most likely there is an ampersand in some other row. If so, this can be solved simply by using replace. Please see the simplified reproduction and solution below, where I have entered one row without an ampersand and one row with an ampersand. One query produces the error stack that you are getting and the one that limits it to the row without the ampersand works. Then, adding replace, the original query works. If this does not help, then I suspect that there is something other than an ampersand that constitutes data that is not well-formed in one or more rows. You may be able to limit your search to ranges of records and such until you can figure out what the offending row(s) is/are and how to deal with them.
-- simplified reproduction of problem:
SCOTT@orcl_11gR2> CREATE TABLE hms_xml_transfer
2 (trans_sequence NUMBER (38),
3 xml_data CLOB)
4 /
Table created.
SCOTT@orcl_11gR2> SET DEFINE OFF
SCOTT@orcl_11gR2> INSERT INTO hms_xml_transfer VALUES (8191602,
2 '<BODY>
3 <ACTIVITY_CODE>2201-020742</ACTIVITY_CODE>
4 <OTHER>test data</OTHER>
5 </BODY>
6 ')
7 /
1 row created.
SCOTT@orcl_11gR2> INSERT INTO hms_xml_transfer VALUES (1234567,
2 '<BODY>
3 <ACTIVITY_CODE>1234-567890</ACTIVITY_CODE>
4 <OTHER>test & data</OTHER>
5 </BODY>
6 ')
7 /
1 row created.
SCOTT@orcl_11gR2> COMMIT
2 /
Commit complete.
SCOTT@orcl_11gR2> SELECT TRANS_SEQUENCE
2 FROM HMS_XML_TRANSFER,
3 TABLE
4 (XMLSEQUENCE
5 (EXTRACT
6 (XMLTYPE(XML_DATA),
7 '//ACTIVITY_CODE'))) x
8 WHERE EXTRACTVALUE(VALUE(x), '//ACTIVITY_CODE') = '2201-020742'
9 /
ERROR:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00242: invalid use of ampersand ('&') character (use &)
Error at line 3
ORA-06512: at "SYS.XMLTYPE", line 272
ORA-06512: at line 1
no rows selected
SCOTT@orcl_11gR2> SELECT TRANS_SEQUENCE,
2 EXTRACTVALUE
3 (XMLTYPE (XML_DATA),
4 '//ACTIVITY_CODE')
5 FROM HMS_XML_TRANSFER
6 WHERE TRANS_SEQUENCE = '8191602'
7 /
TRANS_SEQUENCE
--------------
EXTRACTVALUE(XMLTYPE(XML_DATA),'//ACTIVITY_CODE')
--------------------------------------------------------------------------------
8191602
2201-020742
1 row selected.
-- solution using replace:
SCOTT@orcl_11gR2> SELECT TRANS_SEQUENCE
2 FROM HMS_XML_TRANSFER,
3 TABLE
4 (XMLSEQUENCE
5 (EXTRACT
6 (XMLTYPE(REPLACE (XML_DATA, '&', '&')),
7 '//ACTIVITY_CODE'))) x
8 WHERE EXTRACTVALUE(VALUE(x), '//ACTIVITY_CODE') = '2201-020742'
9 /
TRANS_SEQUENCE
--------------
8191602
1 row selected.
SCOTT@orcl_11gR2>
|
|
|
|
Re: Problem querying XML in CLOB column [message #564339 is a reply to message #564321] |
Wed, 22 August 2012 16:01 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
koteswar wrote on Wed, 22 August 2012 08:59
...CLOB data type is not supported in XML, you have to create a function for that and then call the function in sql statement....
If the clob contains valid xml, then you do not need to create a function, as there are plenty of Oracle built-in functions, as demonstrated in the previous answers in this thread.
|
|
|
Goto Forum:
Current Time: Sun Feb 09 09:50:57 CST 2025
|