如何 return 相似 xml 元素作为 oracle 中的行
How to return similar xml elements as rows in oracle
我正在尝试将我的 oracle 数据库中的 xml 解析为 return 个单独的行。 xml 是我的 table 中一个名为 msg 的字段
样本 xml 是
<application xmlns="http://www.abcxyz.com/Schema/FCX/1"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<client>
<clientSource>
<amount>25000.0</amount>
<clientSourceTypeDd>1</clientSourceTypeDd>
<description>aasdadsf</description>
</clientSource>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>6</clientSourceTypeDd>
<description>wrewertwerewrt</description>
</clientSource>
<clientSource>
<amount>50000.0</amount>
<clientSourceType>10</clientSourceTypeDd>
<description>second and thirs</description>
</clientSource>
</client>
</application>
我尝试了以下查询,但没有按预期工作
SELECT EXTRACT(t.msg, '//application/client/clientSource[*]/clientSourceType/text()')
.getStringVal() clientSourceType,
EXTRACT(t.msg, '/pplication/client/clientSource/amount')
.getStringVal() clientSourceAmount
FROM table t
我想要达到的预期结果是
clientSourceType
clientSourceAmount
1
25000
6
25000
10
50000
请帮助解决,因为我是解析 xml 和 oracle 的新手。
谢谢
您可以使用 XMLTABLE
,这是 Oracle 推荐的而不是弃用的函数 EXTRACTVALUE
,之后通过将 clientSourceTypeDd
转换为 clientSourceType
来修复标签名称,以使开始和结束标签名称匹配,例如
WITH t( xml ) AS
(
SELECT XMLType('<application xmlns="http://www.abcxyz.com/Schema/FCX/1"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<client>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>1</clientSourceType>
<description>aasdadsf</description>
</clientSource>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>6</clientSourceType>
<description>wrewertwerewrt</description>
</clientSource>
<clientSource>
<amount>50000.0</amount>
<clientSourceType>10</clientSourceType>
<description>second and thirs</description>
</clientSource>
</client>
</application>')
FROM dual
)
SELECT "clientSourceType", "clientSourceAmount"
FROM t,
XMLTABLE( XMLNAMESPACES( DEFAULT 'http://www.abcxyz.com/Schema/FCX/1' ),
'/application/client/clientSource'
PASSING xml
COLUMNS
"clientSourceType" INT PATH 'clientSourceType',
"clientSourceAmount" INT PATH 'amount'
)
clientSourceType
clientSourceAmount
1
25000
6
25000
10
50000
我正在尝试将我的 oracle 数据库中的 xml 解析为 return 个单独的行。 xml 是我的 table 中一个名为 msg 的字段 样本 xml 是
<application xmlns="http://www.abcxyz.com/Schema/FCX/1"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<client>
<clientSource>
<amount>25000.0</amount>
<clientSourceTypeDd>1</clientSourceTypeDd>
<description>aasdadsf</description>
</clientSource>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>6</clientSourceTypeDd>
<description>wrewertwerewrt</description>
</clientSource>
<clientSource>
<amount>50000.0</amount>
<clientSourceType>10</clientSourceTypeDd>
<description>second and thirs</description>
</clientSource>
</client>
</application>
我尝试了以下查询,但没有按预期工作
SELECT EXTRACT(t.msg, '//application/client/clientSource[*]/clientSourceType/text()')
.getStringVal() clientSourceType,
EXTRACT(t.msg, '/pplication/client/clientSource/amount')
.getStringVal() clientSourceAmount
FROM table t
我想要达到的预期结果是
clientSourceType | clientSourceAmount |
---|---|
1 | 25000 |
6 | 25000 |
10 | 50000 |
请帮助解决,因为我是解析 xml 和 oracle 的新手。 谢谢
您可以使用 XMLTABLE
,这是 Oracle 推荐的而不是弃用的函数 EXTRACTVALUE
,之后通过将 clientSourceTypeDd
转换为 clientSourceType
来修复标签名称,以使开始和结束标签名称匹配,例如
WITH t( xml ) AS
(
SELECT XMLType('<application xmlns="http://www.abcxyz.com/Schema/FCX/1"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<client>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>1</clientSourceType>
<description>aasdadsf</description>
</clientSource>
<clientSource>
<amount>25000.0</amount>
<clientSourceType>6</clientSourceType>
<description>wrewertwerewrt</description>
</clientSource>
<clientSource>
<amount>50000.0</amount>
<clientSourceType>10</clientSourceType>
<description>second and thirs</description>
</clientSource>
</client>
</application>')
FROM dual
)
SELECT "clientSourceType", "clientSourceAmount"
FROM t,
XMLTABLE( XMLNAMESPACES( DEFAULT 'http://www.abcxyz.com/Schema/FCX/1' ),
'/application/client/clientSource'
PASSING xml
COLUMNS
"clientSourceType" INT PATH 'clientSourceType',
"clientSourceAmount" INT PATH 'amount'
)
clientSourceType | clientSourceAmount |
---|---|
1 | 25000 |
6 | 25000 |
10 | 50000 |