使用 FOR XML 从 SQL Server 2005 数据创建 XML
我正在尝试创建一个 Excel XML,我想将其存储在 SQL Server 2005 中的 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 '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>
我想要的是消除 节点。我想摆脱这个: xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s=" urn:schemas-microsoft-com:office:spreadsheet"
来自
有人知道如何执行此操作吗?
I'm trying to create an Excel XML that I want to store in an XML Field in SQL Server 2005. I have gotten this far:
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')
Here's my output:
<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>
What I want is to eliminate the namespace in the <s:Row>
node. I want to get rid of this: 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 <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">
Anybody have an idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 FOR XML 子句都会添加一个命名空间声明。我知道摆脱它们的唯一方法是在一个“for xml”查询中构建整个文档,但这是不可行的。
Each FOR XML clause will add a namespace declaration. The only way I know to get rid of them is to build the entire document in one single 'for xml' query, and that is not feasible.