You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除SQL Server生成KML中Placemark元素的XML命名空间

解决SQL Server生成KML时Placemark重复添加命名空间的问题

问题说明

在SQL Server中生成KML文件时,使用xmlnamespaces设置默认命名空间后,子查询生成的<Placemark>节点会自动带上重复的命名空间声明,需要移除这些多余的声明。

原查询代码

declare @data table(name nvarchar(100), descr nvarchar(100),loc geography)
insert into @data(name,descr,loc)
select 'New York City','USA',geography::Point(40,40,4326)
union all
select 'Boston','USA',geography::Point(50,50,4326)

;with xmlnamespaces(default 'http://www.opengis.net/kml/2.2')
select CONCAT('kml_',format(getdate(),'yyyy_MM_dd')) [name] 
    ,'icon-1644-000000-labelson' [Style/@id]
    ,'f000000' [Style/color]
    ,1 [Style/scale]
    ,'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png' [Style/Icon/href]
    ,(select d.name [name]
    ,d.descr  [description]
    ,'#icon-1644-4E342E-labelson' [styleUrl]
    ,concat(d.loc.Long,',',d.loc.Lat,',0')[Point/coordinates]
    from @data d
    for xml path('Placemark'), type)
for xml path('Document'), root('kml')

(注:原查询缺失for xml相关语句,已补充完整以保证可执行)

当前输出

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>kml_2024_01_21</name>
    <Style id="icon-1644-000000-labelson">
      <color>f000000</color>
      <scale>1</scale>
      <Icon>
        <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
      </Icon>
    </Style>
    <Placemark xmlns="http://www.opengis.net/kml/2.2">
      <name>New York City</name>
      <description>USA</description>
      <styleUrl>#icon-1644-4E342E-labelson</styleUrl>
      <Point>
        <coordinates>40,40,0</coordinates>
      </Point>
    </Placemark>
    <Placemark xmlns="http://www.opengis.net/kml/2.2">
      <name>Boston</name>
      <description>USA</description>
      <styleUrl>#icon-1644-4E342E-labelson</styleUrl>
      <Point>
        <coordinates>50,50,0</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

期望输出

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>kml_2024_01_21</name>
    <Style id="icon-1644-000000-labelson">
      <color>f000000</color>
      <scale>1</scale>
      <Icon>
        <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
      </Icon>
    </Style>
    <Placemark>
      <name>New York City</name>
      <description>USA</description>
      <styleUrl>#icon-1644-4E342E-labelson</styleUrl>
      <Point>
        <coordinates>40,40,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>Boston</name>
      <description>USA</description>
      <styleUrl>#icon-1644-4E342E-labelson</styleUrl>
      <Point>
        <coordinates>50,50,0</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

解决方案

问题根源是SQL Server的XML子查询会自动继承外部查询的命名空间设置,导致<Placemark>节点重复添加命名空间声明。只需在子查询内部单独设置空的默认命名空间,即可覆盖外部的命名空间配置。

修改后的完整查询代码:

declare @data table(name nvarchar(100), descr nvarchar(100),loc geography)
insert into @data(name,descr,loc)
select 'New York City','USA',geography::Point(40,40,4326)
union all
select 'Boston','USA',geography::Point(50,50,4326)

;with xmlnamespaces(default 'http://www.opengis.net/kml/2.2')
select CONCAT('kml_',format(getdate(),'yyyy_MM_dd')) [name] 
    ,'icon-1644-000000-labelson' [Style/@id]
    ,'f000000' [Style/color]
    ,1 [Style/scale]
    ,'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png' [Style/Icon/href]
    ,(
        -- 子查询内设置空默认命名空间,避免继承外部命名空间
        ;with xmlnamespaces(default '')
        select d.name [name]
            ,d.descr  [description]
            ,'#icon-1644-4E342E-labelson' [styleUrl]
            ,concat(d.loc.Long,',',d.loc.Lat,',0')[Point/coordinates]
        from @data d
        for xml path('Placemark'), type
    )
for xml path('Document'), root('kml')

执行上述代码后,<Placemark>节点将不再携带重复的命名空间声明,符合期望输出。

内容的提问来源于stack exchange,提问作者igmox igmox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 22:42:07