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

x
?
Solved

writing oracle xml query

Posted on 2011-09-22
6
Medium Priority
?
616 Views
Last Modified: 2012-08-14
<node1>
 
  <node2 id=1>
    <text1>abc</text1>
    <text2>def</text2>
    <nest>
      <node3 id=1>ghi</node3>
      <node3 id=2>jkl</node3>
    </nest>
  </node2>

  <node2 id=2>
    <text1>xxx</text1>
    <text2>yyy</text2>
  </node2>

</node1>

Hi All,
Can somebody please advise me how to write an oracle xml query to extract the information in the following format from the above xml:

node2  text1  text2  node3_attribute  node3_text
  1     abc   def    1                ghi
  1     abc   def    2                jkl
  2     xxx   yyy  
 
I tried using the xmltable  feature but cannot extract all the information in a single query in the above format.
I am using oracle 10g.
Many thanks in advance.
0
Comment
Question by:subratoc
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
ID: 36584136
Try this
SELECT xmltype(DBMS_XMLGEN.getxml('select node2,  text1 , text2,  node3_attribute,  node3_text from ' || table_name)), '/ROWSET/ROW/X' FROM your_tabl
0
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
ID: 36584141
sorry Typo

SELECT xmltype(DBMS_XMLGEN.getxml('select node2,  text1 , text2,  node3_attribute,  node3_text from your_table')), '/ROWSET/ROW/X' FROM dual
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 36584895
SELECT EXTRACTVALUE(VALUE(x), '/node2/@id') node2,
       EXTRACTVALUE(VALUE(y), '//text1') text1,
       EXTRACTVALUE(VALUE(y), '//text2') text2,
       EXTRACTVALUE(VALUE(z), '//node3/@id') node3_attribute,
       EXTRACTVALUE(VALUE(z), '//node3') node3_text
  FROM XMLTABLE(
          '//node2'
          PASSING xmltype('<node1>
 
  <node2 id="1">
    <text1>abc</text1>
    <text2>def</text2>
    <nest>
      <node3 id="1">ghi</node3>
      <node3 id="2">jkl</node3>
    </nest>
  </node2>

  <node2 id="2">
    <text1>xxx</text1>
    <text2>yyy</text2>
  </node2>

</node1>')
      ) x
      JOIN XMLTABLE(
               '/node2'
               PASSING x.COLUMN_VALUE
           ) y
          ON 1 = 1
      LEFT OUTER JOIN XMLTABLE(
                          '//node3'
                          PASSING y.COLUMN_VALUE
                      ) z
          ON 1 = 1
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:subratoc
ID: 36585174
Hi ,
Thanks for your replies.
sdstuber, using the above query I cannot see any row corresponding to node2.
On running the query, I am getting the following:

node2  text1  text2  node3_attribute  node3_text
  1        abc     def       1                             ghi
  1        abc      def      2                             jkl

for some reason, I the left outer join is not being used and the following line is not displayed in the result:
node2  text1  text2
"  2     xxx   yyy  "

Regards.
 
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36586143
I don't know what to say,  I tested it, and it works fine for me...

SQL> /

NODE2
---------------------------------------------------------------------------
TEXT1
---------------------------------------------------------------------------
TEXT2
---------------------------------------------------------------------------
NODE3_ATTRIBUTE
---------------------------------------------------------------------------
NODE3_TEXT
---------------------------------------------------------------------------
1
abc
def
1
ghi

1
abc
def
2
jkl

2
xxx
yyy




SQL>
0
 

Author Closing Comment

by:subratoc
ID: 36586759
Thanks a lot sdstuber. Do not know if it is my version of oracle, but when I replaced the left outer join xmltable()z with (+)z it worked just fine.
Cheers.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 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.

721 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