[OSM-newbies] XSLT question

Renaud MICHEL r.h.michel+osm at gmail.com
Wed Sep 16 07:04:42 BST 2009


Le mercredi 16 septembre 2009 à 05:30, Andrew Errington a écrit :
> On Tue, September 15, 2009 16:00, Andrew Errington wrote:
> > On Tue, September 15, 2009 14:55, Renaud MICHEL wrote:
> >> You can make it a lot simpler with an xpath like I gave before:
> >>
> >> <xsl:template match="node[tag[@k='amenity' and @v=$amenity]]">
> 
> I'm afraid I couldn't get this to work.  xsltproc complained.  :(
> 
> I don't have the error message, but I could run it again and tell you if
> you're interested.

Well, I can't help you if I don't know what was the problem.

> Unfortunately my XSLT-fu is very weak.  I haven't grasped how to make a
> result set of lats and a result set of lons.  If I could do that then I
> could sum() them and count() them and calculate the average.

If you want to do a sum you must have a node-set, which you can't have with 
key(), so you will need to reference the elements directly (which is a lot 
slower than key() ), like this (the way being the current node)

<xsl:variable name="nodes" select="../node[@id=current()/nd/@ref]"/>
<xsl:value-of select="sum($nodes/@lat) div count($nodes)"/>
<xsl:value-of select="sum($nodes/@lon) div count($nodes)"/>

The trick here is that the construct @id=current()/nd/@ref which compare one 
value (@id) with a node-set (current()/nd/@ref) is true if any of the 
selected @ref is equal to @id.

(cf http://www.w3.org/TR/xpath#booleans)

-- 
Renaud Michel




More information about the newbies mailing list