【发布时间】:2018-01-29 15:38:06
【问题描述】:
我正在尝试使用 FOR XML PATH 在 SQL Server 2012 中创建 xml 输出。
可以做些什么来获得所需的输出?
我想输出为:
<types>
<type>
<type>FirstType</type>
<attribute>FirstAttribute</attribute>
</type>
</types>
<types>
<type>
<type>SecondType</type>
<attribute>SecondAttribute</attribute>
</type>
</types>
<types>
<type>
<type>ThirdType</type>
<attribute>ThirdAttribute</attribute>
</type>
</types>
我的代码:
DECLARE @table TABLE (
type VARCHAR(50)
, attribute VARCHAR(50)
)
SELECT T1.type
, T1.attribute
FROM @table AS T1
FOR XML path('type'), root('types')
给我错误的输出:
<types>
<type>
<type>FirstType</type>
<attribute>FirstAttribute</attribute>
</type>
<type>
<type>SecondType</type>
<attribute>SecondAttribute</attribute>
</type>
<type>
<type>ThirdType</type>
<attribute>ThirdAttribute</attribute>
</type>
</types>
【问题讨论】:
-
在一个有效的 xml 文档中不能有多个根。如果您多次声明
<types>,那么它不是您的根。
标签: sql-server xml tsql xpath for-xml-path