Solved

Extract value of child from xmltable

Posted on 2016-08-26
9
30 Views
Last Modified: 2016-09-14
There is a big xml , from which i need to extract 2 values. Belowis the snippet.
There could be multiple Identifier tags so i need to find the one which has Scheme= value1 and print its value and the other child's value of the same tag.
I am not able to get to the second child value. Please suggest.

<Identifier>
  <issue Scheme="value1">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>

with mydata as
( select xml_data xx from table1 where id = hextoraw('FFA2F897DD7C30E030CEB15088FE')
)  
select *
from  mydata,
xmltable(xmlnamespaces(  'http://www.fpml.org/FpML-5/recordkeeping' as "n3"),
'/n3:root/*//n3:Identifier/n3:issue[@Scheme=''value1'']'                
                                PASSING mydata.xx
                                columns usi    varchar2(200)  PATH '.'    -- Prints A
                                       ,usi_v  varchar2(200)  PATH '/..//n3:Id'  --Prints Null
                                    );
0
Comment
Question by:Nakul Chawla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points (awarded by participants)
ID: 41771575
I don't have your table or data, so I made up my own.
For simplicity I left out namespace in my example, but you should be able to add that back in.

If not, post a full example xml and I can adjust as needed

DROP TABLE table1 PURGE;

CREATE TABLE table1
(
    xml_data   XMLTYPE,
    id         RAW(14)
);

INSERT INTO table1
     VALUES (xmltype('<whatever>
<Identifier>
  <issue Scheme="valueX">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>
<Identifier>
  <issue Scheme="valueY">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>
<Identifier>
  <issue Scheme="valueZ">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>
<Identifier>
  <issue Scheme="value1">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>
<Identifier>
  <issue Scheme="value9999">A</issue>
  <Id IdScheme="value2">B</Id>
</Identifier>
</whatever>'  ), HEXTORAW('FFA2F897DD7C30E030CEB15088FE'));

WITH mydata
     AS (SELECT xml_data xx
           FROM table1
          WHERE id = HEXTORAW('FFA2F897DD7C30E030CEB15088FE'))
SELECT x.*
  FROM mydata,
       XMLTABLE('//Identifier/issue[@Scheme="value1"]/parent::node()'
                PASSING mydata.xx
                COLUMNS usi VARCHAR2(20) PATH './issue/text()', usi_v VARCHAR2(200) PATH './Id/text()') x;

Open in new window

1
 

Author Comment

by:Nakul Chawla
ID: 41771715
Oh yes it worked. I just didnt know how to go to the parent node. The /parent::node() helped.
Thanks a lot.
0
 

Author Comment

by:Nakul Chawla
ID: 41771738
Hey need further help.
So now i am able to fetch the values but this has to be stored in a another table. I need to fetch value for around 15 million rec.
The explain plan gives a collection iterator pickler fetch and the query keeps on running.
create table temp as
with mydata as
( select  * from temp_3_a_pp
)  
select *
from  mydata,
xmltable(
'//Identifier/issuer [@Scheme="value1"]/parent::node()'                
                                PASSING mydata.xx
                                columns usi varchar2(200)  PATH './issuer'
                                , usi_v varchar2(200)  PATH './Id '

        );      

Plan Hash Value  :

-----------------------------------------------------------------------------------------------------------------------
| Id | Operation                              | Name                  | Rows        | Bytes         | Cost     | Time |
-----------------------------------------------------------------------------------------------------------------------
|  0 | CREATE TABLE STATEMENT                 |                       | 12695914464 | 1104544558368 | 39065237 |      |
|  1 |   LOAD AS SELECT                       | TEMP_CFTC_USI         |             |               |          |      |
|  2 |    OPTIMIZER STATISTICS GATHERING      |                       | 12695914464 | 1104544558368 |   549266 |      |
|  3 |     NESTED LOOPS                       |                       | 12695914464 | 1104544558368 |   549266 |      |
|  4 |      PX COORDINATOR                    |                       |             |               |          |      |
|  5 |       PX SEND QC (RANDOM)              | :TQ10000              |     1554348 |     132119580 |    19243 |      |
|  6 |        PX BLOCK ITERATOR               |                       |     1554348 |     132119580 |    19243 |      |
|  7 |         TABLE ACCESS FULL              | TEMP_3_A_PP           |     1554348 |     132119580 |    19243 |      |
|  8 |      COLLECTION ITERATOR PICKLER FETCH | XQSEQUENCEFROMXMLTYPE |        8168 |         16336 |       26 |      |
-----------------------------------------------------------------------------------------------------------------------
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 41771742
you have no where clause on temp_3_a_pp, so you're going to do a full table scan of it and read all of the xml of every row.

If you can specify the path more explicitly than //Identifier,  that will make the searches more efficient.

Also, is it a true statement that there will only be one node you want? If so, you can tune the path for that as well.

For example, with my simple example above, this is a more efficient search to get only the first matching node.

WITH mydata
     AS (SELECT xml_data xx
           FROM table1
          WHERE id = HEXTORAW('FFA2F897DD7C30E030CEB15088FE'))
SELECT x.*
  FROM mydata,
       XMLTABLE('(/whatever/Identifier/descendant::issue[@Scheme="value1"])[1]/parent::node()'
                PASSING mydata.xx
                COLUMNS usi VARCHAR2(20) PATH './issue/text()', usi_v VARCHAR2(200) PATH './Id/text()') x;

Open in new window



the "PICKLER" is the XMLTABLE.    When you select from any table function in oracle, the operation of extracting data from the function into a queryable result set is the PICKLER.  I don't know why they called it that; but that's the operation.
0
 

Author Comment

by:Nakul Chawla
ID: 41771769
Can't do that as the identifier tab is sometimes under a different tab. Depending on the type of xml is sent  or received. So there is some extra parent element for it.
Yes only the issue and Id values needs to be fetched from xml, for all the records in temp_3_a_pp. Actually this is a temp table which has been created from the main table whose xml needs to be looked upon.
Any suggestion?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41771775
if the Identifier node will have variable depth then // will be needed
but you can still apply a filter that will stop searching after it finds the first matching node, assuming you only want one for each xml doc.

SELECT x.*
  FROM mydata,
       XMLTABLE('(//Identifier/descendant::issue[@Scheme="value1"])[1]/parent::node()'
                PASSING mydata.xx
                COLUMNS usi VARCHAR2(20) PATH './issue/text()', 
                                   usi_v VARCHAR2(200) PATH './Id/text()') x;

Open in new window

0
 

Expert Comment

by:nk student
ID: 41772150
this still takes a collection iterator pickler fetch and it is running for more than an hour
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41772204
nk student - how do you know? Do you have the asker's data?

Also, the pickler is unavoidable using xmltable.
As I stated earlier, the pickler operation is what pulls data from xmltable as a result set.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Append data to an XML file 3 32
Select the 2 most recent visit dates 5 31
Oracle programming for starter 14 75
Force XMLSEQUENCE to return empty tags for null values. 10 45
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question