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