简体   繁体   中英

Oracle SQL XML Functions - How to get encoding in XML Prolog?

This SQL

SELECT XMLRoot(XMLType('<poid>143598</poid>'), VERSION '1.0', STANDALONE YES)
  AS xmlroot FROM DUAL; 

generates an output as follows

XMLROOT
--------------------------------------
<?xml version="1.0" standalone="yes"?>
<poid>143598</poid>

How can get encoding in my xml prolog?

Ex - I want output to be something like

XMLROOT
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<poid>143598</poid>

Reference -

Generate XML Data from the database

select xmlroot (xmltype ('<poid>143598</poid>')
                  , version '1.0" encoding="UTF-8'
                  ) "XMLRoot"
  from dual;

Weird ... but looks like version argument can have anything in it -

replace

version '1.0'

with

version '1.0" encoding="utf-8'

output

XMLROOT
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<poid>143598</poid>

It is easy but let me explain the possible logic of Oracle:

Character data are stored in database encoding by default. If you want to specify encoding then probable it is different from database encoding. OK, Let it be BLOB, ie octet stream represented in desired encoding. So we should use XMLSERIALIZE function to create representation in any encoding (including the default DB encoding as well)

select  
  xmlserialize(document xmltype('<Envelop>Any UTF charachers. Tous les caractères UTF. כל תווי UTF </Envelop>') as blob encoding 'UTF-8' version '1.0')
from dual;

If your default DB encoding is UTF-8 then you can also wrap this call into to_clob(…) or even to_char(…) to see the result. For me

select  
  to_char(xmlserialize(document xmltype('<Envelop>Any UTF charachers. Tous lescaractères UTF. כל תווי UTF </Envelop>') as blob encoding 'UTF-8' version '1.0'))
from dual;

Gives:

<?xml version="1.0" encoding="UTF-8"?>
<Envelop>Any UTF charachers. Tous les caractères UTF. כל תווי UTF </Envelop>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM