Oracle SQL Generate nested xml -


i want nested xml using 1 query in plsql function oracle.

the database (cannot changed):

table 'products_details':

`attr_id` | `attribute`    | `fk_parent_id`(foreign key on `attr_id`) ------------------------------------------------------------------------------- 1         |  name           | null 3         |  sizes          | null 4         |  size_women     | 3 5         |  size_man       | 3 6         |  size_dimension | 3 

table 'product_contents':

`detail`      | `value`   |  variation_number  | `product_id` (doesnt matter) ------------------------------------------------------------------------------- name          |  tshirt   | null               | 1000 price         |  14.99    | null               | 1000 size_man      |  xl       | 1                  | 1000 size_women    |  l        | 1                  | 1000 size_dimesion |  21x25cm  | 1                  | 1000 size_man      |  m        | 2                  | 1000 size_women    |  s        | 2                  | 1000 size_dimesion |  14x16cm  | 2                  | 1000 ... 

as can see there options (name, price) once each product there options (size_man, size_woman...) variations , can exist more 1 time each product.

what want xml:

<attribute detail="name">tshirt</attribute> <attribute detail="price">14.99</attribute> <attribute detail="sizes">       <row variation_number="1">           <attribute detail="size_man">xl</attribute>           <attribute detail="size_women">l</attribute>           ...       </row>       <row variation_number="2">           <attribute detail="size_man">m</attribute>           <attribute detail="size_women">s</attribute>       </row> </attribute> 

what tried far (which of course not working):

select  (     xmlelement(  "attribute",                 xmlattributes(pc.detail "detail"),                 (select xmlagg                 (                  xmlelement("row", xmlattributes(pc.variant_number "variation_number") )                  )                  product_contents pc                   join product_details pd on pc.detail = pd.attribute , pc.product_id = '1000'                  pd.fk_parent_id = pd.id                )        ).getclobval() content  product_details pd    pd.fk_parent_id null order pd.attribute; 

how can single query?

here are:

with   -- "memory table"   product_details (     select 1 attr_id, 'name' attr, null parent_id dual union      select 2, 'price', null dual union      select 3, 'sizes', null dual union      select 4, 'size_women', 3 dual union      select 5, 'size_man', 3 dual union      select 6, 'size_dimension', 3 dual   ),   -- "memory table"   product_contents (     select 'name' detail, 'tshirt' value, null variation, 1000 product_id dual union     select 'price', '14.99', null, 1000 dual union     select 'size_man', 'xl', 1, 1000 dual union     select 'size_women', 'l', 1, 1000 dual union     select 'size_dimesion', '21x25cm', 1, 1000 dual union     select 'size_man', 'm', 2, 1000 dual union     select 'size_women', 's', 2, 1000 dual union     select 'size_dimesion', '14x16cm', 2, 1000 dual   ),   product_contents_xml (     select          variation,       detail,       xmlelement(         "attribute",         xmlattributes(detail "detail"),         value       ) attr,       product_id     product_contents   ),   attrs (       select         pc.attr               product_contents_xml pc join          product_details pd on pc.detail = pd.attr , pc.product_id = 1000               pd.parent_id null        union      select       xmlelement("attribute", xmlattributes(t.attr "detail"), xmlagg(t.value)) attr     (             select             parent.attr,             xmlelement("row", xmlattributes(pc.variation "variation_number"), xmlagg(pc.attr)) value                       product_contents_xml pc join              product_details pd on pc.detail = pd.attr , pc.product_id = 1000 join             product_details parent on parent.attr_id = pd.parent_id                       pd.parent_id not null           group             parent.attr, pc.variation     ) t     group t.attr   ) select xmlagg(attr) attrs 

attrs view splited 2 parts - 1 attributes without parent_id , 1 parent_id.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -