User:Jeroen Muris/osm2gpx.xsl

From OpenStreetMap Wiki
Jump to navigation Jump to search

To quickly get nodes from OSM in my GPS unit, I created an XSL stylesheet to transform *.osm XML to *.gpx XML. It's very basic, and a 'work in progress', but here it is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="key">rcn_ref</xsl:param>

<xsl:template match="osm">
<xsl:text disable-output-escaping="yes"><![CDATA[<?xml version="1.0" encoding="UTF-16"?>
<gpx creator="osm2gpx.xsl" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
]]></xsl:text>
<xsl:apply-templates select="node"/>
<xsl:text disable-output-escaping="yes">
<![CDATA[</gpx>]]>
</xsl:text>
</xsl:template>

<xsl:template match="node">
<xsl:for-each select="tag">

<xsl:if test='@k=$key'>
  &lt;wpt lat=&quot;<xsl:value-of select='../@lat'/>&quot; lon=&quot;<xsl:value-of select='../@lon'/>&quot;&gt;
    &lt;time&gt;<xsl:value-of select='../@timestamp'/>&lt;/time&gt;
    &lt;name&gt;<xsl:value-of select='@v'/>&lt;/name&gt;
    &lt;desc&gt;<xsl:value-of select='@k'/>=<xsl:value-of select='@v'/>&lt;/desc&gt;
  &lt;/wpt>
</xsl:if>

</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

This XSL stylesheet - saved as a file - should be usable with any XSL processor, e.g. Saxon, xsltproc or (Windows) MSXSL.exe. See software documentation on how to apply the stylesheet.