I've written simple Oracle queries to extract XMLTYPE data before, but this XML is different - I need to pull information from attributes, child elements, and their respective attributes. I also would like to write an INSERT statement as well (preferably one that is able to find the highest option value and add 1). Consider the following XML:
<metadata>
<fields>
<field name="cusInt01" label="Reference point">
<option value="1">CB</option>
<option value="2">CF</option>
<option value="3">DF</option>
<option value="4">EKB</option>
<option value="5">ES</option>
<option value="6">GL</option>
<option value="7">GR</option>
<option value="8">KB</option>
<option value="9">KBE</option>
<option value="10">MSL</option>
<option value="11">PT</option>
<option value="12">RB</option>
<option value="13">RF</option>
<option value="14">RT</option>
<option value="15">UN</option>
<option value="16">UNK</option>
</field>
</fields>
</metadata>
I can write, for instance, a query to extract all of the field names:
select
field_names.*
FROM
metadata m,
XMLTABLE('/metadata/fields/field'
PASSING xmltype(m.xml_string)
COLUMNS field_name VARCHAR(32) PATH '@name') field_names;
How do I write a query that can extract all the different information in a tabular form? How do I, for instance, display it as:
field_name | field_label | option_value | option_label
cusInt01 Reference point 1 CB
cusInt01 Reference point 2 CF
cusInt01 Reference point 2 DF
... etc. Thoughts? I've been trying to cobble a query together but so far spinning my wheels.
In your example, the data is at multiple levels. Each
field
can have manyoption
. So, you must break upfield
as well asoption
elements with XMLTable. First, you should breakfield
elements, whereoption
elements are mapped as XMLType. Then pass it to second XMLTable to further break it down.Data Setup:
Query:
Result:
Similar example at Oracle XML DB Developer's Guide.
In addition to Eat A Peach's answer, I developed some INSTEAD OF queries to insert and update data. They're still in development so a tad rough but I figured I'd post them here in case someone might find them handy.