从大于 4,000 个字符的 clob 中检索 XML 数据

Retrieving XML data from clob greater than 4,000 characters

我有以下 Oracle 查询 returns 来自 clob 列 (mytable.personalization_data) 的数据。如果 clob 值小于 4,00 个字符,它工作正常,但如果它更多,我会收到 "ORA-01706: user function result value was too large" 错误消息。

这里是 sql:

select name_str, value_str, order_str
from 
(
SELECT
XMLTYPE(EXTRACTVALUE(XMLTYPE(personalization_data), '/personalizations/personalization[1]/data')) persData 
FROM my_table
),
XMLTable('/Accessories/Personalization/PersonalizationItems'
    PASSING persData
    COLUMNS
        name_str  varchar2(100)  PATH 'DisplayName',
        value_str varchar2(2000) PATH 'Value',
        order_str varchar2(10)   PATH 'SortOrder'
);

样本XML:

<personalizations>
    <personalization>
        <data>
            <![CDATA[
            <Accessories>
                <AccessoryId>1234567</AccessoryId>
                <Personalization>
                    <PersonalizationItems>
                        <SortOrder>1</SortOrder>
                        <DisplayName>Last Name</DisplayName>
                        <Value>Veekoff</Value>
                    </PersonalizationItems>
                    <PersonalizationItems>
                        <SortOrder>2</SortOrder>
                        <DisplayName>First Name</DisplayName>
                        <Value>Ivana</Value>
                    </PersonalizationItems>
                </Personalization>
            </Accessories>
            ]]>
        </data>
    </personalization>
</personalizations>

有人可以提供建议吗?

所以...问题是 EXTRACTVALUE 仅 returns VARCHAR2。你用它来删除 <![CDATA[ 来自 data 节点的内容,因此您可以将其第二次解析为 XML。 事实证明,Oracle 允许您漂亮打印 XML 或在 SQL return VARCHAR2 中就地更改 clob 的大多数方法。

我想我想出了一些应该可行的方法,使用 xpath 函数摆脱 CDATA 包装器,并取消转义结果。这有点尴尬,我相信一定有更好的方法来做,但我找不到。

select name_str, value_str, order_str
from 
(
SELECT XMLTYPE(DBMS_XMLGEN.CONVERT(
    XMLQUERY('/personalizations/personalization[1]/data/substring(text(),9,string-length(text())-12)' passing XMLTYPE(personalization_data) returning content).getClobVal()
    , 1)) persData 
FROM my_table
),
XMLTable('/Accessories/Personalization/PersonalizationItems'
    PASSING persData
    COLUMNS
        name_str  varchar2(100)  PATH 'DisplayName',
        value_str varchar2(2000) PATH 'Value',
        order_str varchar2(10)   PATH 'SortOrder'
);

我想 plain substr 可以处理 clob,但改进不大。

select name_str, value_str, order_str
from 
(
SELECT XMLTYPE(substr( pData, 10, length(pData)-12)) persData 
from (select 
    XMLQUERY('/personalizations/personalization[1]/data/text()' passing XMLTYPE(personalization_data) returning content).getClobVal() as pData
    FROM my_table)
),
XMLTable('/Accessories/Personalization/PersonalizationItems'
    PASSING persData
    COLUMNS
        name_str  varchar2(100)  PATH 'DisplayName',
        value_str varchar2(2000) PATH 'Value',
        order_str varchar2(10)   PATH 'SortOrder'
);