Skip to content

Instantly share code, notes, and snippets.

@gmccoy
Created August 18, 2015 19:58
Show Gist options
  • Save gmccoy/e3e025c84d23381f9b7b to your computer and use it in GitHub Desktop.
Save gmccoy/e3e025c84d23381f9b7b to your computer and use it in GitHub Desktop.
XSLT for transforming NOAA DWML weather data
<?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=&quot;current observations&quot;]" />
</CurrentConditions>
<Forecast>
<xsl:apply-templates select="/dwml/data[@type=&quot;forecast&quot;]/parameters/weather" />
</Forecast>
<Warnings>
<xsl:apply-templates select="//hazard" />
</Warnings>
</WeatherData>
</xsl:template>
<xsl:template match="data[@type=&quot;current observations&quot;]">
<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=&quot;apparent&quot;]/value" />
</Temperature>
<DewPoint>
<xsl:value-of select="parameters/temperature[@type=&quot;dew point&quot;]/value" />
</DewPoint>
<RelativeHumidity>
<xsl:value-of select="parameters/humidity[@type=&quot;relative&quot;]/value" />
</RelativeHumidity>
<WindSpeed type="sustained">
<xsl:value-of select="parameters/wind-speed[@type=&quot;sustained&quot;]/value" />
</WindSpeed>
<WindSpeed type="gust">
<xsl:value-of select="parameters/wind-speed[@type=&quot;gust&quot;]/value" />
</WindSpeed>
<WindDirection>
<xsl:value-of select="parameters/direction[@type=&quot;wind&quot;]/value" />
</WindDirection>
<Barometer>
<xsl:value-of select="parameters/pressure[@type=&quot;barometer&quot;]/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=&quot;maximum&quot;]/@time-layout" />
<xsl:variable name="tl-minTemp-key" select="//temperature[@type=&quot;minimum&quot;]/@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=&quot;maximum&quot;]//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=&quot;minimum&quot;]//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