Skip to content

Instantly share code, notes, and snippets.

@wmanth
Last active April 20, 2017 03:44
Show Gist options
  • Save wmanth/5413400 to your computer and use it in GitHub Desktop.
Save wmanth/5413400 to your computer and use it in GitHub Desktop.
XSLT to transform geo data from GPX to KML.
<?xml version="1.0"?>
<!--
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gpx="http://www.topografix.com/GPX/1/0">
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gpx="http://www.topografix.com/GPX/1/1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<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">
<xsl:apply-templates select="gpx:gpx"/>
</kml>
</xsl:template>
<xsl:template match="gpx:gpx">
<Document>
<Style id="route">
<LineStyle>
<color>a02020ff</color>
<width>4</width>
</LineStyle>
</Style>
<xsl:apply-templates select="gpx:trk"/>
</Document>
</xsl:template>
<xsl:template match="gpx:trk">
<Placemark>
<name><xsl:value-of select="gpx:name"/></name>
<styleUrl>#route</styleUrl>
<xsl:apply-templates select="gpx:trkseg"/>
</Placemark>
</xsl:template>
<xsl:template match="gpx:trkseg">
<LineString>
<tessellate>1</tessellate>
<coordinates>
<xsl:for-each select="gpx:trkpt">
<xsl:value-of select="@lon"/>,<xsl:value-of select="@lat"/>,<xsl:value-of select="gpx:ele"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</coordinates>
</LineString>
</xsl:template>
</xsl:stylesheet>
@fb71
Copy link

fb71 commented Aug 26, 2014

Thanks! Helps me a lot.
Namespace is missing for "name" in line 33.

@wmanth
Copy link
Author

wmanth commented Jun 1, 2015

Thanks! Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment