具有FOR XML PATH的默认名称空间 [英] Default namespace with FOR XML PATH

查看:82
本文介绍了具有FOR XML PATH的默认名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下SQL:

;WITH XMLNAMESPACES(DEFAULT 'http://www.mynamespace.co.uk')
,CTE_DummyData AS (
    select id=1
)
select TOP 1
    [@ID]=1,
    (select top 1 [@ID] = 1 FROM CTE_DummyData FOR XML PATH ('Child'), TYPE)
from CTE_DummyData
FOR XML PATH ('Parent')

Thie返回xml:

Thie returns the xml:

<Parent xmlns="http://www.mynamespace.co.uk" ID="1">
  <Child xmlns="http://www.mynamespace.co.uk" ID="1" />
</Parent>

我需要的是仅在根元素上返回带有xmlns声明的xml.例如:

What I need is to return the xml with the xmlns declaration on only the root element. eg:

<Parent xmlns="http://www.mynamespace.co.uk" ID="1">
  <Child ID="1" />
</Parent>

有没有办法做到这一点?

Is there a way to do this?

注意: 上面的SQL极大地简化了实际代码,从而产生了一个复杂的文档,因此,在不给我几天额外工作的情况下,从FOR XML PATH进行更改实际上不是一个选择.要在整个文档的最上层清楚,必须具有NS,并且所有子代都应没有NS.

Note: The above SQL is an extreme simplification of the actual code, which produces a complex document, so changing from FOR XML PATH isn't really an option without giving me a couple of days of extra work to do. To be clear on the the top level of the entire document is required to have the NS, and all children should be without.

推荐答案

您可以使用

;with CTE_DummyData AS (
    select id=1
)
select 1 as tag,
       0 as parent,
       'http://www.mynamespace.co.uk' as [Parent!1!xmlns],
       id as [Parent!1!ID],
       null as [Child!2!ID]
from CTE_DummyData       
union all
select top 1
       2,
       1,
       null,
       null,
       id
from CTE_DummyData       
for xml explicit

这篇关于具有FOR XML PATH的默认名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