简体   繁体   中英

Oracle - extracting XML version and encoding from XML prolog in XMLType

Let's say we have following XMLType in Oracle:

<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>

I need to get XML version and encoding values or whole XML prolog preferably with XML functions, f.ex. XMLTABLE. Is that possible? Or only with string/CLOB functions?

We use Oracle 19c.

Is that possible?

No, unfortunately, it isn't, because XML functions like XMLTable work with XMLType which parses your input xml (from your NLS_LANG charset if it is CLOB or Varchar2, and from specified charset if it is BLOB) and store it in own internal codepage and when you get it, oracle returns it in your NLS_LANG:

SQL> with v as (select q'[<?xml version="1.1" encoding="blabla"?><root/>]' as vXML from dual)
  2  select xmltype(vXML) x from v;

X
------------------------------------------------
<?xml version="1.1" encoding="US-ASCII"?>
<root/>

And look what if I change my NLS_LANG to UTF8:

SQL> with v as (select q'[<?xml version="1.1" encoding="blabla"?><root/>]' as vXML from dual)
  2  select xmltype(vXML) x from v;

X
-----------------------------------------
<?xml version="1.1" encoding="UTF-8"?>
<root/>


SQL> ! env | grep NLS_LANG
NLS_LANG=american_america.UTF8

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