在这里,我有一个从 XML 中提取费率的代码:
with tbl as
(
select
XMLType(
'<exchange_rates>
<rate units="1" code="AUD">2.6</rate>
<rate units="1" code="THB">0.1</rate>
<rate units="1" code="BRL">0.9</rate>
<rate units="1" code="BGN">2.2</rate>
<rate units="1" code="CAD">2.9</rate>
<rate units="100" code="CLP">0.5</rate>
</exchange_rates>'
) xml
from
t_temp
)
select
extractValue(value(t), 'rate') result
from
tbl t,
table(XMLSequence(t.xml.extract('//rate'))) t;
这段代码的结果是
RESULT
------
2.6
0.9
2.2
2.9
0.5
没关系,但我还想获得属性值并获得如下结果:
Units Code RESULT
----- ---- ------
1 AUD 2.6
1 BRL 0.9
1 BGN 2.2
1 CAD 2.9
100 CLP 0.5
有没有办法做到这一点?
Oracle 的 XML 函数采用 XPath 表达式,因此您可以使用@attributename语法来标识属性。此外,您可以使用该XMLTABLE
功能使结构更简单/更清晰。
with tbl as
(
select
XMLType(
'<exchange_rates>
<rate units="1" code="AUD">2.6</rate>
<rate units="1" code="THB">0.1</rate>
<rate units="1" code="BRL">0.9</rate>
<rate units="1" code="BGN">2.2</rate>
<rate units="1" code="CAD">2.9</rate>
<rate units="100" code="CLP">0.5</rate>
</exchange_rates>'
) xmldata
from
dual
)
select units, code, rate
from tbl,
xmltable('/exchange_rates/rate'
PASSING tbl.xmldata
COLUMNS rate NUMBER PATH '.',
code VARCHAR2(3) PATH './@code',
units NUMBER PATH './@units');
+-------+------+------+ | UNITS | CODE | RATE | +-------+------+------+ | 1 | AUD | 2.6 | | 1 | THB | 0.1 | | 1 | BRL | 0.9 | | 1 | BGN | 2.2 | | 1 | CAD | 2.9 | | 100 | CLP | 0.5 | +-------+------+------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句