postgresql query_to_xml删除标签'row'

时间:2018-03-12 12:09:57

标签: xml postgresql

我是postgreql的新手,并且正在使用xml 从我使用以下查询的文档开始:

select xmlelement(name user,query_to_xml('select cu_fname,cu_sname,cu_id from p_customers',true,true,''))

结果是

<user>
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <cu_fname>Test</cu_fname>
  <cu_sname>user</cu_sname>
  <cu_id>66976662</cu_id>
</row>

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <cu_fname>Test</cu_fname>
  <cu_sname>User</cu_sname>
  <cu_id>60134543</cu_id>
</row>

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <cu_fname>Test</cu_fname>
  <cu_sname>User</cu_sname>
  <cu_id>14483654</cu_id>
</row>

</user>

但我的期望是:

<user >
  <cu_fname>Test</cu_fname>
  <cu_sname>user</cu_sname>
  <cu_id>66976662</cu_id>
</user>

<user>
  <cu_fname>Test</cu_fname>
  <cu_sname>User</cu_sname>
  <cu_id>60134543</cu_id>
</user>

<user >
  <cu_fname>Test</cu_fname>
  <cu_sname>User</cu_sname>
  <cu_id>14483654</cu_id>
</user>
你能救我吗?

提前感谢!!!

2 个答案:

答案 0 :(得分:2)

我想你想要:

select xmlelement(name user,xmlforest(cu_fname,cu_sname,cu_id)) 
from p_customers;

?..

答案 1 :(得分:2)

您可以先获取用户元素,然后将它们聚合为:

select XMLAGG(XMLELEMENT(name user, xmlforest(cu_fname,cu_sname,cu_id))) from p_customers;