Question

I have a .gpx file which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
   <gpx version="1.1">
       <metadata>
          <author>
             <name>asd</name>
             <email id="asd" domain="gmail.com"/>
          </author>
          <link href="http://www.endomondo.com">
             <text>Endomondo</text>
          </link>
          <time>2014-03-25T15:58:33Z</time>
       </metadata>
       <trk>
          <src>http://www.endomondo.com/</src>
          <link href="http://www.endomondo.com/workouts/246394762/4704124">
              <text>endomondo</text>
          </link>
          <type>MOUNTAIN_BIKING</type>
          <trkseg>
              <trkpt lat="12.123456" lon="12.123456">
                  <time>2013-09-16T12:01:23Z</time>
              </trkpt>
              <trkpt lat="12.123456" lon="12.123456">
                  <ele>627.5</ele>
                  <time>2013-09-16T12:01:28Z</time>
              </trkpt>
              ...additional <trkpt> nodes
          </trk>

I would like to extract the longitude and latitude, time and elevation values from each trkpt node and output them in a CSV file. I've heard of XSLT transformations but I don't know how to write them. Another way would be to use a xml/gpx parser.

Is there a quicker way I could retrieve these values?

The output CSV should be in this form:

latitude \t longitude \t elevation(if present) \t time
Was it helpful?

Solution

easy using xslt-1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:text>longitude,latitude,time,elevation&#xD;&#xA;</xsl:text>
        <xsl:for-each select="gpx/trk/trkseg/trkpt">
            <xsl:value-of select="concat(@lon, ',', @lat, ',', time, ',', ele, '&#xD;&#xA;')"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

it outputs

longitude,latitude,time,elevation
12.123456,12.123456,2013-09-16T12:01:23Z,
12.123456,12.123456,2013-09-16T12:01:28Z,627.5

OTHER TIPS

Slightly modified version works fine with 'msxml.exe' processing Garmin GPX-Files.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gpx="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<xsl:output method="text" indent="yes" encoding="utf-8"/>
<xsl:template match="/">
    <xsl:text>latitude,longitude,elevation,time,&#xD;&#xA;</xsl:text>
    <xsl:for-each select="/gpx:gpx/gpx:trk/gpx:trkseg/gpx:trkpt">
        <xsl:value-of select="concat(@lat, ',', @lon, ',', gpx:ele, ',', gpx:time, '&#xD;&#xA;')"/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top