SQL 查询 XML KML Google 地球

SQL query to XML for KML Google Earth

我正在尝试使用 SQL 为 XML 制作一个 KML 文件到路径。但我需要另一个 "root" 部分,名为 "Folder",位于 KML 下方。我尝试的一切都会成为另一个 每个小节的节命名文件夹。

with xmlnamespaces ( 
'http://www.w3.org/2005/Atom' as Atom,
'http://www.opengis.net/kml/2.2' as kml,
'http://www.google.com/kml/ext/2.2' as gx,
default 'http://www.opengis.net/kml/2.2'
) 

select --'kml' as kml,
    --'NewSpots 2013' as 'Folder/Name',
    --Waypoint as 'Folder/Placemark/name',
    --Description as 'Folder/Placemark/description',
    --ProperDecimalGPS as 'Folder/Placemark/Point/coordinates'
    Waypoint as 'Placemark/name',
    Description as 'Placemark/description',
    ProperDecimalGPS as 'Placemark/Point/coordinates'
    --WPGroup as 'Folder/Name'
from dbo.ttt
where PortArea = 'NewSpots 2013'

for xml path(''), root('kml')

这是输出。我需要下面结果中显示的部分,但这些项目已被注释掉以轻松查看我的身份 missing/needing.

<kml xmlns="http://www.opengis.net/kml/2.2" 

xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:Atom="http://www.w3.org/2005/Atom">
 <!--<Folder><Name>NewSpots 2013</Name>-->
  <Placemark>
    <name>106 Rok-1</name>
    <Point>
      <coordinates>-94.271833,28.633483,0</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>106 Rok-2</name>
    <Point>
      <coordinates>-94.269783,28.649767,0</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>106 Rok-3</name>
    <Point>
      <coordinates>-94.269033,28.6336,0</coordinates>
    </Point>
  </Placemark>
 <!--</Folder>-->
</kml>

如果需要ttt的设计table...

USE [Fishing]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[ttt](
    [PortArea] [varchar](50) NULL,
    [WPGroup] [varchar](150) NULL,
    [Waypoint] [varchar](150) NULL,
    [ProperDecimalGPS] [varchar](50) NULL,
    [Description] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

一些示例数据:

像这样尝试

简短说明:打包 一体化 是可能的,但在这种情况下,您会重复看到命名空间声明(每个 Placemark将是 *sub-select)。这并没有错,但很烦人(参见

我的解决方案:分两步创建 XML,只有最后一步带有命名空间

CREATE TABLE [dbo].[ttt](
    [PortArea] [varchar](50) NULL,
    [WPGroup] [varchar](150) NULL,
    [Waypoint] [varchar](150) NULL,
    [ProperDecimalGPS] [varchar](50) NULL,
    [Description] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
GO
INSERT INTO dbo.ttt VALUES
 ('NewSpots 2013','106 Rok','106 Rok-1','-94.271833,28.633483,0',NULL)
,('NewSpots 2013','106 Rok','106 Rok-2','-94.269783,28.649767,0',NULL)
,('NewSpots 2013','106 Rok','106 Rok-3','-94.269033,28.6336,0',NULL);
GO

DECLARE @FolderName VARCHAR(MAX)='NewSpots 2013';

DECLARE @Placemarks XML;

SELECT @Placemarks=
(
    select Waypoint as 'name',
           Description as 'description',
           ProperDecimalGPS as 'Point/coordinates'
    from dbo.ttt
    where PortArea = @FolderName
    for xml path('Placemark'),TYPE
);

with xmlnamespaces ( 
'http://www.w3.org/2005/Atom' as Atom,
'http://www.opengis.net/kml/2.2' as kml,
'http://www.google.com/kml/ext/2.2' as gx,
default 'http://www.opengis.net/kml/2.2'
)
SELECT @FolderName AS 'Name'
      ,@Placemarks
FOR XML PATH('Folder'),ROOT('kml') 

GO
--Clean-up
--DROP TABLE dbo.ttt;

结果

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:Atom="http://www.w3.org/2005/Atom">
  <Folder>
    <Name>NewSpots 2013</Name>
    <Placemark xmlns="">
      <name>106 Rok-1</name>
      <Point>
        <coordinates>-94.271833,28.633483,0</coordinates>
      </Point>
    </Placemark>
    <Placemark xmlns="">
      <name>106 Rok-2</name>
      <Point>
        <coordinates>-94.269783,28.649767,0</coordinates>
      </Point>
    </Placemark>
    <Placemark xmlns="">
      <name>106 Rok-3</name>
      <Point>
        <coordinates>-94.269033,28.6336,0</coordinates>
      </Point>
    </Placemark>
  </Folder>
</kml>

更新:这种方法不正确!

由于 xmlns="" 在嵌套元素中,这些元素 不在同一个默认命名空间中!

唯一的解决方案似乎是转换为 nvarchar(max),然后转换为 REPLACE() 以摆脱这些 xmlns="" 声明,然后重新转换为 XML .希望以后的版本能解决这个问题!