<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<!-- NaturalViewFoX.xsl                                                    -->
<!-- Jim Calkins, MDL                                                      -->
<!--                                                                       -->
<!-- This simple Stylesheet will present the contents of the Observations  -->
<!-- in XML (FoX) in a simple format that is easy to view by the user.     -->

<xsl:template match="dwml">
<!-- Create some variables for later use. -->
   <xsl:variable name="cities" select="data/location" />
   <xsl:variable name="layouts" select="data/time-layout" />
<!-- Output descriptive header information -->
   <xsl:value-of select="head/product/title" />
   <xsl:text>&#10;</xsl:text>
<!-- Format the date -->
   <xsl:value-of select="head/product/creation-date" />
   <xsl:text>&#10;&#10;</xsl:text>
<!-- Output the header -->
   <xsl:text>                                       HI   LO  WX</xsl:text>
   <xsl:text>&#10;</xsl:text>
   <xsl:for-each select="data/parameters">
<!-- Output the METAR ID and City Name -->
      <xsl:variable name="loc" select="@applicable-location"/>
      <xsl:value-of select="$loc"/>
      <xsl:text>&#32;</xsl:text>
      <xsl:variable name="cityname" select="$cities[location-key=$loc]/city"/>
      <xsl:value-of select="substring(concat(string($cityname), '                    '), 1, 20)" />
<!-- Determine which weather is first, daytime or nighttime -->
      <xsl:variable name="maxtl" select="temperature[@type='maximum']/attribute::time-layout"/>
      <xsl:variable name="wxtl" select="weather/attribute::time-layout"/>
      <xsl:variable name="maxstrt" select="translate($layouts[layout-key=$maxtl]/start-valid-time[1], ':T-Z', '')"/>
      <xsl:variable name="wxend" select="translate($layouts[layout-key=$wxtl]/end-valid-time[1], ':T-Z', '')"/>
      <xsl:variable name="maxminstring">      
         <xsl:choose>
            <xsl:when test="number(substring($maxstrt, 1, 14)) &gt; number(substring($wxend, 1, 14))">
               <xsl:text> Night/Day </xsl:text>
            </xsl:when>
            <xsl:otherwise>
               <xsl:text> Day/Night </xsl:text>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:variable>
<!-- Output the day/night 1 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="1"/>
         <xsl:with-param name="wxnum1" select="1"/>
         <xsl:with-param name="wxnum2" select="2"/>
      </xsl:call-template>
<!-- Output the day/night 2 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="2"/>
         <xsl:with-param name="wxnum1" select="3"/>
         <xsl:with-param name="wxnum2" select="4"/>
      </xsl:call-template>
<!-- Output the day/night 3 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="3"/>
         <xsl:with-param name="wxnum1" select="5"/>
         <xsl:with-param name="wxnum2" select="6"/>
      </xsl:call-template>
<!-- Output the day/night 4 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="4"/>
         <xsl:with-param name="wxnum1" select="7"/>
         <xsl:with-param name="wxnum2" select="8"/>
      </xsl:call-template>
<!-- Output the day/night 5 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="5"/>
         <xsl:with-param name="wxnum1" select="9"/>
         <xsl:with-param name="wxnum2" select="10"/>
      </xsl:call-template>
<!-- Output the day/night 6 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="6"/>
         <xsl:with-param name="wxnum1" select="11"/>
         <xsl:with-param name="wxnum2" select="12"/>
      </xsl:call-template>
<!-- Output the day/night 7 data -->
      <xsl:call-template name="formatmaxminwx">
         <xsl:with-param name="maxminstring" select="$maxminstring"/>
         <xsl:with-param name="mxmnnum" select="7"/>
         <xsl:with-param name="wxnum1" select="13"/>
         <xsl:with-param name="wxnum2" select="14"/>
      </xsl:call-template>
      <xsl:text>&#10;</xsl:text>
   </xsl:for-each>
   <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template name="formatmaxminwx">
   <xsl:param name="maxminstring" select="''"/>
   <xsl:param name="mxmnnum" select="''"/>
   <xsl:param name="wxnum1" select="''"/>
   <xsl:param name="wxnum2" select="''"/>
      <xsl:if test="number($mxmnnum) &gt; 1"><xsl:text>                         </xsl:text></xsl:if>
      <xsl:value-of select="$maxminstring"/>
      <xsl:value-of select="$mxmnnum"/>
      <xsl:text>&#32;</xsl:text>
      <xsl:if test="string-length(temperature[@type='maximum']/value[$mxmnnum]) &lt; 3"><xsl:text>&#32;</xsl:text></xsl:if>
      <xsl:if test="string-length(temperature[@type='maximum']/value[$mxmnnum]) &lt; 2"><xsl:text>&#32;</xsl:text></xsl:if>
      <xsl:value-of select="temperature[@type='maximum']/value[$mxmnnum]"/>
      <xsl:text>F</xsl:text>
      <xsl:text>&#32;</xsl:text>
      <xsl:if test="string-length(temperature[@type='minimum']/value[$mxmnnum]) &lt; 3"><xsl:text>&#32;</xsl:text></xsl:if>
      <xsl:if test="string-length(temperature[@type='minimum']/value[$mxmnnum]) &lt; 2"><xsl:text>&#32;</xsl:text></xsl:if>
      <xsl:value-of select="temperature[@type='minimum']/value[$mxmnnum]"/>
      <xsl:text>F</xsl:text>
      <xsl:text>&#32;</xsl:text>
      <xsl:value-of select="substring(concat(string(weather/weather-conditions[$wxnum1]/attribute::weather-summary), '                         '), 1, 25)" />
      <xsl:value-of select="substring(concat(string(weather/weather-conditions[$wxnum2]/attribute::weather-summary), '                         '), 1, 25)" />
      <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>