Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extract value of child from xmltable

Posted on 2016-08-26
9
Medium Priority
?
34 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

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…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

705 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