Oracle:将varchar2字段中的xml实体转换为utf-8字符

maw*_*awi 4 oracle utf-8 latin1

我在一个表中有一个字段,它包含特殊字符的XML实体,因为该表是在latin-1中.例如" Hallöle slovenčina"("ö"在latin-1中,但"slovenčina"中的"č"必须由某些将值存储到数据库中的应用程序转换为实体)

现在我需要通过将XML实体转换为原始字符将表导出为utf-8编码文件.

Oracle中是否有可以为我处理此问题的函数,或者我是否真的需要为此创建一个巨大的键/值映射?

任何帮助是极大的赞赏.

编辑:我发现了这个功能DBMS_XMLGEN.convert,但它只适用于<,>&.不上&#NNN;:-(

Ola*_*son 7

您也可以使用国际化套餐:

UTL_I18N.unescape_reference('text')

非常适合将这些html实体更改为普通字符(例如将数据库从iso 8859P1移动到UTF-8后进行清理)


Dan*_* A. 6

我相信dbms_xmlgen的问题在于技术上只有五个XML实体.您的示例有一个数字HTML实体,与Unicode对应:

http://theorem.ca/~mvcorks/cgi-bin/unicode.pl.cgi?start=0100&end=017F

Oracle有一个功能UNISTR,在这里有用:

select unistr('sloven\010dina') from dual;
Run Code Online (Sandbox Code Playgroud)

我已经将269转换为010d上面示例中的十六进制等效值(在Unicode中U+010D).但是,您可以传递十进制数并执行如下转换:

select unistr('sloven\' || replace(to_char(269, 'xxx'), ' ', '0') || 'ina') from dual;
Run Code Online (Sandbox Code Playgroud)

编辑:PL/SQL解决方案:

这是我为你准备好的一个例子.这应该循环并替换您从表中选择的每一行的任何出现次数.

create table html_entities (
    id NUMBER(3),
    text_row VARCHAR2(100)
);

INSERT INTO html_entities 
VALUES (1, 'Hallöle sloven&#269;ina &#266; &#250;');

INSERT INTO html_entities 
VALUES (2, 'I like the letter &#266;');

INSERT INTO html_entities 
VALUES (3, 'Nothing to change here.');

DECLARE
    v_replace_str NVARCHAR2(1000);
    v_fh UTL_FILE.FILE_TYPE;       
BEGIN
    --v_fh := utl_file.fopen_nchar(LOCATION IN VARCHAR2, FILENAME IN VARCHAR2, OPEN_MODE IN VARCHAR2, MAX_LINESIZE IN BINARY_INTEGER);

    FOR v_rec IN (select id, text_row from html_entities) LOOP
        v_replace_str := v_rec.text_row;
        WHILE (REGEXP_INSTR(v_replace_str, '&#[0-9]+;') <> 0) LOOP
            v_replace_str := REGEXP_REPLACE(
                v_replace_str, 
                '&#([0-9]+);',
                unistr('\' || replace(to_char(to_number(regexp_replace(v_replace_str, '.*?&#([0-9]+);.*$', '\1')), 'xxx'), ' ', '0')),
                1,
                1
            );
        END LOOP;

        -- utl_file.put_line_nchar(v_fh, v_replace_str);
        dbms_output.put_line(v_replace_str);

    END LOOP;
    --utl_file.fclose(v_fh);
END;
/
Run Code Online (Sandbox Code Playgroud)

请注意,我已经调用了UTL_FILE函数来将NVARCHAR行(Oracle的扩展字符集)写入数据库服务器上的文件.dbms_output虽然非常适合调试,但似乎不支持扩展字符,但如果您使用UTL_FILE写入文件,这应该不是问题.这是DBMS_OUTPUT:

Hallöle slovencina C ú
I like the letter C
Nothing to change here.
Run Code Online (Sandbox Code Playgroud)