summaryrefslogtreecommitdiffstats
path: root/xslt/uddf-export.xslt
blob: b37a145ee1942543a318a1a24adc3c725c43a45f (plain) (blame)
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
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:include href="commonTemplates.xsl"/>
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <xsl:key name="gases" match="cylinder" use="concat(substring-before(@o2, '.'), '/', substring-before(@he, '.'))" />

  <xsl:template match="/divelog/dives">
    <uddf version="3.2.0">
      <generator>
        <manufacturer id="subsurface">
          <name>Subsurface Team</name>
          <contact>http://subsurface.hohndel.org/</contact>
        </manufacturer>
        <version>
          <xsl:value-of select="/divelog/@version"/>
        </version>
        <xsl:if test="/divelog/generator/@date != ''">
          <datetime>
            <xsl:value-of select="concat(/divelog/generator/@date, 'T', /divelog/generator/@time)"/>
          </datetime>
        </xsl:if>
      </generator>

      <diver>
        <owner id="1">
          <equipment>
            <xsl:for-each select="/divelog/settings/divecomputerid">
              <divecomputer id="{./@deviceid}">
                <name>
                  <xsl:choose>
                    <xsl:when test="./@nickname != ''">
                      <xsl:value-of select="./@nickname"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="./@model"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </name>
                <model>
                  <xsl:value-of select="./@model"/>
                </model>
              </divecomputer>
            </xsl:for-each>
          </equipment>
        </owner>

        <xsl:apply-templates select="//buddy"/>
      </diver>

      <xsl:apply-templates select="//location"/>

      <!-- Define all the unique gases found in the dive log -->
      <gasdefinitions>
        <!-- Get unique gas mixes from all the recorded dives -->
        <xsl:for-each select="dive/cylinder[generate-id() = generate-id(key('gases', concat(substring-before(@o2, '.'), '/', substring-before(@he, '.')))[1])]">

          <xsl:variable name="o2">
            <xsl:choose>
              <xsl:when test="@o2 != ''">
                <xsl:value-of select="substring-before(@o2, '.')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="'21'"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="he">
            <xsl:choose>
              <xsl:when test="@he != ''">
                <xsl:value-of select="substring-before(@he, '.')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="'0'"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <!-- The gas definitions will have the o2 percentage as mix ID
               to ease up the reference on switchmix events. Thus we can
               just use the same references used internally on
               Subsurface.
          -->
          <mix id="{$o2}">
            <name>
              <xsl:value-of select="concat($o2, '/', $he)"/>
            </name>
            <o2>
              <xsl:value-of select="$o2"/>
            </o2>
            <he>
              <xsl:value-of select="$he"/>
            </he>
          </mix>
        </xsl:for-each>
      </gasdefinitions>

      <profiledata>
        <xsl:for-each select="trip">
          <repetitiongroup id="{generate-id(.)}">
            <xsl:apply-templates select="dive"/>
          </repetitiongroup>
        </xsl:for-each>
        <xsl:for-each select="dive">
          <repetitiongroup id="{generate-id(.)}">
            <xsl:apply-templates select="."/>
          </repetitiongroup>
        </xsl:for-each>
      </profiledata>
    </uddf>
  </xsl:template>

  <xsl:key name="buddy" match="buddy" use="."/>
  <xsl:template match="buddy">
    <xsl:if test="generate-id() = generate-id(key('buddy', normalize-space(.)))">
      <buddy>
        <xsl:attribute name="id">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <personal>
          <first_name>
            <xsl:value-of select="."/>
          </first_name>
        </personal>
      </buddy>
      </xsl:if>
  </xsl:template>

  <xsl:key name="location" match="location" use="."/>
  <xsl:template match="location">
    <xsl:if test="generate-id() = generate-id(key('location', normalize-space(.)))">
      <dive_site>
        <xsl:attribute name="id">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <name>
          <xsl:value-of select="."/>
        </name>
        <geography>
          <location>
            <xsl:value-of select="."/>
          </location>
          <gps>
            <latitude>
              <xsl:value-of select="substring-before(@gps, ' ')"/>
            </latitude>
            <longitude>
              <xsl:value-of select="substring-after(@gps, ' ')"/>
            </longitude>
          </gps>
        </geography>
      </dive_site>
      </xsl:if>
  </xsl:template>

  <xsl:template match="dive">
    <dive id="{generate-id(.)}">

      <informationbeforedive>
        <xsl:if test="temperature/@air|divetemperature/@air != ''">
          <airtemperature>
            <xsl:value-of select="format-number(substring-before(temperature/@air|divetemperature/@air, ' ') + 273.15, '0.00')"/>
          </airtemperature>
        </xsl:if>
        <datetime>
          <xsl:value-of select="concat(./@date, 'T', ./@time)"/>
        </datetime>
        <divenumber>
          <xsl:value-of select="./@number"/>
        </divenumber>
        <xsl:if test="buddy != ''">
          <buddy_ref>
            <xsl:attribute name="ref">
              <xsl:value-of select="buddy"/>
            </xsl:attribute>
          </buddy_ref>
        </xsl:if>
        <xsl:if test="location != ''">
          <dive_site_ref>
            <xsl:attribute name="ref">
              <xsl:value-of select="location"/>
            </xsl:attribute>
          </dive_site_ref>
        </xsl:if>
      </informationbeforedive>

      <!-- We get sample interval from the time between first and second
           sample to include all events in the existing samples.
           -->
      <xsl:variable name="timefirst">
        <xsl:call-template name="time2sec">
          <xsl:with-param name="time">
            <xsl:value-of select="./divecomputer[1]/sample[1]/@time"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="timesecond">
        <xsl:call-template name="time2sec">
          <xsl:with-param name="time">
            <xsl:value-of select="./divecomputer[1]/sample[2]/@time"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="delta">
        <xsl:value-of select="$timesecond - $timefirst"/>
      </xsl:variable>

      <samples>
        <xsl:for-each select="./divecomputer[1]/sample">
          <waypoint>
            <depth>
              <xsl:value-of select="substring-before(./@depth, ' ')"/>
            </depth>
            <divetime>
              <xsl:call-template name="time2sec">
                <xsl:with-param name="time">
                  <xsl:value-of select="./@time"/>
                </xsl:with-param>
              </xsl:call-template>
            </divetime>
            <xsl:if test="./@pressure != ''">
              <tankpressure>
                <xsl:value-of select="substring-before(./@pressure, ' ') * 100000"/>
              </tankpressure>
            </xsl:if>
            <xsl:if test="./@temp != ''">
              <temperature>
                <xsl:value-of select="format-number(substring-before(./@temp, ' ') + 273.15, '0.00')"/>
              </temperature>
            </xsl:if>

            <!-- We need to look up if there is an event at the time we
                 are handling currently. And then translate that event
                 to the one in UDDF specification.
            -->
            <xsl:variable name="time">
              <xsl:call-template name="time2sec">
                <xsl:with-param name="time">
                  <xsl:value-of select="./@time"/>
                </xsl:with-param>
              </xsl:call-template>
            </xsl:variable>

            <!-- Event is included in waypoint when:
                 sample_time - $delta < event_time <= sample_time

                 This should include all the events that occurred
                 between the samples. This also introduces inaccuracy in
                 the timestamp of events, but it is either that or a
                 crafted waypoint, with inaccurate depth.
                 -->
              <xsl:if test="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and @name='gaschange']/@name">

                <!-- Gas change is a reference to the gases section, as
                     the gases index was pure o2 value, we can directly
                     use Subsurfaces reference here.
                -->
                <xsl:for-each select="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and @name='gaschange']/@value">
                <switchmix>
                  <xsl:attribute name="ref">
                    <xsl:value-of select="."/>
                  </xsl:attribute>
                </switchmix>
              </xsl:for-each>
            </xsl:if>

              <xsl:if test="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and @name='heading']/@name">
                <xsl:for-each select="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and @name='heading']/@value">
                <heading>
                  <xsl:value-of select="."/>
                </heading>
              </xsl:for-each>
            </xsl:if>

              <!-- We'll just print the alarm text from our event name
                   as is, deco and surface are specified in UDDF
                   specification but the rest is not recognized and
                   there is no equivalent available.
              -->
              <xsl:if test="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and not(@name='heading' or @name='gaschange')]/@name">
                <xsl:for-each select="preceding-sibling::event[substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&lt;=$time and substring-before(@time, ':') * 60 + substring-before(substring-after(@time, ':'), ' ')&gt;($time - $delta) and not(@name='heading' or @name='gaschange')]/@name">
                  <alarm>
                    <xsl:value-of select="."/>
                  </alarm>
                </xsl:for-each>
              </xsl:if>
          </waypoint>
        </xsl:for-each>
      </samples>

      <tankdata>
        <xsl:if test="cylinder[1]/@size">
          <tankvolume>
            <xsl:value-of select="substring-before(cylinder[1]/@size, ' ')"/>
          </tankvolume>
        </xsl:if>
        <xsl:choose>
          <xsl:when test="divecomputer[1]/sample/@pressure != ''">
            <tankpressurebegin>
              <xsl:value-of select="substring-before(divecomputer[1]/sample/@pressure[1], ' ') * 100000"/>
            </tankpressurebegin>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="cylinder[1]/@start">
              <tankpressurebegin>
                <xsl:value-of select="substring-before(cylinder[1]/@start, ' ') * 100000"/>
              </tankpressurebegin>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>

        <xsl:choose>
          <xsl:when test="count(divecomputer[1]/sample[@pressure]) &gt; 0">
            <tankpressureend>
              <xsl:value-of select="substring-before(divecomputer[1]/sample[@pressure][last()]/@pressure, ' ') * 100000"/>
            </tankpressureend>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="cylinder[1]/@end">
              <tankpressureend>
                <xsl:value-of select="substring-before(cylinder[1]/@end, ' ') * 100000"/>
              </tankpressureend>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>

      </tankdata>

      <informationafterdive>
        <xsl:if test="node()/depth/@max != ''">
          <greatestdepth>
            <xsl:value-of select="substring-before(node()/depth/@max, ' ')"/>
          </greatestdepth>
        </xsl:if>
        <xsl:if test="node()/depth/@mean != ''">
          <averagedepth>
            <xsl:value-of select="substring-before(node()/depth/@mean, ' ')"/>
          </averagedepth>
        </xsl:if>
        <xsl:if test="./@duration != ''">
          <diveduration>
            <xsl:call-template name="time2sec">
              <xsl:with-param name="time">
                <xsl:value-of select="./@duration"/>
              </xsl:with-param>
            </xsl:call-template>
          </diveduration>
        </xsl:if>
        <xsl:if test="temperature/@water|divetemperature/@water != ''">
          <lowesttemperature>
            <xsl:value-of select="format-number(substring-before(temperature/@water|divetemperature/@water, ' ') + 273.15, '0.00')"/>
          </lowesttemperature>
        </xsl:if>
        <notes>
          <para>
            <xsl:value-of select="notes"/>
          </para>
        </notes>
        <rating>
          <ratingvalue>
            <xsl:choose>
              <xsl:when test="./@rating = 0">
                <xsl:value-of select="'1'"/>
              </xsl:when>
              <xsl:when test="./@rating != ''">
                <xsl:value-of select="./@rating * 2"/>
              </xsl:when>
            </xsl:choose>
          </ratingvalue>
        </rating>
        <visibility>
            <xsl:choose>
              <xsl:when test="./@visibility = 1">
                <xsl:value-of select="1"/>
              </xsl:when>
              <xsl:when test="./@visibility = 2">
                <xsl:value-of select="3"/>
              </xsl:when>
              <xsl:when test="./@visibility = 3">
                <xsl:value-of select="5"/>
              </xsl:when>
              <xsl:when test="./@visibility = 4">
                <xsl:value-of select="10"/>
              </xsl:when>
              <xsl:when test="./@visibility = 5">
                <xsl:value-of select="15"/>
              </xsl:when>
            </xsl:choose>
        </visibility>
      </informationafterdive>

    </dive>
  </xsl:template>
</xsl:stylesheet>