如何总结,将4000+ GPX Trackpoints分组为每天命名的曲目?

时间:2014-08-26 16:14:25

标签: xml xslt gps gpx

下面的XSLT仅从Garmin GPX文件中提取所有跟踪点(trkpt)(省略任何Waypoint),并将结果作为一(1)个轨道导出到准备用于MapSource或Basecamp的新GPX文件中。 目前,该轨道的名称是第一个轨道点的日期。

如何按每个日期命名一条曲目(YYYY-MM-DD),每条曲目包含所有相应的曲目点?

(我认为必须考虑到trkpt / time的日期变化,但我在这里丢失了。)

GPX源文件可以是Garmin创建的任何更大的GPX文件(不了解其他人)。 我的源文件跨越3个月的旅行,每4分钟记录一次跟踪点记录(在75天内共计7633 trkpt)。 当然,该设备在睡觉时被关闭。 下面请参阅名为的文件的XSLT代码:' extract-TRKPTs.xsl'

<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" />
<!-- command: msxsl.exe 20101008.gpx extract-TRKPTs.xsl > 20101008-trkpts.gpx -->
<!-- extracts all trackpoints only from a given Garmin GPX file -->
<!-- output is piped into a new .gpx file -->
<xsl:template match="/">&#60;<xsl:text>?xml version="1.0" encoding="utf-8" standalone="no" ?&#62;</xsl:text>
&#60;<xsl:text>gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="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:text>&#62;
<gpx>
<trk>&#60;trk&#62;
&#60;name&#62;Trackpoints: <xsl:value-of select="substring(/gpx:gpx/gpx:trk/gpx:trkseg/gpx:trkpt/gpx:time,1,10)"/>&#60;/name&#62;
<trkseg>&#60;trkseg&#62;
    <xsl:for-each select="/gpx:gpx/gpx:trk/gpx:trkseg/gpx:trkpt">
    <xsl:value-of select="gpx:trkpt"/><trkpt>
        &#60;<xsl:text>trkpt lat="</xsl:text><xsl:value-of select="@lat"/><xsl:text>" lon="</xsl:text><xsl:value-of select="@lon"/><xsl:text>"</xsl:text>&#62;
        &#60;ele&#62;<xsl:value-of select="gpx:ele"/>&#60;/ele&#62;
        &#60;time&#62;<xsl:value-of select="gpx:time"/>&#60;/time&#62;
        &#60;/trkpt&#62;</trkpt>
    </xsl:for-each>
</trkseg>&#60;/trkseg&#62;
</trk>&#60;/trk&#62;
</gpx>&#60;/gpx&#62;
</xsl:template>
</xsl:stylesheet>

代码看起来很难看,因为它设置为输出&#39; text&#39;因此我需要使用特殊字符实体。但是,嘿,到目前为止它的确有效。对不起,我是XSLT新手。 感谢您的反馈。

当前输出如下(单个轨道中的所有轨迹点):

<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="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">
<trk>
<name>Trackpoints: 2010-09-21</name>
<trkseg>
    <trkpt lat="34.004124635830522" lon="-118.47464606165886">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:12Z</time>
    </trkpt>
    <trkpt lat="34.004120444878936" lon="-118.47464254125953">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:26Z</time>
    </trkpt>
    <trkpt lat="34.004129162058234" lon="-118.47465343773365">
    <ele>56.8551025390625</ele>
    <time>2010-09-22T21:02:55Z</time>
    </trkpt>
...
...
    <trkpt lat="37.042149296030402" lon="-112.52694656141102">
    <ele>1506.523193359375</ele>
    <time>2010-12-01T07:01:06Z</time>
    </trkpt>
    <trkpt lat="37.042256919667125" lon="-112.52645093947649">
    <ele>1504.1201171875</ele>
    <time>2010-12-01T07:01:23Z</time>
    </trkpt>
</trkseg>
</trk>
</gpx>

预期输出如下(请注意:每天一首曲目)。

