]> git.tdb.fi Git - ext/subsurface.git/blob - xslt/jdivelog2subsurface.xslt
Added support for Mares Darwin, M1, M2, ... from latest libdivecomputer.
[ext/subsurface.git] / xslt / jdivelog2subsurface.xslt
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3   <xsl:strip-space elements="*"/>
4   <xsl:output method="xml" indent="yes"/>
5
6   <xsl:template match="/">
7     <dives>
8       <program name="subsurface" version="1"/>
9       <xsl:apply-templates select="/JDiveLog/JDive"/>
10     </dives>
11   </xsl:template>
12
13   <xsl:template match="JDive">
14     <xsl:variable name="units" select="UNITS"/>
15
16     <dive number="{DiveNum}">
17       <xsl:attribute name="date">
18         <xsl:value-of select="concat(DATE/YEAR,'-',format-number(DATE/MONTH,
19            '00'), '-', format-number(DATE/DAY, '00'))"/>
20       </xsl:attribute>
21
22       <xsl:attribute name="time">
23         <xsl:value-of select="concat(format-number(TIME/HOUR, '00'), ':',
24            format-number(TIME/MINUTE, '00'))"/>
25       </xsl:attribute>
26
27       <xsl:attribute name="duration">
28         <xsl:call-template name="timeConvert">
29           <xsl:with-param name="timeSec" select="Duration"/>
30           <xsl:with-param name="units" select="$units"/>
31         </xsl:call-template>
32       </xsl:attribute>
33
34       <xsl:choose>
35         <xsl:when test="Average_Depth != ''">
36           <depth max="{concat(Depth,' m')}" mean="{concat(Average_Depth, ' m')}"/>
37         </xsl:when>
38         <xsl:otherwise>
39           <depth max="{concat(Depth,' m')}"/>
40         </xsl:otherwise>
41       </xsl:choose>
42
43       <xsl:if test="TEMPERATURE != ''">
44         <xsl:choose>
45           <xsl:when test="$units = 'si'">
46             <temperature water="{concat(format-number(TEMPERATURE - 273.15, '00.0'), ' C')}"/>
47           </xsl:when>
48           <xsl:otherwise>
49             <temperature water="{concat(TEMPERATURE, ' C')}"/>
50           </xsl:otherwise>
51         </xsl:choose>
52       </xsl:if>
53
54       <xsl:if test="diveSiteId != ''">
55         <xsl:variable name="diveSite" select="diveSiteId"/>
56         <location>
57           <xsl:value-of select="concat(/JDiveLog/Masterdata/DiveSites/DiveSite[privateId=$diveSite]/country, ' ', /JDiveLog/Masterdata/DiveSites/DiveSite[privateId=$diveSite]/state, ' ', /JDiveLog/Masterdata/DiveSites/DiveSite[privateId=$diveSite]/city, ' ', /JDiveLog/Masterdata/DiveSites/DiveSite[privateId=$diveSite]/spot)"/>
58         </location>
59       </xsl:if>
60
61       <xsl:if test="Buddy">
62         <buddy>
63           <xsl:value-of select="Buddy"/>
64         </buddy>
65       </xsl:if>
66
67       <notes>
68         <xsl:if test="DiveActivity != ''">
69 Diveactivity: <xsl:value-of select="DiveActivity"/>
70         </xsl:if>
71         <xsl:if test="DiveType != ''">
72 Divetype: <xsl:value-of select="DiveType"/>
73         </xsl:if>
74         <xsl:if test="Equipment/Visibility != ''">
75 Visibility: <xsl:value-of select="Equipment/Visibility"/>
76         </xsl:if>
77         <xsl:if test="Equipment/Suit != ''">
78 Suit: <xsl:value-of select="Equipment/Suit"/>
79         </xsl:if>
80         <xsl:if test="Equipment/Gloves != ''">
81 Gloves: <xsl:value-of select="Equipment/Gloves"/>
82         </xsl:if>
83         <xsl:if test="Equipment/Weight != ''">
84 Weight: <xsl:value-of select="Equipment/Weight"/>
85         </xsl:if>
86         <xsl:if test="Comment != ''">
87 Comment: <xsl:value-of select="Comment"/>
88         </xsl:if>
89       </notes>
90
91       <!-- cylinder -->
92       <xsl:for-each select="Equipment/Tanks/Tank">
93         <cylinder>
94           <xsl:attribute name="o2">
95             <xsl:choose>
96               <xsl:when test="MIX/O2 != ''">
97                 <xsl:value-of select="concat(MIX/O2*100, '%')"/>
98               </xsl:when>
99               <xsl:otherwise>21.0%</xsl:otherwise>
100             </xsl:choose>
101           </xsl:attribute>
102           <xsl:if test="MIX/HE != '0.0'">
103             <xsl:attribute name="he">
104               <xsl:value-of select="concat(MIX/HE*100, '%')"/>
105             </xsl:attribute>
106           </xsl:if>
107           <xsl:attribute name="size">
108             <xsl:choose>
109               <xsl:when test="MIX/TANK/TANKVOLUME != ''">
110                 <xsl:value-of select="concat(MIX/TANK/TANKVOLUME * 1000, ' l')"/>
111               </xsl:when>
112               <xsl:otherwise>0 l</xsl:otherwise>
113             </xsl:choose>
114           </xsl:attribute>
115           <xsl:attribute name="start">
116             <xsl:variable name="number" select="MIX/TANK/PSTART"/>
117             <xsl:call-template name="pressure">
118               <xsl:with-param name="number" select="$number"/>
119               <xsl:with-param name="units" select="$units"/>
120             </xsl:call-template>
121           </xsl:attribute>
122           <xsl:attribute name="end">
123             <xsl:variable name="number" select="MIX/TANK/PEND"/>
124             <xsl:call-template name="pressure">
125               <xsl:with-param name="number" select="$number"/>
126               <xsl:with-param name="units" select="$units"/>
127             </xsl:call-template>
128           </xsl:attribute>
129         </cylinder>
130       </xsl:for-each>
131       <!-- end cylinder -->
132
133       <!-- DELTA is the sample interval -->
134       <xsl:variable name="delta">
135         <xsl:choose>
136           <xsl:when test="DIVE/SAMPLES/DELTA != ''">
137             <xsl:choose>
138               <xsl:when test="$units = 'si'">
139                 <xsl:value-of select="DIVE/SAMPLES/DELTA"/>
140               </xsl:when>
141               <xsl:otherwise>
142                 <xsl:value-of select="round(60 * DIVE/SAMPLES/DELTA)"/>
143               </xsl:otherwise>
144             </xsl:choose>
145           </xsl:when>
146           <xsl:otherwise>0</xsl:otherwise>
147         </xsl:choose>
148       </xsl:variable>
149       <!-- end delta -->
150
151       <!-- events -->
152       <xsl:for-each select="DIVE/SAMPLES/ALARM">
153         <xsl:if test=". != 'SURFACE'">
154           <event type="" name="{.}">
155             <xsl:attribute name="time">
156               <xsl:call-template name="timeConvert">
157                 <xsl:with-param name="timeSec" select="count(preceding-sibling::D) * $delta"/>
158                 <xsl:with-param name="units" select="'si'"/>
159               </xsl:call-template>
160             </xsl:attribute>
161           </event>
162         </xsl:if>
163       </xsl:for-each>
164       <!-- end events -->
165
166       <!-- gas change -->
167       <xsl:for-each select="DIVE/SAMPLES/SWITCH">
168         <event name="gaschange">
169           <xsl:attribute name="time">
170             <xsl:call-template name="timeConvert">
171               <xsl:with-param name="timeSec" select="count(preceding-sibling::D) * $delta"/>
172               <xsl:with-param name="units" select="'si'"/>
173             </xsl:call-template>
174           </xsl:attribute>
175           <xsl:attribute name="value">
176             <xsl:value-of select="ancestor::DIVE/GASES/MIX[MIXNAME=current()]/O2 * 100" />
177           </xsl:attribute>
178         </event>
179       </xsl:for-each>
180       <!-- end gas change -->
181
182       <!-- dive sample - all the depth and temp readings -->
183       <xsl:for-each select="DIVE/SAMPLES/D">
184         <xsl:variable name="timeSec" select="(position() - 1) * $delta"/>
185         <xsl:variable name="time" select="concat(floor($timeSec div 60), ':',
186           format-number(floor($timeSec mod 60), '00'), ' min')"/>
187         <xsl:choose>
188           <xsl:when test="name(following-sibling::*[1]) = 'TEMPERATURE'">
189             <sample time="{$time}" depth="{concat(., ' m')}"
190               temp="{following-sibling::TEMPERATURE}"/>
191           </xsl:when>
192           <xsl:otherwise>
193             <sample time="{$time}" depth="{concat(., ' m')}"/>
194           </xsl:otherwise>
195         </xsl:choose>
196       </xsl:for-each>
197       <!-- dive sample -->
198
199     </dive>
200   </xsl:template>
201   <!-- end JDive -->
202
203   <!-- convert pressure to bars -->
204   <xsl:template name="pressure">
205     <xsl:param name="number"/>
206     <xsl:param name="units"/>
207
208     <xsl:variable name="pressure">
209       <xsl:choose>
210         <xsl:when test="$number != ''">
211           <xsl:variable name="Exp" select="substring-after($number, 'E')"/>
212           <xsl:variable name="Man" select="substring-before($number, 'E')"/>
213           <xsl:variable name="Fac" select="substring('100000000000000000000', 1, $Exp + 1)"/>
214           <xsl:choose>
215             <xsl:when test="$Exp != ''">
216               <xsl:value-of select="(number($Man) * number($Fac))"/>
217             </xsl:when>
218             <xsl:otherwise>
219               <xsl:value-of select="$number"/>
220             </xsl:otherwise>
221           </xsl:choose>
222         </xsl:when>
223         <xsl:otherwise>0</xsl:otherwise>
224       </xsl:choose>
225     </xsl:variable>
226
227     <xsl:choose>
228       <xsl:when test="$units = 'si'">
229         <xsl:value-of select="concat(($pressure div 100000), ' bar')"/>
230       </xsl:when>
231       <xsl:otherwise>
232         <xsl:value-of select="concat($pressure, ' bar')"/>
233       </xsl:otherwise>
234     </xsl:choose>
235
236   </xsl:template>
237   <!-- end convert pressure -->
238
239   <!-- convert time in seconds to minutes:seconds -->
240   <xsl:template name="timeConvert">
241     <xsl:param name="timeSec"/>
242     <xsl:param name="units"/>
243
244     <xsl:choose>
245       <xsl:when test="$units = 'si'">
246         <xsl:value-of select="concat(floor(number($timeSec) div 60), ':',    format-number(floor(number($timeSec) mod 60), '00'), ' min')"/>
247       </xsl:when>
248       <xsl:otherwise>
249         <xsl:value-of select="concat(substring-before($timeSec, '.'), ':',           format-number(substring-after($timeSec, '.'), '00'), ' min')"/>
250       </xsl:otherwise>
251     </xsl:choose>
252   </xsl:template>
253   <!-- end convert time -->
254
255 </xsl:stylesheet>