使用FOR XML从SQL Server 2005数据创建XML

时间:2009-10-12 23:23:50

标签: sql xml sql-server-2005 for-xml

我正在尝试创建一个我想要存储在SQL Server 2005中的XML字段中的Excel 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>

我想要的是消除<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: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">

任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

每个FOR XML子句都将添加一个名称空间声明。我知道摆脱它们的唯一方法是在一个'for xml'查询中构建整个文档,这是不可行的。