]> git.tdb.fi Git - ext/subsurface.git/blob - xslt/jdivelog2subsurface.xslt
Add weight and suit support for JDiveLog import
[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       <xsl:if test="Equipment/Suit != ''">
68         <suit>
69           <xsl:value-of select="Equipment/Suit"/>
70         </suit>
71         <xsl:value-of select="Equipment/Suit"/>
72       </xsl:if>
73
74       <xsl:if test="Equipment/Weight != ''">
75         <weightsystem>
76           <xsl:attribute name="weight">
77             <xsl:value-of select="Equipment/Weight"/>
78           </xsl:attribute>
79         </weightsystem>
80       </xsl:if>
81
82       <notes>
83         <xsl:if test="DiveActivity != ''">
84 Diveactivity: <xsl:value-of select="DiveActivity"/>
85         </xsl:if>
86         <xsl:if test="DiveType != ''">
87 Divetype: <xsl:value-of select="DiveType"/>
88         </xsl:if>
89         <xsl:if test="Equipment/Visibility != ''">
90 Visibility: <xsl:value-of select="Equipment/Visibility"/>
91         </xsl:if>
92         <xsl:if test="Equipment/Gloves != ''">
93 Gloves: <xsl:value-of select="Equipment/Gloves"/>
94         </xsl:if>
95         <xsl:if test="Comment != ''">
96 Comment: <xsl:value-of select="Comment"/>
97         </xsl:if>
98       </notes>
99
100       <!-- cylinder -->
101       <xsl:for-each select="Equipment/Tanks/Tank">
102         <cylinder>
103           <xsl:attribute name="o2">
104             <xsl:choose>
105               <xsl:when test="MIX/O2 != ''">
106                 <xsl:value-of select="concat(MIX/O2*100, '%')"/>
107               </xsl:when>
108               <xsl:otherwise>21.0%</xsl:otherwise>
109             </xsl:choose>
110           </xsl:attribute>
111           <xsl:if test="MIX/HE != '0.0'">
112             <xsl:attribute name="he">
113               <xsl:value-of select="concat(MIX/HE*100, '%')"/>
114             </xsl:attribute>
115           </xsl:if>
116           <xsl:attribute name="size">
117             <xsl:choose>
118               <xsl:when test="MIX/TANK/TANKVOLUME != ''">
119                 <xsl:value-of select="concat(MIX/TANK/TANKVOLUME * 1000, ' l')"/>
120               </xsl:when>
121               <xsl:otherwise>0 l</xsl:otherwise>
122             </xsl:choose>
123           </xsl:attribute>
124           <xsl:attribute name="start">
125             <xsl:variable name="number" select="MIX/TANK/PSTART"/>
126             <xsl:call-template name="pressure">
127               <xsl:with-param name="number" select="$number"/>
128               <xsl:with-param name="units" select="$units"/>
129             </xsl:call-template>
130           </xsl:attribute>
131           <xsl:attribute name="end">
132             <xsl:variable name="number" select="MIX/TANK/PEND"/>
133             <xsl:call-template name="pressure">
134               <xsl:with-param name="number" select="$number"/>
135               <xsl:with-param name="units" select="$units"/>
136             </xsl:call-template>
137           </xsl:attribute>
138         </cylinder>
139       </xsl:for-each>
140       <!-- end cylinder -->
141
142       <!-- DELTA is the sample interval -->
143       <xsl:variable name="delta">
144         <xsl:choose>
145           <xsl:when test="DIVE/SAMPLES/DELTA != ''">
146             <xsl:choose>
147               <xsl:when test="$units = 'si'">
148                 <xsl:value-of select="DIVE/SAMPLES/DELTA"/>
149               </xsl:when>
150               <xsl:otherwise>
151                 <xsl:value-of select="round(60 * DIVE/SAMPLES/DELTA)"/>
152               </xsl:otherwise>
153             </xsl:choose>
154           </xsl:when>
155           <xsl:otherwise>0</xsl:otherwise>
156         </xsl:choose>
157       </xsl:variable>
158       <!-- end delta -->
159
160       <!-- events -->
161       <xsl:for-each select="DIVE/SAMPLES/ALARM">
162         <xsl:if test=". != 'SURFACE'">
163           <event type="" name="{.}">
164             <xsl:attribute name="time">
165               <xsl:call-template name="timeConvert">
166                 <xsl:with-param name="timeSec" select="count(preceding-sibling::D) * $delta"/>
167                 <xsl:with-param name="units" select="'si'"/>
168               </xsl:call-template>
169             </xsl:attribute>
170           </event>
171         </xsl:if>
172       </xsl:for-each>
173       <!-- end events -->
174
175       <!-- gas change -->
176       <xsl:for-each select="DIVE/SAMPLES/SWITCH">
177         <event name="gaschange">
178           <xsl:attribute name="time">
179             <xsl:call-template name="timeConvert">
180               <xsl:with-param name="timeSec" select="count(preceding-sibling::D) * $delta"/>
181               <xsl:with-param name="units" select="'si'"/>
182             </xsl:call-template>
183           </xsl:attribute>
184           <xsl:attribute name="value">
185             <xsl:value-of select="ancestor::DIVE/GASES/MIX[MIXNAME=current()]/O2 * 100" />
186           </xsl:attribute>
187         </event>
188       </xsl:for-each>
189       <!-- end gas change -->
190
191       <!-- dive sample - all the depth and temp readings -->
192       <xsl:for-each select="DIVE/SAMPLES/D">
193         <xsl:variable name="timeSec" select="(position() - 1) * $delta"/>
194         <xsl:variable name="time" select="concat(floor($timeSec div 60), ':',
195           format-number(floor($timeSec mod 60), '00'), ' min')"/>
196         <xsl:choose>
197           <xsl:when test="name(following-sibling::*[1]) = 'TEMPERATURE'">
198             <sample time="{$time}" depth="{concat(., ' m')}"
199               temp="{following-sibling::TEMPERATURE}"/>
200           </xsl:when>
201           <xsl:otherwise>
202             <sample time="{$time}" depth="{concat(., ' m')}"/>
203           </xsl:otherwise>
204         </xsl:choose>
205       </xsl:for-each>
206       <!-- dive sample -->
207
208     </dive>
209   </xsl:template>
210   <!-- end JDive -->
211
212   <!-- convert pressure to bars -->
213   <xsl:template name="pressure">
214     <xsl:param name="number"/>
215     <xsl:param name="units"/>
216
217     <xsl:variable name="pressure">
218       <xsl:choose>
219         <xsl:when test="$number != ''">
220           <xsl:variable name="Exp" select="substring-after($number, 'E')"/>
221           <xsl:variable name="Man" select="substring-before($number, 'E')"/>
222           <xsl:variable name="Fac" select="substring('100000000000000000000', 1, $Exp + 1)"/>
223           <xsl:choose>
224             <xsl:when test="$Exp != ''">
225               <xsl:value-of select="(number($Man) * number($Fac))"/>
226             </xsl:when>
227             <xsl:otherwise>
228               <xsl:value-of select="$number"/>
229             </xsl:otherwise>
230           </xsl:choose>
231         </xsl:when>
232         <xsl:otherwise>0</xsl:otherwise>
233       </xsl:choose>
234     </xsl:variable>
235
236     <xsl:choose>
237       <xsl:when test="$units = 'si'">
238         <xsl:value-of select="concat(($pressure div 100000), ' bar')"/>
239       </xsl:when>
240       <xsl:otherwise>
241         <xsl:value-of select="concat($pressure, ' bar')"/>
242       </xsl:otherwise>
243     </xsl:choose>
244
245   </xsl:template>
246   <!-- end convert pressure -->
247
248   <!-- convert time in seconds to minutes:seconds -->
249   <xsl:template name="timeConvert">
250     <xsl:param name="timeSec"/>
251     <xsl:param name="units"/>
252
253     <xsl:choose>
254       <xsl:when test="$units = 'si'">
255         <xsl:value-of select="concat(floor(number($timeSec) div 60), ':',    format-number(floor(number($timeSec) mod 60), '00'), ' min')"/>
256       </xsl:when>
257       <xsl:otherwise>
258         <xsl:value-of select="concat(substring-before($timeSec, '.'), ':',           format-number(substring-after($timeSec, '.'), '00'), ' min')"/>
259       </xsl:otherwise>
260     </xsl:choose>
261   </xsl:template>
262   <!-- end convert time -->
263
264 </xsl:stylesheet>