从 XMLType Oracle 中检索 XML 个元素
Retrieving XML elements from XMLType Oracle
谁能帮我从 Oracle 中的 XMLType 列中检索数据?
drop table xml_analysis;
create table xml_analysis(id number,soft_attributes XMLType);
create table xml_softattributes(id number,soft_attributes varchar2(200));
INSERT INTO xml_analysis VALUES
( 1, XMLType(
'<softattributes>
<attr1>ABC</attr1>
<attr2>XYZ</attr2>
<attr3>PQR</attr3>
</softattributes>
'));
insert into xml_softattributes values(1,'attr1');
insert into xml_softattributes values(1,'attr2');
insert into xml_softattributes values(1,'attr3');
- Table xml_analysis 包含 xmltype 列,我不知道其属性
- Table xml_softattributes 包含软属性列表(不是 xpath),它们出现在 xml_analysis table
的 xmltype 列中
- Table 是根据 id
加入的
现在我的问题是使用 table xml_softattributes 从 xml_analysis table 动态检索数据,我该怎么做?
需要输出
Softattribute Value
=======================
attr1 ABC
attr2 XYZ
attr3 PQR
我能想到的可能解决方案是使用动态字符串并执行,但我不希望动态字符串查询来检索数据。
您可以使用 existsNode
和 extract
函数的组合,如下所示。
SELECT b.SOFT_ATTRIBUTES,
CASE
WHEN existsNode (a.soft_attributes ,'/*/'
||b.SOFT_ATTRIBUTES) = 1
THEN a.soft_attributes.extract('/*/'
||b.SOFT_ATTRIBUTES
||'/text()').getStringVal()
END value
FROM xml_analysis a,
xml_softattributes b
WHERE a.id = b.id;
*
用作通配符以匹配任何子节点。例如,/PO/*/STREET 匹配作为 PO 元素的孙元素的任何街道元素。
输出:
attr1 ABC
attr2 XYZ
attr3 PQR
如果属性集是固定的 ('attr1', 'attr2', 'attr3'
),那么您可以从 XML 结构中提取数据,然后逆透视。然后你可以按照通常的方式加入另一个table。
select id, softattribute, value
from (
select x.id, xt.attr1, xt.attr2, xt.attr3
from xml_analysis x,
xmltable('/softattributes'
passing x.soft_attributes
columns
attr1 varchar2(100) path 'attr1',
attr2 varchar2(100) path 'attr2',
attr3 varchar2(100) path 'attr3'
) xt
)
unpivot ( value
for softattribute in (attr1 as 'attr1', attr2 as 'attr2', attr3 as 'attr3')
)
;
ID SOFTATTRIBUTE VALUE
-- ------------- -----
1 attr1 ABC
1 attr2 XYZ
1 attr3 PQR
如果只能通过检查数据知道属性集,那么 UNPIVOT
将不起作用 - XMLTABLE
也不会起作用,因为 PATH
必须是一个字符串文字,在您编写代码时为您所知(或为您的动态查询编写过程所知),它无法在运行时学习。
如果这是一个问题,您可能需要重新考虑 XML 结构; 'attr1'
等应该是值,而不是标签,就像值一样。 (如果您打算在 EAV 模型中工作,请一直朝那个方向前进。)
谁能帮我从 Oracle 中的 XMLType 列中检索数据?
drop table xml_analysis;
create table xml_analysis(id number,soft_attributes XMLType);
create table xml_softattributes(id number,soft_attributes varchar2(200));
INSERT INTO xml_analysis VALUES
( 1, XMLType(
'<softattributes>
<attr1>ABC</attr1>
<attr2>XYZ</attr2>
<attr3>PQR</attr3>
</softattributes>
'));
insert into xml_softattributes values(1,'attr1');
insert into xml_softattributes values(1,'attr2');
insert into xml_softattributes values(1,'attr3');
- Table xml_analysis 包含 xmltype 列,我不知道其属性
- Table xml_softattributes 包含软属性列表(不是 xpath),它们出现在 xml_analysis table 的 xmltype 列中
- Table 是根据 id 加入的
现在我的问题是使用 table xml_softattributes 从 xml_analysis table 动态检索数据,我该怎么做?
需要输出
Softattribute Value
=======================
attr1 ABC
attr2 XYZ
attr3 PQR
我能想到的可能解决方案是使用动态字符串并执行,但我不希望动态字符串查询来检索数据。
您可以使用 existsNode
和 extract
函数的组合,如下所示。
SELECT b.SOFT_ATTRIBUTES,
CASE
WHEN existsNode (a.soft_attributes ,'/*/'
||b.SOFT_ATTRIBUTES) = 1
THEN a.soft_attributes.extract('/*/'
||b.SOFT_ATTRIBUTES
||'/text()').getStringVal()
END value
FROM xml_analysis a,
xml_softattributes b
WHERE a.id = b.id;
*
用作通配符以匹配任何子节点。例如,/PO/*/STREET 匹配作为 PO 元素的孙元素的任何街道元素。
输出:
attr1 ABC
attr2 XYZ
attr3 PQR
如果属性集是固定的 ('attr1', 'attr2', 'attr3'
),那么您可以从 XML 结构中提取数据,然后逆透视。然后你可以按照通常的方式加入另一个table。
select id, softattribute, value
from (
select x.id, xt.attr1, xt.attr2, xt.attr3
from xml_analysis x,
xmltable('/softattributes'
passing x.soft_attributes
columns
attr1 varchar2(100) path 'attr1',
attr2 varchar2(100) path 'attr2',
attr3 varchar2(100) path 'attr3'
) xt
)
unpivot ( value
for softattribute in (attr1 as 'attr1', attr2 as 'attr2', attr3 as 'attr3')
)
;
ID SOFTATTRIBUTE VALUE
-- ------------- -----
1 attr1 ABC
1 attr2 XYZ
1 attr3 PQR
如果只能通过检查数据知道属性集,那么 UNPIVOT
将不起作用 - XMLTABLE
也不会起作用,因为 PATH
必须是一个字符串文字,在您编写代码时为您所知(或为您的动态查询编写过程所知),它无法在运行时学习。
如果这是一个问题,您可能需要重新考虑 XML 结构; 'attr1'
等应该是值,而不是标签,就像值一样。 (如果您打算在 EAV 模型中工作,请一直朝那个方向前进。)