<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="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">
<trk>
<name>Trackpoints: 2010-09-21</name>
<trkseg>
    <trkpt lat="34.004124635830522" lon="-118.47464606165886">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:12Z</time>
    </trkpt>
    <trkpt lat="34.004120444878936" lon="-118.47464254125953">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:26Z</time>
    </trkpt>
    <trkpt lat="34.004129162058234" lon="-118.47465343773365">
    <ele>56.8551025390625</ele>
    <time>2010-09-22T21:02:55Z</time>
    </trkpt>
</trkseg>
</trk>
...
...
<trk>
<name>Trackpoints: 2010-12-01</name>
<trkseg>

    <trkpt lat="37.042149296030402" lon="-112.52694656141102">
    <ele>1506.523193359375</ele>
    <time>2010-12-01T07:01:06Z</time>
    </trkpt>
    <trkpt lat="37.042256919667125" lon="-112.52645093947649">
    <ele>1504.1201171875</ele>
    <time>2010-12-01T07:01:23Z</time>
    </trkpt>
</trkseg>
</trk>
</gpx>

1 个答案:

答案 0 :(得分:2)

您应该可以使用Muenchian Grouping执行此操作。

由于您没有提供示例输入,我自己创建了示例here的修改版本。)

XML输入

<gpx xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
    xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" 
    creator="Oregon 400t" 
    version="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 http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
    <metadata>
        <link href="http://www.garmin.com">
            <text>Garmin International</text>
        </link>
        <time>2009-10-17T22:58:43Z</time>
    </metadata>
    <trk>
        <name>Example 1</name>
        <trkseg>
            <trkpt lat="47.644548" lon="-122.326897">
                <ele>4.46</ele>
                <time>2009-10-17T18:37:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326898">
                <ele>4.47</ele>
                <time>2009-10-17T18:38:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326898">
                <ele>4.48</ele>
                <time>2009-10-18T18:39:26Z</time>
            </trkpt>
        </trkseg>
    </trk>
    <trk>
        <name>Example 2</name>
        <trkseg>
            <trkpt lat="47.644548" lon="-122.326899">
                <ele>4.49</ele>
                <time>2009-10-18T18:37:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326900">
                <ele>4.50</ele>
                <time>2009-10-18T18:38:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326901">
                <ele>4.51</ele>
                <time>2009-10-19T18:39:26Z</time>
            </trkpt>
        </trkseg>    
    </trk>
</gpx>

XSLT 1.0

<xsl:stylesheet version="1.0" 
    xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:gpx="http://www.topografix.com/GPX/1/1" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="gpx">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="date" match="gpx:trkpt" use="substring(gpx:time,1,10)"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="//gpx:trkpt[generate-id(key('date',substring(gpx:time,1,10))[1])=generate-id()]">
                <trk>
                    <name>Trackpoints: <xsl:value-of select="substring(gpx:time,1,10)"/></name>
                    <trkseg>
                        <xsl:for-each select="key('date',substring(gpx:time,1,10))">
                            <xsl:copy-of select="."/>
                        </xsl:for-each>
                    </trkseg>
                </trk>
            </xsl:for-each>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

XML输出

<gpx xmlns="http://www.topografix.com/GPX/1/1"
     xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
     xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     creator="Oregon 400t"
     version="1.1"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
   <trk>
      <name>Trackpoints: 2009-10-17</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326897">
            <ele>4.46</ele>
            <time>2009-10-17T18:37:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326898">
            <ele>4.47</ele>
            <time>2009-10-17T18:38:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
   <trk>
      <name>Trackpoints: 2009-10-18</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326898">
            <ele>4.48</ele>
            <time>2009-10-18T18:39:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326899">
            <ele>4.49</ele>
            <time>2009-10-18T18:37:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326900">
            <ele>4.50</ele>
            <time>2009-10-18T18:38:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
   <trk>
      <name>Trackpoints: 2009-10-19</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326901">
            <ele>4.51</ele>
            <time>2009-10-19T18:39:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
</gpx>