summaryrefslogtreecommitdiffstats
path: root/capatomcustom.xsl
blob: b9a57264f57175e00f506aa9e2fb540168cce145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:cap="urn:oasis:names:tc:emergency:cap:1.1"
  xmlns:ha="http://www.alerting.net/namespace/index_1.0" version="1.0">

  <xsl:import href="dst_check.xsl"/>
  
  <xsl:output method="html"
      indent="yes"
      omit-xml-declaration="yes"/>
  
  
  <!-- Feed header -->
  <xsl:template match="cap:alert">
      <table width="525" cellspacing="2" cellpadding="0" border="0">
        <tr valign="top">
          <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
          <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
          <xsl:variable name="st">
           <xsl:value-of select="substring(substring-after(cap:identifier/text(),'NOAA-NWS-ALERTS-'),1,2)"/>
          </xsl:variable>
          <xsl:variable name="lcst">
           <xsl:value-of select="translate(substring(substring-after(cap:identifier/text(),'NOAA-NWS-ALERTS-'),1,2),$ucletters,$lcletters)"/>
          </xsl:variable>

          <xsl:variable name="forecastoffice">
            <xsl:value-of select="cap:info/cap:senderName/text()"/>
          </xsl:variable>

          <td width="100%" colspan="2"><p><a name="contents" id="contents"></a><a href="http://www.weather.gov/">Home</a> &gt; <a href="http://alerts.weather.gov/">Alerts</a> &gt; 
           <a border="0">
            <xsl:attribute name="href">http://alerts.weather.gov/cap/<xsl:copy-of select="$lcst"/>.php?x=1</xsl:attribute>
            <xsl:choose>
             <xsl:when test="contains(cap:note/text(),'(')">
              <xsl:value-of select="substring-before(substring-after(cap:note/text(),'('),')')"/>
             </xsl:when>
             <xsl:otherwise>
              <xsl:copy-of select="$st"/>
             </xsl:otherwise>
            </xsl:choose>
           </a>
           &gt; <xsl:value-of select="cap:info/cap:event/text()"/> Issued by <xsl:copy-of select="$forecastoffice"/></p>
           <table style="width: 100%; margin-top: 5px; background-color: #e0e0e0; font-family: sans-serif; border: 1px solid black;">
            <tr>
             <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Message:</td>
             <td>
              <xsl:value-of select="cap:identifier/text()"/><span style="text-align: left;"> from </span><xsl:value-of select="cap:sender/text()"/>
             </td>
            </tr>
            <tr>
             <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Sent:</td>
             <td>
              <xsl:value-of select="substring(normalize-space(cap:sent/text()), 12, 5)"/>
               <xsl:call-template name="timeZoneName">
                <xsl:with-param name="offset" select="substring(normalize-space(cap:sent/text()), 20, 6)"/>
                <xsl:with-param name="year" select="substring(normalize-space(cap:sent/text()), 1, 4)"/>
                <xsl:with-param name="month" select="substring(normalize-space(cap:sent/text()), 6, 2)"/>
                <xsl:with-param name="day" select="substring(normalize-space(cap:sent/text()), 9, 2)"/>
                <xsl:with-param name="st" select="$lcst"/>
               </xsl:call-template>
               <span style="text-align: left;"> on </span>
               <xsl:value-of select="substring(normalize-space(cap:sent/text()), 6, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:sent/text()), 9, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:sent/text()), 1, 4)"/>
             </td>
            </tr>
            <tr>
             <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Effective:</td>
             <td>
              <xsl:value-of select="substring(normalize-space(cap:info/cap:effective/text()), 12, 5)"/>
              <xsl:call-template name="timeZoneName">
               <xsl:with-param name="offset" select="substring(normalize-space(cap:info/cap:effective/text()), 20, 6)"/>
               <xsl:with-param name="year" select="substring(normalize-space(cap:info/cap:effective/text()), 1, 4)"/>
               <xsl:with-param name="month" select="substring(normalize-space(cap:info/cap:effective/text()), 6, 2)"/>
               <xsl:with-param name="day" select="substring(normalize-space(cap:info/cap:effective/text()), 9, 2)"/>
               <xsl:with-param name="st" select="$lcst"/>
              </xsl:call-template>
              <span style="text-align: left;"> on </span>
              <xsl:value-of select="substring(normalize-space(cap:info/cap:effective/text()), 6, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:info/cap:effective/text()), 9, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:info/cap:effective/text()), 1, 4)"/>
             </td>
            </tr>
            <tr>
             <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Expires:</td>
             <td>
              <xsl:value-of select="substring(normalize-space(cap:info/cap:expires/text()), 12, 5)"/>
              <xsl:call-template name="timeZoneName">
               <xsl:with-param name="offset" select="substring(normalize-space(cap:info/cap:expires/text()), 20, 6)"/>
               <xsl:with-param name="year" select="substring(normalize-space(cap:info/cap:expires/text()), 1, 4)"/>
               <xsl:with-param name="month" select="substring(normalize-space(cap:info/cap:expires/text()), 6, 2)"/>
               <xsl:with-param name="day" select="substring(normalize-space(cap:info/cap:expires/text()), 9, 2)"/>
               <xsl:with-param name="st" select="$lcst"/>
              </xsl:call-template>
              <span style="text-align: left;"> on </span>
              <xsl:value-of select="substring(normalize-space(cap:info/cap:expires/text()), 6, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:info/cap:expires/text()), 9, 2)"/>-<xsl:value-of select="substring(normalize-space(cap:info/cap:expires/text()), 1, 4)"/>
             </td>
            </tr>
            <tr>
             <td colspan="2">
              <table style="background-color: #ffffff; width: 100%; margin-top: 5px; border: 1px solid black;">
               <tr>
                <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Event:</td>
                <td style="font-size: 1.2em; font-weight: bold;"><xsl:value-of select="cap:info/cap:event/text()"/></td>
               </tr>
               <tr>
                <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Alert:</td>
                <td style="font-size: 1.0em;">
                 <xsl:variable name="descrip">
                  <xsl:call-template name="globalReplace">
                   <xsl:with-param name="outputString" select="substring(cap:info/cap:description/text(),1)"/>
                   <xsl:with-param name="target" select="'&#xa;&#42;'"/>
                   <xsl:with-param name="replacement" select="'&#xa;&#xa;&#42;'"/>
                  </xsl:call-template> 
                 </xsl:variable>
                 <xsl:variable name="descrip2">
                  <xsl:call-template name="globalReplace">
                   <xsl:with-param name="outputString" select="$descrip"/>
                   <xsl:with-param name="target" select="'&#xa;&#46;'"/>
                   <xsl:with-param name="replacement" select="'&#xa;&#xa;&#46;'"/>
                  </xsl:call-template> 
                 </xsl:variable>
                 <xsl:variable name="descrip3">
                  <xsl:call-template name="globalReplace">
                   <xsl:with-param name="outputString" select="$descrip2"/>
                   <xsl:with-param name="target" select="'&#46;&#46;&#46;&#xa;THE'"/>
                   <xsl:with-param name="replacement" select="'&#46;&#46;&#46;&#xa;&#xa;THE'"/>
                  </xsl:call-template> 
                 </xsl:variable>
                 <xsl:variable name="descrip4">
                  <xsl:call-template name="globalReplace">
                   <xsl:with-param name="outputString" select="$descrip3"/>
                   <xsl:with-param name="target" select="'&#46;&#46;&#46;&#xa;A'"/>
                   <xsl:with-param name="replacement" select="'&#46;&#46;&#46;&#xa;&#xa;A'"/>
                  </xsl:call-template> 
                 </xsl:variable>
                 <!-- force a space before newline to facilitate url links -->
                 <xsl:variable name="descrip5">
                  <xsl:call-template name="globalReplace">
                   <xsl:with-param name="outputString" select="$descrip4"/>
                   <xsl:with-param name="target" select="'&#xa;'"/>
                   <xsl:with-param name="replacement" select="' &#xa;'"/>
                  </xsl:call-template>
                 </xsl:variable>
                 <!-- make links hot -->
                 <xsl:variable name="descrip6">
                  <xsl:call-template name="hotlink">
                   <xsl:with-param name="text" select="$descrip5"/>
                   <xsl:with-param name="searchterm1" select="'HTTP'"/>
                  </xsl:call-template>
                 </xsl:variable>
                 <pre style="font-size: 1.0em;">
                  <xsl:copy-of select="$descrip6"/>
                 </pre>
                </td>
               </tr>
               <tr>
                <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Instructions:</td>
                <td style="font-size: 1.0em;">
                  <xsl:value-of select="cap:info/cap:instruction/text()"/>
                </td>
               </tr>
               <tr>
                <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Target Area:</td>
                <td>
                 <table style="background-color: #e0e0e0; margin-right: 20px; font-weight: bold;">
                  <tr>
                   <td colspan="2">
                    <xsl:variable name="areas1">
                     <xsl:call-template name="globalReplace">
                      <xsl:with-param name="outputString" select="cap:info/cap:area/cap:areaDesc/text()"/>
                      <xsl:with-param name="target" select="'/'"/>
                      <xsl:with-param name="replacement" select="'&#xa;'"/>
                     </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="areas">
                     <xsl:call-template name="globalReplace">
                      <xsl:with-param name="outputString" select="$areas1"/>
                      <xsl:with-param name="target" select="';'"/>
                      <xsl:with-param name="replacement" select="'&#xa;'"/>
                     </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="firstchar" select="substring($areas,1,1)"/>
                    <xsl:choose>
                     <xsl:when test = "$firstchar = '&#xa;'"> <!-- first character is a carriage return -->
                      <xsl:call-template name="br-replace">
                       <xsl:with-param name="text" select="substring($areas,2)"/>
                      </xsl:call-template>
                     </xsl:when>
                     <xsl:otherwise>
                      <xsl:call-template name="br-replace">
                       <xsl:with-param name="text" select="$areas"/>
                      </xsl:call-template>
                     </xsl:otherwise>
                    </xsl:choose>
                   </td>
                  </tr>
                 </table>
                </td>
               </tr>
              </table>
             </td>
            </tr>
            <tr>
             <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Forecast Office:</td>
             <td><xsl:value-of select="cap:info/cap:senderName/text()"/></td>
            </tr>
            <tr>
             <td colspan="2">
              <table style="background-color: #ffffff; width: 100%; margin-top: 5px; border: 1px solid black;">
                <tr>
                    <td style="width: 25px; font-weight: bold; vertical-align: text-top; text-align: right;">Raw CAP Message:</td>
                    <td style="font-size: 0.9em; color: #606060">
                      <pre>
                        <xsl:apply-templates select="/cap:alert" mode="escape"/>
                      </pre>
                    </td>
                </tr>
               </table>
             </td>
            </tr>
           </table>
          </td>
        </tr>
      </table> 
     <!-- end of main content -->
  </xsl:template>


    <xsl:template match="*" mode="escape">
        <!-- Begin opening tag -->
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="name()"/>

        <!-- Attributes -->
        <xsl:for-each select="@*">
            <xsl:text> </xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>='</xsl:text>
            <xsl:call-template name="escape-xml">
                <xsl:with-param name="text" select="."/>
            </xsl:call-template>
            <xsl:text>'</xsl:text>
        </xsl:for-each>

        <!-- End opening tag -->
        <xsl:text>&gt;</xsl:text>

        <!-- Content (child elements, text nodes, and PIs) -->
        <xsl:apply-templates select="node()" mode="escape" />

        <!-- Closing tag -->
        <xsl:text>&lt;/</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>&gt;&#10;</xsl:text>
    </xsl:template>

    <xsl:template match="text()" mode="escape">
        <xsl:call-template name="escape-xml">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="processing-instruction()" mode="escape">
        <xsl:text>&lt;?</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text> </xsl:text>
        <xsl:call-template name="escape-xml">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
        <xsl:text>?&gt;</xsl:text>
    </xsl:template>

    <xsl:template name="escape-xml">
        <xsl:param name="text"/>
        <xsl:if test="$text != ''">
            <xsl:variable name="head" select="substring($text, 1, 1)"/>
            <xsl:variable name="tail" select="substring($text, 2)"/>
            <xsl:choose>
                <xsl:when test="$head = '&amp;'">&amp;amp;</xsl:when>
                <xsl:when test="$head = '&lt;'">&amp;lt;</xsl:when>
                <xsl:when test="$head = '&gt;'">&amp;gt;</xsl:when>
                <xsl:when test="$head = '&quot;'">&amp;quot;</xsl:when>
                <xsl:when test="$head = &quot;&apos;&quot;">&amp;apos;</xsl:when>
                <xsl:otherwise><xsl:value-of select="$head"/></xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="escape-xml">
                <xsl:with-param name="text" select="$tail"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

  <!-- Time Zone function -->
  <xsl:template name="timeZoneName">
   <xsl:param name="offset"/>
   <xsl:param name="year"/>
   <xsl:param name="month"/>
   <xsl:param name="day"/>
   <xsl:param name="st"/>

   <!-- determine if its DST or not, based on run time -->
   <xsl:variable name="dst_start">
    <xsl:call-template name="day-light-savings-start">
     <xsl:with-param name="year" select="$year"/>
    </xsl:call-template>
   </xsl:variable>

   <xsl:variable name="dst_end">
    <xsl:call-template name="day-light-savings-end">
     <xsl:with-param name="year" select="$year"/>
    </xsl:call-template>
   </xsl:variable>

   <xsl:variable name="tod_jul">
    <xsl:call-template name="date-to-absolute-day">
     <xsl:with-param name="year" select="$year"/>
     <xsl:with-param name="month" select="$month"/>
     <xsl:with-param name="day" select="$day"/>
    </xsl:call-template>
   </xsl:variable>

   <xsl:variable name="dst_flag">
    <xsl:choose>
     <xsl:when test="$dst_start &lt;= $tod_jul and $dst_end &gt;= $tod_jul">dst</xsl:when>
     <xsl:otherwise>nodst</xsl:otherwise>
    </xsl:choose>
   </xsl:variable>

   <xsl:if test="$offset = '+10:00'"> <xsl:text> ChST</xsl:text> </xsl:if>
   <xsl:if test="$offset = '-11:00'"> <xsl:text> SST</xsl:text> </xsl:if>
   <xsl:if test="$offset = '-10:00'"> <xsl:text> HST</xsl:text> </xsl:if>
   <xsl:if test="$offset = '-00:00'"> <xsl:text> GMT</xsl:text> </xsl:if>
   <xsl:if test="$offset = '+00:00'"> <xsl:text> GMT</xsl:text> </xsl:if>

   <xsl:choose>
    <xsl:when test="$dst_flag = 'nodst'">
     <xsl:if test="$offset = '-09:00'"> <xsl:text> AKST</xsl:text> </xsl:if>
     <xsl:if test="$offset = '-08:00'"> <xsl:text> PST</xsl:text> </xsl:if>
     <xsl:if test="$offset = '-07:00'"> <xsl:text> MST</xsl:text> </xsl:if>
     <xsl:if test="$offset = '-06:00'"> <xsl:text> CST</xsl:text> </xsl:if>
     <xsl:if test="$offset = '-05:00'"> <xsl:text> EST</xsl:text> </xsl:if>
     <xsl:if test="$offset = '-04:00'"> <xsl:text> AST</xsl:text> </xsl:if>
    </xsl:when>
    <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$st = 'az'">
       <xsl:if test="$offset = '-08:00'"> <xsl:text> PST</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-07:00'"> <xsl:text> MST</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-06:00'"> <xsl:text> CST</xsl:text> </xsl:if>
      </xsl:when>
      <xsl:otherwise>
       <xsl:if test="$offset = '-08:00'"> <xsl:text> AKDT</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-07:00'"> <xsl:text> PDT</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-06:00'"> <xsl:text> MDT</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-05:00'"> <xsl:text> CDT</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-04:00'"> <xsl:text> EDT</xsl:text> </xsl:if>
       <xsl:if test="$offset = '-03:00'"> <xsl:text> ADT</xsl:text> </xsl:if>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
   </xsl:choose>

  </xsl:template>

  <!-- Replace function -->
  <xsl:template name="globalReplace">
   <xsl:param name="outputString"/>
   <xsl:param name="target"/>
   <xsl:param name="replacement"/>
   <xsl:choose>
     <xsl:when test="contains($outputString,$target)">

       <xsl:value-of select=
         "concat(substring-before($outputString,$target),$replacement)"/>
       <xsl:call-template name="globalReplace">
         <xsl:with-param name="outputString" select="substring-after($outputString,$target)"/>
         <xsl:with-param name="target" select="$target"/>
         <xsl:with-param name="replacement" select="$replacement"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$outputString"/>
     </xsl:otherwise>
   </xsl:choose>
  </xsl:template>

  <!-- Replace new lines with html <br> tags -->
  <xsl:template name="br-replace">
    <xsl:param name="text"/>
    <xsl:variable name="cr" select="'&#xa;'"/>
    <xsl:choose>
      <!-- If the value of the $text parameter contains carriage ret -->
      <xsl:when test="contains($text,$cr)">
        <!-- Return the substring of $text before the carriage return -->
        <xsl:value-of select="substring-before($text,$cr)"/>
        <!-- And construct a <br/> element -->
        <br/>
        <!--
         | Then invoke this same br-replace template again, passing the
         | substring *after* the carriage return as the new "$text" to
         | consider for replacement
         +-->
        <xsl:call-template name="br-replace">
          <xsl:with-param name="text" select="substring-after($text,$cr)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
   </xsl:choose>
  </xsl:template>

  <!-- If it looks like a link, make it hot -->
  <xsl:template name="hotlink">
   <xsl:param name="text"/>
   <xsl:param name="searchterm1"/>

   <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
   <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

   <xsl:choose>
    <xsl:when test="contains($text,$searchterm1)">

     <!-- grab data before the searchterm -->
      <xsl:value-of select="substring-before($text,$searchterm1)"/>
      <a border="0">
      <xsl:attribute name="href"> 
     <!-- determine actual link by taking data from the searchterm to 
          either the space or the newline - whichever is first  -->
      <xsl:value-of select="translate(concat($searchterm1,substring-before(substring-after($text,$searchterm1),' ')),$ucletters,$lcletters)"/>
     </xsl:attribute>
     <xsl:value-of select="translate(concat($searchterm1,substring-before(substring-after($text,$searchterm1),' ')),$ucletters,$lcletters)"/> </a>

     <!-- data after the link - check for more links -->
     <xsl:call-template name="hotlink">
      <xsl:with-param name="text" select="substring-after(substring-after($text,$searchterm1),' ')"/>
      <xsl:with-param name="searchterm1" select="$searchterm1"/>
     </xsl:call-template> 
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$text"/>
    </xsl:otherwise> 
   </xsl:choose> 
  </xsl:template>

  <!-- Ignore anything else -->
  <xsl:template match="*"></xsl:template>

</xsl:stylesheet>