Created
March 3, 2012 15:09
-
-
Save StevenLooman/1966549 to your computer and use it in GitHub Desktop.
XSLT converting WFS output to GeoJSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:wfs="http://www.opengis.net/wfs" | |
xmlns:gml="http://www.opengis.net/gml" | |
xmlns:rws="http://mapserver.gis.umn.edu/mapserver"> | |
<xsl:output method="text" /> | |
<xsl:template match="/wfs:FeatureCollection"> | |
{ | |
"type": "FeatureCollection", | |
<xsl:apply-templates select="gml:boundedBy" />, | |
"features": [ | |
<xsl:for-each select="gml:featureMember"> | |
<xsl:if test="position() != 1"> | |
, | |
</xsl:if> | |
<xsl:apply-templates select="." /> | |
</xsl:for-each> | |
] | |
} | |
</xsl:template> | |
<xsl:template match="gml:boundedBy"> | |
"bbox": [ | |
<xsl:value-of select="substring-before(gml:Box/gml:coordinates, ' ')" />,<xsl:value-of select="substring-after(gml:Box/gml:coordinates, ' ')" /> | |
] | |
</xsl:template> | |
<xsl:template match="gml:featureMember/*"> | |
{ | |
"type": "Feature", | |
<xsl:apply-templates select="gml:boundedBy" />, | |
<xsl:apply-templates select="rws:GEOMETRY" />, | |
"properties": { | |
<xsl:for-each select="rws:NAAM|rws:SVK_BEGDTM"> | |
<xsl:if test="position() != 1"> | |
, | |
</xsl:if> | |
"<xsl:value-of select="substring-after(name(), ':')" />": "<xsl:value-of select="string(.)" />" | |
</xsl:for-each> | |
} | |
} | |
</xsl:template> | |
<xsl:template match="rws:GEOMETRY"> | |
<xsl:apply-templates/> | |
</xsl:template> | |
<xsl:template name="dump-coordinates"> | |
<xsl:param name="list" /> | |
<xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" /> | |
<xsl:variable name="first" select="substring-before($newlist, ' ')" /> | |
<xsl:variable name="remaining" select="substring-after($newlist, ' ')" /> | |
[ <xsl:value-of select="$first" /> ] | |
<xsl:if test="$remaining"> | |
, | |
<xsl:call-template name="dump-coordinates"> | |
<xsl:with-param name="list" select="$remaining" /> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="gml:LineString"> | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [ | |
<xsl:call-template name="dump-coordinates"> | |
<xsl:with-param name="list" select="gml:coordinates" /> | |
</xsl:call-template> | |
] | |
} | |
</xsl:template> | |
<xsl:template match="gml:Point"> | |
"geometry": { | |
"type": "Point", | |
"coordinates": | |
<xsl:call-template name="dump-coordinates"> | |
<xsl:with-param name="list" select="gml:coordinates" /> | |
</xsl:call-template> | |
} | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment