SQL Server 2008: How to remove namespace from the elements, but let it display on root

偶尔善良 提交于 2019-12-07 15:57:31

问题


I am using SQL Server2008 FOR XML clause to generate XML file using bcp.

When I use the below query, the result is fine:

WITH XMLNAMESPACES (        
   'http://base.google.com/ns/1.0' AS g,
   DEFAULT 'http://www.w3.org/2005/Atom'
    )
select ID, Name AS "g:Name" from PaymentMethods For XML PATH ('entry'), Root('feed') 

Result:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
  <entry>
    <ID>1</ID>
    <g:Name>BPay</g:Name>
  </entry>
  <entry>
    <ID>2</ID>
    <g:Name>Cash</g:Name>
  </entry>
</feed>

But I also want to add some static elements, say, Title and date after the Root. I am able to do that, but then, the namespace tags appear in the element as well which I don't want. Please note, I want some elements with the namespace prefix eg,

Query I am using for this is:

WITH XMLNAMESPACES (
    'http://base.google.com/ns/1.0' AS g,
    DEFAULT 'http://www.w3.org/2005/Atom'
    )
SELECT 
'Google Feed' As Title,
CONVERT(Date, getdate()) As Updated,
(   
select ID, Name AS "g:Name" from PaymentMethods For XML PATH ('entry'), TYPE
)

FOR XML PATH(''),   Root('feed') 

And the result I get is:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
  <Title>Google Feed</Title>
  <Updated>2012-06-27</Updated>
  <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
     <ID>1</ID>
    <g:Name>BPay</g:Name>
  </entry>
 <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
    <ID>2</ID>
    <g:Name>Cash</g:Name>
  </entry>
</feed>

Whereas i want a result like:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
  <Title>Google Feed</Title>
  <Updated>2012-06-27</Updated>
  <entry>
    <ID>1</ID>
    <g:Name>BPay</g:Name>
  </entry>
  <entry>
    <ID>2</ID>
    <g:Name>Cash</g:Name>
  </entry>
</feed>

Please help...

Thanks,

PS

来源:https://stackoverflow.com/questions/11221375/sql-server-2008-how-to-remove-namespace-from-the-elements-but-let-it-display-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!