Created
August 18, 2015 19:58
-
-
Save gmccoy/e3e025c84d23381f9b7b to your computer and use it in GitHub Desktop.
XSLT for transforming NOAA DWML weather data
This file contains 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"?> | |
<!-- Partially inspired by https://github.com/greencoder/noaa-dwml-to-json-xslt/blob/master/noaa.xsl --> | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/> | |
<xsl:template match="dwml"> | |
<WeatherData> | |
<CurrentConditions> | |
<xsl:apply-templates select="/dwml/data[@type="current observations"]" /> | |
</CurrentConditions> | |
<Forecast> | |
<xsl:apply-templates select="/dwml/data[@type="forecast"]/parameters/weather" /> | |
</Forecast> | |
<Warnings> | |
<xsl:apply-templates select="//hazard" /> | |
</Warnings> | |
</WeatherData> | |
</xsl:template> | |
<xsl:template match="data[@type="current observations"]"> | |
<Icon> | |
<xsl:value-of select="parameters/conditions-icon/icon-link" /> | |
</Icon> | |
<Description> | |
<xsl:value-of select="parameters/weather/weather-conditions/@weather-summary" /> | |
</Description> | |
<Temperature> | |
<xsl:value-of select="parameters/temperature[@type="apparent"]/value" /> | |
</Temperature> | |
<DewPoint> | |
<xsl:value-of select="parameters/temperature[@type="dew point"]/value" /> | |
</DewPoint> | |
<RelativeHumidity> | |
<xsl:value-of select="parameters/humidity[@type="relative"]/value" /> | |
</RelativeHumidity> | |
<WindSpeed type="sustained"> | |
<xsl:value-of select="parameters/wind-speed[@type="sustained"]/value" /> | |
</WindSpeed> | |
<WindSpeed type="gust"> | |
<xsl:value-of select="parameters/wind-speed[@type="gust"]/value" /> | |
</WindSpeed> | |
<WindDirection> | |
<xsl:value-of select="parameters/direction[@type="wind"]/value" /> | |
</WindDirection> | |
<Barometer> | |
<xsl:value-of select="parameters/pressure[@type="barometer"]/value" /> | |
</Barometer> | |
</xsl:template> | |
<xsl:template match="weather"> | |
<xsl:variable name="tl-key" select="@time-layout" /> | |
<xsl:for-each select="weather-conditions"> | |
<xsl:variable name="pos" select="position()" /> | |
<xsl:variable name="startTime" select="//layout-key[text()=$tl-key]/../start-valid-time[position()=$pos]" /> | |
<xsl:variable name="tl-maxTemp-key" select="//temperature[@type="maximum"]/@time-layout" /> | |
<xsl:variable name="tl-minTemp-key" select="//temperature[@type="minimum"]/@time-layout" /> | |
<Period> | |
<Description> | |
<xsl:value-of select="//layout-key[text()=$tl-key]/../start-valid-time[position()=$pos]/@period-name" /> | |
</Description> | |
<StartTime> | |
<xsl:value-of select="$startTime" /> | |
</StartTime> | |
<HighTemp> | |
<xsl:for-each select="//layout-key[text()=$tl-maxTemp-key]/../start-valid-time"> | |
<xsl:variable name="test" select="position()" /> | |
<xsl:if test="text()=$startTime"> | |
<xsl:value-of select="//temperature[@type="maximum"]//value[$test]" /> | |
</xsl:if> | |
</xsl:for-each> | |
</HighTemp> | |
<LowTemp> | |
<xsl:for-each select="//layout-key[text()=$tl-minTemp-key]/../start-valid-time"> | |
<xsl:variable name="test" select="position()" /> | |
<xsl:if test="text()=$startTime"> | |
<xsl:value-of select="//temperature[@type="minimum"]//value[$test]" /> | |
</xsl:if> | |
</xsl:for-each> | |
</LowTemp> | |
<Conditions> | |
<xsl:value-of select="@weather-summary" /> | |
</Conditions> | |
<Icon> | |
<xsl:value-of select="//conditions-icon[@time-layout=$tl-key]/icon-link[position()=$pos]" /> | |
</Icon> | |
<Detail> | |
<xsl:value-of select="//wordedForecast[@time-layout=$tl-key]/text[position()=$pos]" /> | |
</Detail> | |
</Period> | |
</xsl:for-each> | |
</xsl:template> | |
<xsl:template match="hazard"> | |
<Warning><xsl:value-of select="@headline" /></Warning> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment