Solved

XML Forest,XML Concat

Posted on 2009-07-14
17
1,622 Views
Last Modified: 2012-05-07
Hi,

I have a wo function in oracle which returns XML type.

Sample XML From First Function
<customer>
<Fname>testFname</Fname>
<Lname>testLname</Lname>
</customer>

Sample XML From Second Function
<PostalAddress>
<address1>testaddress1</Fname>
<address2>testaddress2</Lname>
</PostalAddress>

I need to add postal address tag under customer tag.

Oupt Put

<Customers>
<customer>
<Fname>testFname</Fname>
<Lname>testLname</Lname>
<PostalAddress>
<address1>testaddress1</Fname>
<address2>testaddress2</Lname>
</PostalAddress>
</customer>
</Customers>

how to achieve this ?

Regards,
Chandan N




--Presenly i am using the below code
--but not able to generate the required XMl structure
SELECT  XMLElement("Customers",function1(),Function2()) INTO xml_temp FROM dual;
please find the attachment for the present code

Open in new window

query.txt
0
Comment
Question by:Chandan_Gowda
  • 4
  • 3
  • 3
17 Comments
 
LVL 47

Expert Comment

by:schwertner
ID: 24849170
Curious what will give you
SELECT  XMLElement("Customers",function1() || Function2()) INTO xml_temp FROM dual;
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 24850001
you don't want to use xmelement or any of the xml aggregation functions,
you are simply inserting nodes within other nodes.

use INSERTCHILDXML to do that...


select INSERTCHILDXML(
           xmltype('<Customers></Customers>'),
           '/Customers',
           'customer',
           INSERTCHILDXML(function1, '/customer', 'PostalAddress', function2)
       ) from dual
0
 
LVL 47

Expert Comment

by:schwertner
ID: 24852430
Thanks!
0
 
LVL 7

Author Comment

by:Chandan_Gowda
ID: 24877023
Finally by some R & D,made this working
find the attachment for  solution

select insertChildXML(xml_cust, '/Customer', 'PostalAddress', xml_postal, null) into xml_temp 

from dual;

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24878264
that appears to be what I posted except you have removed the outer <Customers>  node
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Expert Comment

by:schwertner
ID: 24878355
Thanks!
0
 
LVL 7

Author Comment

by:Chandan_Gowda
ID: 24921849
i resolved it myself....Please refund my points back
0
 
LVL 7

Author Comment

by:Chandan_Gowda
ID: 24921885
The core part is to get the nodes;sdstuber solution didnt had that part correct.That is the main element of the solution.How can this comment be considered as a correct one if it is not working,I had to do lot of work around to get the solution,Only the syntax is correct,But the logic is not correct,
Before accepcting the solution forcibly,The solution has to be checked for its logic.
Please refund my points back.
0
 
LVL 47

Expert Comment

by:schwertner
ID: 24924637
I will suggest to delete the whole question.

Also please regard the will of the Asker.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24931088
Chandan_Gowda,

Sorry, I was gone on vacation for several days and just got back to see the comments.

I didn't have the your functions to work with so I wrote my own that returned the xml presented in the original question (except I corrected the address1 and address2 closing tags)
The post I made above is exactly what I tested with and it returned exactly the xml you requested.

The query I posted returns the requested xml, if this does not work, please post what did not work about it.
I've attached the functions from my test case.

<Customers>
  <customer>
    <Fname>testFname</Fname>
    <Lname>testLname</Lname>
    <PostalAddress>
      <address1>testaddress1</address1>
      <address2>testaddress2</address2>
    </PostalAddress>
  </customer>
</Customers>

 

create or replace function function1 return xmltype

is

begin

  return xmltype('<customer>

<Fname>testFname</Fname>

<Lname>testLname</Lname>

</customer>');

end;
 

--  Note, address1 and address2  closing tags have been corrected, from the original question's sample data.

create or replace function function2 return xmltype

is

begin

  return xmltype('<PostalAddress>

<address1>testaddress1</address1>    

<address2>testaddress2</address2>

</PostalAddress>');

end;

Open in new window

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.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

746 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

11 Experts available now in Live!

Get 1:1 Help Now