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

标签 xml oracle plsql namespaces

我正在尝试使用以下查询(在游标函数中使用)解析 XML 文件:
enter image description here
我面临的问题是我的 XML 文件包含 2 个命名空间,如下所示:
enter image description here
如果只有一个 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/

相关文章:

c# - 如何删除XML中未关闭的节点?

java.xml.bind 从外部文件创建 XMLStreamReader

forms - 使用OATS的Oracle Forms Automation

HTML 和 JSP 与 Oracle 使用 tomcat

sql - 使用单个 PL SQL 查询返回多个计数

sql - 在 Oracle 的过程中重用选择查询

oracle - PL/SQL : Contradiction in Oracle document on Implicit Rollbacks

java - 应用程序启动时调用 onCreateOptionsMenu 方法

xml - 使用xpath部分搜索属性值

java - 动态向数据库表添加列?