【问题标题】:Reading XML from Oracle DB as <column1>column2<column1> for each row从 Oracle DB 读取 XML 作为每行的 <column1>column2<column1>
【发布时间】:2014-07-11 17:08:34
【问题描述】:
表格信息
ID PARAM_NAME PARAM_VALUE
1 from blah@example.org
2 to pew@example.org
3 customer John Doe
4 order 100500
5 status yes
结果是简单的 xml 字符串
<?xml version="1.0" encoding="UTF-8"?>
<data>
<from>blah@example.org</from>
<to>pew@example.org</to>
<customer>John Doe</customer>
<order>100500</order>
<status>yes</status>
</data>
SELECT XMLELEMENT(m.param_name, m.param_value) AS data
FROM message m;
不起作用。
【问题讨论】:
标签:
sql
xml
oracle
oracle11g
【解决方案1】:
因为 XML 元素的名称是标识符,所以需要使用 evalname 函数:
with test_data as (
select 1 id, 'from' param_name, 'blah@example.org' param_value from dual union all
select 2 id, 'to' param_name, 'pew@example.org' param_value from dual union all
select 3 id, 'customer' param_name, 'John Doe' param_value from dual union all
select 4 id, 'order' param_name, '100500' param_value from dual union all
select 5 id, 'status' param_name, 'yes' param_value from dual
)
select
xmlelement("data",
xmlagg(
xmlelement( evalname(param_name), param_value)
)
)
from test_data
order by id