Solved

writing oracle xml query

Posted on 2011-09-22
6
602 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
UNIX SCP 5 48
Oracle TEXT search question 9 29
Outer Query not returning data - SQL HELP 16 41
Create html table using xsl 8 11
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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…
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

759 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

18 Experts available now in Live!

Get 1:1 Help Now