Solved

Extract value of child from xmltable

Posted on 2016-08-26
9
25 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

789 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