简体   繁体   English

如何在SQL Server中使用FOR XML抑制空名称空间

[英]How do I suppress empty namespaces with FOR XML in Sql Server

We are encountering a strange problem with SQL Server 2005/2008 using the FOR XML with fragments of xml and namespaces. 使用FOR XML以及xml和名称空间片段的SQL Server 2005/2008,我们遇到一个奇怪的问题。 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. [Customer]字段是一个xml片段。 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. 如果您注意到标记的子项中有xmlns =“” 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. 它删除了xmlns =“”,但我们需要将其保留在文件中。 Any ideas? 有任何想法吗?

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 "". 在表中,您有没有命名空间的元素,因此,当您将它们添加到具有默认命名空间xmlns =“ http://tempuri.org/newincomingxml.xsd”的Consumers元素下时 ,表中的元素必须覆盖默认命名空间回到 ””。

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. 没有xmlns =“”表示ConsumerType / FirstName / LastName元素位于名称空间“ http://tempuri.org/newincomingxml.xsd ”中,这是错误的。

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. 您可能希望将ConsumerType / FirstName / LastName元素移动到“ http://tempuri.org/newincomingxml.xsd ”命名空间中,以匹配父Consumer元素的命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM