如何在 Sql Server 中使用 FOR XML 抑制空命名空间

发布于 2024-08-22 17:40:40 字数 1808 浏览 5 评论 0原文

我们在使用带有 xml 和命名空间片段的 FOR XML 的 SQL Server 2005/2008 时遇到了一个奇怪的问题。这是有问题的查询。

WITH XMLNAMESPACES ( 
DEFAULT 'http://tempuri.org/newincomingxml.xsd',
'http://tempuri.org/newincomingxml.xsd' as [xsi],
'http://tempuri.org/newincomingxml.xsd' as [a]
) 
SELECT 
 [@a:Source], [AddressCount], [ConsumerCount], [EmailCount], [PermissionCount]
, (
  SELECT 
   [Consumer]
  FROM tbcExportBRC_Current xmlmaster
  FOR XML PATH(''), ROOT('Consumers'), TYPE
 )
FROM tbcExportBRCBatch_Current xmlroot
FOR XML PATH('Datafeed'), TYPE

[Customer] 字段是一个 xml 片段。当我运行这个时,我得到了。

<Datafeed xmlns:a="http://tempuri.org/newincomingxml.xsd" xmlns:xsi="http://tempuri.org/newincomingxml.xsd" xmlns="http://tempuri.org/newincomingxml.xsd" a:Source="DSD">
  <AddressCount>0</AddressCount>
  <ConsumerCount>0</ConsumerCount>
  <EmailCount>0</EmailCount>
  <PermissionCount>0</PermissionCount>
  <Consumers xmlns:a="http://tempuri.org/newincomingxml.xsd" xmlns:xsi="http://tempuri.org/newincomingxml.xsd" xmlns="http://tempuri.org/newincomingxml.xsd">
    <Consumer>
      <ConsumerType xmlns="">Individual</ConsumerType>
      <FirstName xmlns="">STEVE</FirstName>
      <LastName xmlns="">SMITH</LastName>
    </Consumer>
  </Consumers>
</Datafeed>

如果您注意到标签的子标签中有 xmlns="" 。如果我们直接查看表中的片段,它看起来像这样。

      <ConsumerType>Individual</ConsumerType>
      <FirstName>STEVE</FirstName>
      <LastName>SMITH</LastName>

我可以删除默认命名空间

DEFAULT 'http://tempuri.org/newincomingxml.xsd',

它会删除 xmlns="" 但我们需要将其保留在文件中。有什么想法吗?

We are encountering a strange problem with SQL Server 2005/2008 using the FOR XML with fragments of xml and namespaces. Here is the query in question.

WITH XMLNAMESPACES ( 
DEFAULT 'http://tempuri.org/newincomingxml.xsd',
'http://tempuri.org/newincomingxml.xsd' as [xsi],
'http://tempuri.org/newincomingxml.xsd' as [a]
) 
SELECT 
 [@a:Source], [AddressCount], [ConsumerCount], [EmailCount], [PermissionCount]
, (
  SELECT 
   [Consumer]
  FROM tbcExportBRC_Current xmlmaster
  FOR XML PATH(''), ROOT('Consumers'), TYPE
 )
FROM tbcExportBRCBatch_Current xmlroot
FOR XML PATH('Datafeed'), TYPE

The [Customer] field is an xml fragment. When I run this I get.

<Datafeed xmlns:a="http://tempuri.org/newincomingxml.xsd" xmlns:xsi="http://tempuri.org/newincomingxml.xsd" xmlns="http://tempuri.org/newincomingxml.xsd" a:Source="DSD">
  <AddressCount>0</AddressCount>
  <ConsumerCount>0</ConsumerCount>
  <EmailCount>0</EmailCount>
  <PermissionCount>0</PermissionCount>
  <Consumers xmlns:a="http://tempuri.org/newincomingxml.xsd" xmlns:xsi="http://tempuri.org/newincomingxml.xsd" xmlns="http://tempuri.org/newincomingxml.xsd">
    <Consumer>
      <ConsumerType xmlns="">Individual</ConsumerType>
      <FirstName xmlns="">STEVE</FirstName>
      <LastName xmlns="">SMITH</LastName>
    </Consumer>
  </Consumers>
</Datafeed>

If you notice the tag's children have xmlns="" in them. If we look at the fragment directly in the table it looks like this.

      <ConsumerType>Individual</ConsumerType>
      <FirstName>STEVE</FirstName>
      <LastName>SMITH</LastName>

I can remove the default namespace

DEFAULT 'http://tempuri.org/newincomingxml.xsd',

It removes the xmlns="" but we need to keep that in the file. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烟花肆意 2024-08-29 17:40:40

结果是正确的。在表中,您有没有命名空间的元素,因此当您将它们添加到默认命名空间 xmlns="http://tempuri.org/newincomingxml.xsd" 的 Consumers 元素下时,来自的元素该表必须将默认命名空间覆盖回“”。

这正是您应该看到的。没有 xmlns="" 意味着 ConsumerType/FirstName/LastName 元素位于命名空间“http:// tempuri.org/newincomingxml.xsd”,这是错误的。

您可能想要的是将 ConsumerType/FirstName/LastName 元素移动到“http://tempuri.org/ newincomingxml.xsd”命名空间,以匹配父 Consumer 元素的命名空间。

The result is the correct one. In the table you have elements with no namespace, so when you add them under the Consumers element with the default namespace of xmlns="http://tempuri.org/newincomingxml.xsd", the elements from the table must overwride the default namespace back to "".

That is exactly what you should see. Not having the xmlns="" would mean that the ConsumerType/FirstName/LastName elements are in the namespace "http://tempuri.org/newincomingxml.xsd", which is false.

What you probably whant is to probably move the ConsumerType/FirstName/LastName elements into the "http://tempuri.org/newincomingxml.xsd" namespace, to match the namespace of the parent Consumer element.

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