I will start with what my desired output is:
<?xml version="1.0"?>
<Accounts>
<Account>
<variable1>variable1</variable1>
<variable2>variable2</variable2>
<variable3>variable3</variable3>
<variable4>variable4</variable4>
<variable5>variable5</variable5>
<variable6>variable6</variable6>
<Table>
<Row>
<Column1>row1col1</Column1>
<Column2>row1col2</Column2>
<Column3>row1col3</Column3>
</Row>
<Row>
<Column1>row2col1</Column1>
<Column2>row2col2</Column2>
<Column3>row2col3</Column3>
</Row>
<Row>
<Column1>row3col1</Column1>
<Column2>row3col2</Column2>
<Column3>row3col3</Column3>
</Row>
</Table>
</Account>
...
</Accounts>
The SQL function this needs to come from currently loops through Accounts and for each account ID gathers all the relevant data above for each account, e.g. variable1, 2, table, etc. So per Account ID it returns all the variables and the table with its 3 columns and all rows.
So to simplify, for every Account ID, the XML file needs the above structure appended to the file. Now my dilemma comes when I try to create the XML file with all these varied parts.
The first issue is how to combine the Table information and the variable information into a single XML tree. I know it is possible to create an XML Forest with Elements from a table, but the issue is that the for each account there is a set of variable data and a table. I have considered table_to_xml but this takes directly from a table and I have no idea how I would append the variable elements before the table/xml.
I have considered doing text arrays:
for rec in execute cmd
loop
cmd:= 'select xmlelement(name "Account",
xmlelement(name "Variable1", '||var1||'))';
execute cmd into vtext
varray[n] := vtext;
n++
end loop;
And then possibly putting the table nested into the xmlelement as a text array as well. Keep nesting until I have all the 's and then send those to XML after they are all together.
The next issue is combining all these Accounts into one overarching element within the same XML file.
If anyone has any ideas of how to approach solving this I would greatly appreciate the help. I feel like I am over thinking the entire process, or maybe it is too ridiculous to do in SQL and maybe I should try these operations in a scripting language like Python.