Solved

Extract value of child from xmltable

Posted on 2016-08-26
9
19 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
  • 4
  • 3
9 Comments
 
LVL 73

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
 
LVL 73

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 73

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 73

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now