[OSM-newbies] XSLT question

Renaud MICHEL r.h.michel+osm at gmail.com
Tue Sep 15 06:55:35 BST 2009


Le mardi 15 septembre 2009 à 06:40, Andrew Errington a écrit :
> Thanks for your help.  I have made this, which is part of the solution I
> want:
> 
> <xsl:template match="node">
>   <xsl:for-each select="tag">
>     <xsl:if test='@k="amenity" and @v=$amenity'>
>       <xsl:value-of select='../@id'/><br/>
>       <xsl:value-of select='../@lat'/><br/>
>       <xsl:value-of select='../@lon'/><br/>
>       <xsl:value-of select='../tag[@k="name"]/@v'/>
>       <br/>
>     </xsl:if>
>   </xsl:for-each>
> </xsl:template>

You can make it a lot simpler with an xpath like I gave before:

<xsl:template match="node[tag[@k='amenity' and @v=$amenity]]">     
  <xsl:value-of select='@id'/><br/>
  <xsl:value-of select='@lat'/><br/>
  <xsl:value-of select='@lon'/><br/>
  <xsl:value-of select='tag[@k="name"]/@v'/><br/>
</xsl:template>

> I am using xsltproc under Linux, and I can set the $amenity parameter on
> the command line.  This fragment will return all amenities which are
> nodes, but not areas.  I have some more work to do there.
> 
> For an area I will match "way" but then I have to get all nodes that make
> the way and calculate a simple centroid.

Using an xsl:key is the way to go here:

<xsl:key name="nodes" match="node" use="@id"/>
<xsl:template match="way[tag[@k='amenity' and @v=$amenity]]">
  <xsl:for-each match="nd">
    <xsl:variable name="mynode" select="key('nodes, at ref)""/>
    <!--do something here with $mynode-->
  </xsl:for-each>
</xsl:template>

cf http://www.w3.org/TR/xslt#key


-- 
Renaud Michel




More information about the newbies mailing list