sql - How to get formatted XML out of Oracle -
i'm inexperience oracle , having trouble exporting data xml. i've managed query working, format of xml seems fixed , won't work me. here's query:
select value(em).getclobval() "output" table(xmlsequence(cursor ( select * usermain ) )) em what is:
<row><status>active</status><name>joe smith<name><phone>234-2345</phone>...</row> <row><status>inactive</status><name>sally smith<name><phone>234-4444</phone>...</row> etc. but want this, xml tags "rows" around outside of output, shown here:
<rows> <record><status>active</status><name>joe smith<name><phone>234-2345</phone>...</record> <record><status>inactive</status><name>sally smith<name><phone>234-4444</phone>...</record> etc. </rows> so need query change , put outer tags around output?
there trick i've found years ago. if use xmltype , apply xsl template xmltype using oracle transform function, there unexpected behaviour: xml becomes formatted. unexpected , point of view funny.
this procedure trick:
procedure beautify(xmlout in out nocopy clob) xml xmltype := new xmltype(xmlout); xsl xmltype := new xmltype('<?xml version="1.0" encoding="iso-8859-1"?><document></document>'); tmp xmltype; begin tmp := xml.transform(xsl,null); xmlout := xml.getclobval; if tmp null null; end if; end; of course, should pass function valid xmltype, e.g. in case should kind of
<?xml version="1.0" encoding="iso-8859-1"?> <document> <row><status>active</status><name>joe smith<name><phone>234-2345</phone>...</row> <row><status>inactive</status><name>sally smith<name><phone>234-4444</phone>...</row> </document> please, note encoding="iso-8859-1" used german language, should change use encoding purposes.
if want use in query can create function instead of procedure. can add additional rows before formatting , remove after formatting.
this works in oracle 11.2
Comments
Post a Comment