我正在尝试使用以下查询(在游标函数中使用)解析 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,它们允许多个命名空间;尽管动态提供命名空间并不容易。
关于使用 xmlsequence 提取函数 (PL/SQL) 进行 XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66216172/