Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Return XML Format with Grouping

Nick CliniteMay 14 2013 — edited May 14 2013
Allow me to preface this with the notice that I am not familiar with XML outside of its hierarchical structure, and am not familiar with what you can do with it using formatting.

As an example, let us say you have the following table:

Object_Type | Object_Name | Descriptor |
------------------------------------------------------------
Fruit | Apple | Crunchy |
Fruit | Orange | Sour |
Utensil | Pencil | Wooden |

Now let's say you want to query this table to return an XML format, which will be used in a web site to display the information, and you want to group the display by Object_Type, so that you want an XML format like this:

<Object Group>
<Object Type>Fruit</Object Type>
<Object>
<Object Name>Apple</Object Name>
<Descriptor>Crunchy</Descriptor>
</Object>
<Object>
<Object Name>Orange</Object Name>
<Descriptor>Sour</Descriptor>
</Object>
</Object Group>

<Object Group>
<Object Type>Utensil</Object Type>
<Object>
<Object Name>Pencil</Object Name>
<Descriptor>Wooden</Descriptor>
</Object>
</Object Group>

However, from what I can tell, using the XMLELEMENT function, it appears the closest I can get is following:

SELECT XMLELEMENT("Object Group",
XMLELEMENT("Object Type", object_type),
XMLELEMENT("Object",
XMLELEMENT("Object Name", object_name),
XMLELEMENT("Descriptor", descriptor)
)
)
FROM object_tbl;


<Object Group>
<Object Type>Fruit</Object Type>
<Object>
<Object Name>Apple</Object Name>
<Descriptor>Crunchy</Descriptor>
</Object>
</Object Group>

<Object Group>
<Object Type>Fruit</Object Type>
<Object>
<Object Name>Orange</Object Name>
<Descriptor>Sour</Descriptor>
</Object>
</Object Group>

<Object Group>
<Object Type>Utensil</Object Type>
<Object>
<Object Name>Pencil</Object Name>
<Descriptor>Wooden</Descriptor>
</Object>
</Object Group>

Is it possible to group it in a way so that Apple and Orange end up in the the same <Object Group>? Or is that meaningless and such grouping can be done on the web site itself by formatting the XML?

Edited by: Nick Clinite on May 14, 2013 9:59 AM
This post has been answered by Frank Kulash on May 14 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 11 2013
Added on May 14 2013
5 comments
115 views