2014-01-08 76 views
0

我使用FOR XML路徑查詢生成從我們的數據庫一個網站地圖,我需要聲明的是具有不同的名稱比默認的命名空間:SQL Server作爲根元素的XML命名空間PATH聲明

(看到XSI:下面的schemaLocation)

<urlset 
     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
      http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> 

如何才能做到這一點使用:

;with xmlnamespaces(default 'http://www.sitemaps.org/schemas/sitemap/0.9', 
       'http://www.w3.org/2001/XMLSchema-instance' as xsi, 
       'http://www.sitemaps.org/schemas/sitemap/0.9 
     http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd' --as "xsi:schemaLocation" 

謝謝!

回答

0

我發現這裏的解決方案:TSQL for xml add schema attribute to root node

從本質上講,我去了這一點:

DECLARE @siteMapXml XML 

SELECT @siteMapXml = (
SELECT 
    s.Dns + '/SomeSection' loc, 
    Convert(char(10), GetDate() - 7, 126) as lastmod, 
    'weekly' as changefreq, 
    0.9 as priority 
FROM 
    ThisTable s 
WHERE 
    s.Id= 1234 
FOR XML PATH ('url'), ROOT ('urlset')) 

set @siteMapXml.modify('insert (attribute xsi:schemaLocation {"http://www.mydomain.com/xmlns/bla/blabla/myschema.xsd"}) into (/urlset)[1]') 

select @siteMapXml