I cannot come to a solution to this task: my goal is to pass a cursor to a PL/SQL procedure and get the results as an XMLType. The function dbms_xmlgen.getxmltype()
makes this task straightforward
<ROWSET>
<ROW>
<FIRST_NAME>John</FIRST_NAME>
<LAST_NAME>Goodman</LAST_NAME>
<HIRE_DATE>22-JUN-2011</HIRE_DATE>
</ROW>
</ROWSET>
Now I want to add the cursor column data type as an attribute to each corresponding XML element.
<ROWSET>
<ROW>
<FIRST_NAME type="VARCHAR2">John</FIRST_NAME>
<LAST_NAME type="VARCHAR2">Goodman</LAST_NAME>
<HIRE_DATE type="DATE">22-JUN-2011</HIRE_DATE>
</ROW>
</ROWSET>
This could be done using dynamic SQL, so I can write a PL/SQL function to get an associative array mapping each column to the corresponding data type.
Supposing I have both the aforementioned associativa array and the XMLType, how can I apply a set of transformations using a XPATH expression such as
-- pseudocode ;)
func(myXMLType, '//FIRST_NAME', ?add attribute to the matching node?)
Any other approach to get the job done will be fine
You could convert your metadata information to its own XML representation, and then have an XPath that finds the matching entry:
Each ROWSET (there's only one, of course) generates a new ROWSET element; then each ROW under that generates a new ROW element; then each mode under that generates a new node with the same name and value, but the name is also used to find the matching entry in the metadata and extract it's type attribute and use it as an attribute for this node instead.
A worked example:
Then an block that generates a cursor - similar to your example but with two rows just to check that works - and then calls the function to get the modified XML:
You may want more data types defined in the CASE, of course.