Last active
August 29, 2015 14:16
-
-
Save mfurlend/0a002959651d02745890 to your computer and use it in GitHub Desktop.
XSLT for parsing dwml from forecast.weather.gov
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
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> | |
<xsl:output method="text" encoding="UTF-8" /> | |
<xsl:template name="replace-string"> | |
<xsl:param name="text"/> | |
<xsl:param name="replace"/> | |
<xsl:param name="with"/> | |
<xsl:choose> | |
<xsl:when test="contains($text,$replace)"> | |
<xsl:value-of select="substring-before($text,$replace)"/> | |
<xsl:value-of select="$with"/> | |
<xsl:call-template name="replace-string"> | |
<xsl:with-param name="text" select="substring-after($text,$replace)"/> | |
<xsl:with-param name="replace" select="$replace"/> | |
<xsl:with-param name="with" select="$with"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$text"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
<xsl:variable name="weatherCond" select="//weather-conditions/value[1]" /> | |
<xsl:variable name="weatherCondMore" select="//weather-conditions/value[2]" /> | |
<xsl:template match="/"> | |
<xsl:for-each select="//start-valid-time"> | |
<xsl:variable name="myPos" select="position()" /> | |
<xsl:variable name="txt"> | |
<xsl:value-of select="$weatherCond[position() = $myPos]/@coverage" /> | |
<xsl:text> </xsl:text> | |
<xsl:value-of select="$weatherCond[position() = $myPos]/@weather-type" /> | |
<xsl:if test="$weatherCondMore[position() = $myPos]/@coverage !=''"> | |
<xsl:text> and </xsl:text> | |
<xsl:value-of select="$weatherCondMore[position() = $myPos]/@coverage" /> | |
<xsl:text> </xsl:text> | |
<xsl:value-of select="$weatherCondMore[position() = $myPos]/@weather-type" /> | |
</xsl:if> | |
</xsl:variable> | |
<xsl:if test="$txt != ' '"> | |
<xsl:value-of select="." /> | |
<xsl:text>,</xsl:text> | |
<xsl:call-template name="replace-string"> | |
<xsl:with-param name="text" select="$txt"/> | |
<xsl:with-param name="replace" select="'chance '" /> | |
<xsl:with-param name="with" select="'chance of '"/> | |
</xsl:call-template> | |
<xsl:value-of select="'
'" /> | |
</xsl:if> | |
</xsl:for-each> | |
</xsl:template> | |
</xsl:transform> | |
<!-- usage: | |
xmlstarlet tr xsl XML | |
(where xsl is this XSLT file, and XML is a forecast.weather.gov XML file) | |
output: | |
2015-03-11T14:00:00-04:00,chance of rain and slight chance of thunderstorms | |
2015-03-11T15:00:00-04:00,chance of rain and slight chance of thunderstorms | |
.. | |
2015-03-15T18:00:00-04:00,slight chance of rain | |
2015-03-15T19:00:00-04:00,slight chance of rain | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment