简体   繁体   English

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

[英]SQL Server FOR XML PATH namespace declaration on root element

I'm using a FOR XML PATH query to generate a sitemap from our database and I need to declare a namespace that has a different name than the default: 我正在使用FOR XML PATH查询从我们的数据库生成站点地图,我需要声明一个名称空间,该名称空间的名称与默认名称不同:

(see xsi:schemaLocation below) (请参见下面的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">

How can this be done using : 如何使用以下方法完成此操作:

;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"

Thank you! 谢谢!

I've found the solution here: TSQL for xml add schema attribute to root node 我在这里找到了解决方案: xml的TSQL将架构属性添加到根节点

Essentially, I went with this: 本质上,我这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM