【问题标题】:Create XML from SQL Server 2005 data using FOR XML使用 FOR XML 从 SQL Server 2005 数据创建 XML
【发布时间】:2009-10-12 23:23:49
【问题描述】:

我正在尝试创建要存储在 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>

我想要的是消除&lt;s:Row&gt; 节点中的命名空间。我想摆脱这个: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 &lt;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"&gt;

有人知道怎么做吗?

【问题讨论】:

  • 为什么要消除它们?它们是多余的,但却是正确的。
  • 我正在处理大量数据。虽然它现在可以正常工作,但如果我能修剪掉真正不必要的部分,那就太好了。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多