【问题标题】:SQL Server FOR XML PATH namespace declaration on root element根元素上的 SQL Server FOR XML PATH 命名空间声明
【发布时间】:2014-01-27 01:44:02
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: sql xml namespaces sql-server-2012


    【解决方案1】:

    我在这里找到了解决方案: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
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2016-10-30
      • 2011-01-08
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多