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

发布于 2024-08-08 03:37:38 字数 3381 浏览 7 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-08-15 03:37:38

每个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文