XML 使用 xmlsequence 提取函数进行解析 (PL/SQL)

XML parsing using xmlsequence extract function (PL/SQL)

我正在尝试使用以下查询(在游标函数中使用)来解析 XML 文件:

我面临的问题是我的 XML 文件包含 2 个命名空间,如下所示:

如果只有一个 xmlns,我可以解决这个问题,方法是将 p_urn 定义为 xmlns 的值。

在我有 2 个 xmlns 的情况下,如何使用提取函数实现此目的?

可以通过跳过或通配符的部分或全部元素来做到这一点:

-- with only p_urn_1 - wildcard inner default namespace
select extractvalue(value(p), '/*:Header/*:VersionInfo/*:Version/text()', p_urn_1) as version
from table (xmlsequence(extract(xmltype(pp_xml), '/Document/*:Header', p_urn_1))) p;

-- with only p_urn_2 - wildcard outer default namespace
select extractvalue(value(p), '/Header/VersionInfo/Version/text()', p_urn_2) as version
from table (xmlsequence(extract(xmltype(pp_xml), '/*:Document/Header', p_urn_2))) p;

-- with only p_urn_2 - skip outer element
select extractvalue(value(p), '/Header/VersionInfo/Version/text()', p_urn_2) as version
from table (xmlsequence(extract(xmltype(pp_xml), '//Header', p_urn_2))) p;

-- without either p_urn_1 or p_urn_2 - wildcard everything
select extractvalue(value(p), '/*:Header/*:VersionInfo/*:Version/text()') as version
from table (xmlsequence(extract(xmltype(pp_xml), '/*:Document/*:Header'))) p;

-- with both p_urn_1 and p_urn_2 - single wildcard (safe-ish)
select extractvalue(value(p), '/Header/VersionInfo/Version/text()', p_urn_2) as version
from table (xmlsequence(extract(xmltype(pp_xml), '/Document/*:Header', p_urn_1))) p;

db<>fiddle

但是您使用了几个已弃用的函数。您应该查看 XMLQuery 和 XMLTable,它们允许多个名称空间;尽管动态提供命名空间并不容易。