How to escape <, >, and & characters to html entities in Oracle PL/SQL

我只是一个虾纸丫 提交于 2019-11-30 08:12:18

You can use the htf.escape_sc function:

SQL> select htf.escape_sc('Please escape <this> tag') from dual;

HTF.ESCAPE_SC('PLEASEESCAPE<THIS>TAG')
------------------------------------------------------------------
Please escape &lt;this&gt; tag

Also available is DBMS_XMLGEN.CONVERT which can handle a clob.

Example:

select DBMS_XMLGEN.CONVERT('<foo>') from dual

Details: https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_xmlgen.htm

HTF.BDY :
/* SPECIAL FUNCTIONS */
function escape_sc(ctext in varchar2 character set any_cs)
         return varchar2 character set ctext%charset is
begin return(replace(
             replace(
             replace(
             replace(ctext, '&', '&' || 'amp;'),
                            '"', '&' || 'quot;'),
                            '<', '&' || 'lt;'),
                            '>', '&' || 'gt;'));
end;

You can create this function yourself

But better use this variant of function dbms_xmlgen.convert

SQL> select dbms_xmlgen.convert('<test>&''"</test>') from dual
  2  /

DBMS_XMLGEN.CONVERT('<TEST>&''"</TEST>')
--------------------------------------------------------------------------------
&lt;test&gt;&amp;&apos;&quot;&lt;/test&gt;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!