这里我有一个从XML中提取费率的代码:
BSchema
此代码的结果为
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
有办法吗?
答案 0 :(得分:2)
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 | +-------+------+------+