使用FOR XML从SQL Server 2005数据创建XML
我正在尝试创建一个Excel XML,并将其存储在SQLServer2005的XML字段中。我已经走了这么远:使用FOR XML从SQL Server 2005数据创建XML,sql,xml,sql-server-2005,for-xml,Sql,Xml,Sql Server 2005,For Xml,我正在尝试创建一个Excel XML,并将其存储在SQLServer2005的XML字段中。我已经走了这么远: WITH XMLNAMESPACES ( 'urn:schemas-microsoft-com:office:spreadsheet' as "s", 'urn:schemas-microsoft-com:office:office' as "o", 'urn:schemas-microsoft-com:office:excel' as "x" ) select 'Orde
WITH XMLNAMESPACES (
'urn:schemas-microsoft-com:office:spreadsheet' as "s",
'urn:schemas-microsoft-com:office:office' as "o",
'urn:schemas-microsoft-com:office:excel' as "x"
)
select 'Order' as "@s:Name",
(
select
'String' as 's:Cell/s:Data/@s:Type',
[Order] as 's:Cell/s:Data',
null as 'tmp',
'String' as 's:Cell/s:Data/@s:Type',
[Material] as 's:Cell/s:Data',
null as 'tmp',
'String' as 's:Cell/s:Data/@s:Type',
[Ship-To] as 's:Cell/s:Data'
from
(
select
'Order' as [Order],
'Material' as [Material],
'Ship-To' as [Ship-To]
union all
select
[Order],
[Material],
[Ship-To]
from Orders
WHERE [Material] IN(1234,5678))
) as Temp
FOR XML PATH('s:Row'), type
) AS 's:Table'
FOR XML PATH('s:Worksheet'), root('s:Workbook')
以下是我的输出:
<s:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
<s:Worksheet s:Name="Order">
<s:Table>
<s:Row xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
<s:Cell>
<s:Data s:Type="String">Order</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">Material</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">Ship-To</s:Data>
</s:Cell>
</s:Row>
<s:Row xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
<s:Cell>
<s:Data s:Type="String">200909</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">1234</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">US</s:Data>
</s:Cell>
</s:Row>
<s:Row xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
<s:Cell>
<s:Data s:Type="String">200909</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">5678</s:Data>
</s:Cell>
<s:Cell>
<s:Data s:Type="String">ASIA</s:Data>
</s:Cell>
</s:Row>
</s:Table>
</s:Worksheet>
</s:Workbook>
命令
材料
运送到
200909
1234
美国
200909
5678
亚洲
我想要的是消除
节点中的名称空间。我想摆脱这种情况:xmlns:x=“urn:schemas microsoft com:office:excel”xmlns:o=“urn:schemas microsoft com:office:office”xmlns:s=“urn:schemas microsoft com:office:spreadsheet”
from
有人知道怎么做吗?每个FOR XML子句都会添加一个名称空间声明。我所知道的摆脱它们的唯一方法是在一个“for xml”查询中构建整个文档,这是不可行的 你为什么要消灭他们?它们是超级棒,但正确。我正在处理大量数据。虽然它现在可以正常工作,但如果我能把真正不必要的东西修剪一下就好了。