根元素上的SQL Server FOR XML PATH名称空间声明

时间:2014-01-08 19:09:23

标签: sql xml namespaces sql-server-2012

我正在使用FOR 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"

谢谢!

1 个答案:

答案 0 :(得分: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
相关问题