Alternating colors in GPX-Tracks

2019-08-08 15:04发布

问题:

This Question refers to:How to summarize, group 4000+ GPX Trackpoints as Tracks named for by each days date?

Garmin GPS-Tracks provide the option to define a different color for each track.

As a perfection to the result of the referring question, I wonder how to apply alternating colors for each GPS-track. For instance the 1st. track (all trackpoints for that day) are colored 'Magenta' the 2nd. day is colored 'DarkGray' and the 3rd. day again 'Magenta'.

Using the node (date) to alternate between odd or even days would often produce the same color for 2 days. For example: March 31 to April 1st. would receive the same colors.

Colors apply to the node: <gpxx:DisplayColor>

How can this be achieved using the code provided in the answer of the referring question?

<trk>
<name>ACTIVE LOG</name>
<extensions>
  <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
    <gpxx:DisplayColor>Magenta</gpxx:DisplayColor>
  </gpxx:TrackExtension>
</extensions>
<trkseg>

回答1:

You can divide the position() by 2 and check to see if there is a remainder.

<xsl:stylesheet version="1.0" 
    xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:gpx="http://www.topografix.com/GPX/1/1" 
    xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="gpx">
    <xsl:output indent="yes" encoding="utf-8"/>
    <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>
                    <extensions>
                        <gpxx:TrackExtension>
                            <xsl:choose>
                                <xsl:when test="position() mod 2 = 1">
                                    <gpxx:DisplayColor>Magenta</gpxx:DisplayColor>                                    
                                </xsl:when>
                                <xsl:otherwise>
                                    <gpxx:DisplayColor>DarkGray</gpxx:DisplayColor>
                                </xsl:otherwise>
                            </xsl:choose>
                        </gpxx:TrackExtension>
                    </extensions>
                    <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>