Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:05:37+00:00 2026-06-13T14:05:37+00:00

Using Oracle 10gR2, I need to produce something like the following pseudo-example from data

  • 0

Using Oracle 10gR2, I need to produce something like the following pseudo-example from data stored in standard relational tables.

<product>
  <productName>p1</productName>
  <productNumber>100</productNumber>
  <productObsoletes>
     <obsoletedProduct label=1>50</obsoletedProduct>
     <obsoletedProduct label=2>55</obsoletedProduct>
  </productObsoletes>
</product>

The problem is, I need to do decision making using the rows which contain the data. My database (which I inherited) is poorly designed and the logic needed to decide upon a rows inclusion is complex. Unfortunately, redesigning the database is not an option. I’m vastly simplifying the logic here, so it’s not a case where a simple join or where clause can be used. There’s a complex hierarchy to the data, and the data format definition.

In psuedo code, the way-oversimplified decision would look something like:

BEGIN
   --select our basic attributes

     select XMLEMENT("product",XMLELEMENT("productName",name),XMLELEMENT("productNumber",product_number))
into xml_output from products where product_number = 100;

   --now process our obsolete rows
   select XMLELEMENT("productObsoletes") into xml_output from dual;
   FOR c_row in (select * from product_obsoletes where id=100)
   LOOP
       IF c_row.display = 'YES' THEN
        select XMLELEMENT("obsoletedProduct", XMLATTRIBUTES(c_row.label as "label"), c_row.obsoleted_product_id) into xml_output from dual;
       ELSE
          CONTINUE;
       END IF;
   END LOOP;
END;

Obviously though, this doesn’t work. First, XMLElement always puts a closing tag on, so unless you can formulate a single select statement containing all of your elements, it’s not going to work. Second, in this example, I’d be overwriting the previous XMLELEMENT output. I can’t concatenate, because XMLELEMENT has already closed the tag.

The only solution I can think of at this point is to select the various XMLElement pieces I need into different VARCHARS and then use string manipulation functions to find the right spot to insert the various pieces. Ugly with a capital-U.

Is there another solution? Is there perhaps a way to prevent XMELEMENT from closing the tag so that you can you use complex looping logic? Or another Oracle construct which would allow me to achieve my goal of building up this XML without having to use a single select statement?

My path of last resort would be to do the XML generation from another language such as Java or Perl where I could simply fetch the data and then do my complex processing and XML generation there. However, I’d much prefer just to have a SQL function which would return the correct XML as a string, so if it’s possible to do it in PL/SQL, that’d be my preferred route.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:05:38+00:00Added an answer on June 13, 2026 at 2:05 pm

    How about using XMLDOM (a different inbuilt package) to create the XML instead?

    You’ll probably need to write more code, but if your logic is sufficiently complicated this is what you’re going to have to do anyway.


    This is a good quick summary of a few options you could use (including XMLDOM), with examples:

    http://www.akadia.com/services/ora_gen_xml.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Oracle 10.2 and have the following query: select h.company, count(*) from history
Using Oracle 11 I'm wanting to pull data from a table, and add a
Im using Oracle, BlazeDS, Java & Flex. I have an ArrayCollection containing data from
I am using Oracle 11GR2 version. I exported a dump successfully using the following
Using Oracle, how can I find index names and creation dates from systables/information_schema? How
I'm using Oracle 11g Schema I want to select the month and day from
Using Oracle 11g release 2, the following query gives an ORA-01790: expression must have
(Using Oracle) I have a table with key/value pairs like this: create table MESSAGE_INDEX
Using Oracle 10g, I need to rename a bunch of FK constraints which all
I'm using oracle with orbeon and I would like to use the flat view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.