使用带有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 =“”但我们需要将其保留在文件中。有什么想法吗?
答案 0 :(得分:3)
结果是正确的。在表中,您有没有命名空间的元素,因此当您使用默认命名空间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元素的命名空间。