Solved

writing oracle xml query

Posted on 2011-09-22
6
605 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
  • 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 73

Accepted Solution

by:
sdstuber earned 500 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 73

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
any soltion create a connection to import XML file from website 4 38
case statement in where clause 5 42
data lookup in Oracle - need suggestions 55 102
SQL Query 34 81
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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 about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now