<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xt="http://www.jclark.com/xt"
extension-element-prefixes="xt" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:param name="units" select="units"/>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="fs"><xsl:text> </xsl:text></xsl:variable>
<xsl:variable name="lf"><xsl:text>
</xsl:text></xsl:variable>
<xsl:template match="/divelog/dives">
<xsl:variable name="cylinders">
<xsl:value-of select="count(dive[position()=last()]/cylinder|trip[position()=last()]/dive[position()=last()]/cylinder)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$units = 1">
<!-- Print beginning of the header -->
<xsl:value-of select="concat(
'"dive number"',
$fs,
'"date"',
$fs,
'"time"',
$fs,
'"duration (min)"',
$fs,
'"sac (cuft/min)"',
$fs,
'"maxdepth (ft)"',
$fs,
'"avgdepth (ft)"',
$fs,
'"mode"',
$fs,
'"airtemp (F)"',
$fs,
'"watertemp (F)"',
$fs)"/>
<!-- Print cylinder info according to the amount of cylinders in dive -->
<xsl:for-each select="dive[position()=last()]/cylinder|trip[position()=last()]/dive[position()=last()]/cylinder">
<xsl:value-of select="concat(
'"cylinder size (cuft)"',
$fs,
'"startpressure (psi)"',
$fs,
'"endpressure (psi)"',
$fs,
'"o2 (%)"',
$fs,
'"he (%)"',
$fs)"/>
</xsl:for-each>
<!-- Print rest of the header -->
<xsl:value-of select="concat(
'"location"',
$fs,
'"gps"',
$fs,
'"divemaster"',
$fs,
'"buddy"',
$fs,
'"suit"',
$fs,
'"rating"',
$fs,
'"visibility"',
$fs,
'"notes"',
$fs,
'"weight (lbs)"',
$fs,
'"tags"')"/>
</xsl:when>
<xsl:otherwise>
<!-- Print beginning of the header -->
<xsl:value-of select="concat(
'"dive number"',
$fs,
'"date"',
$fs,
'"time"',
$fs,
'"duration (min)"',
$fs,
'"sac (l/min)"',
$fs,
'"maxdepth (m)"',
$fs,
'"avgdepth (m)"',
$fs,
'"mode"',
$fs,
'"airtemp (C)"',
$fs,
'"watertemp (C)"',
$fs)"/>
<!-- Print cylinder info according to the amount of cylinders in dive -->
<xsl:for-each select="dive[position()=last()]/cylinder|trip[position()=last()]/dive[position()=last()]/cylinder">
<xsl:value-of select="concat(
'"cylinder size (l)"',
$fs,
'"startpressure (bar)"',
$fs,
'"endpressure (bar)"',
$fs,
'"o2 (%)"',
$fs,
'"he (%)"',
$fs)"/>
</xsl:for-each>
<!-- Print rest of the header -->
<xsl:value-of select="concat(
'"location"',
$fs,
'"gps"',
$fs,
'"divemaster"',
$fs,
'"buddy"',
$fs,
'"suit"',
$fs,
'"rating"',
$fs,
'"visibility"',
$fs,
'"notes"',
$fs,
'"weight (kg)"',
$fs,
'"tags"')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="dive|trip/dive">
<xsl:with-param name="cylinders" select="$cylinders"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="divesites/site/notes"/>
<xsl:template match="dive">
<xsl:param name="cylinders"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@number"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@date"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@time"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(@duration, ' ')"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="format-number(substring-before(@sac, ' ') * 0.035315, '#.##')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@sac, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="divecomputer[1]/depth/@mean|divecomputer[1]/depth/@max != ''">
<xsl:apply-templates select="divecomputer[1]/depth"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:otherwise>
</xsl:choose>
<!-- Dive mode -->
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:if test="divecomputer[1]/@dctype != ''">
<xsl:value-of select="divecomputer[1]/@dctype"/>
</xsl:if>
<xsl:text>"</xsl:text>
<!-- Air temperature -->
<xsl:choose>
<xsl:when test="divetemperature/@air != ''">
<xsl:call-template name="temperature">
<xsl:with-param name="temp" select="divetemperature/@air"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="divecomputer[1]/temperature/@air != ''">
<xsl:call-template name="temperature">
<xsl:with-param name="temp" select="divecomputer[1]/temperature/@air"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- empty air temperature -->
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:otherwise>
</xsl:choose>
<!-- Water temperature -->
<xsl:choose>
<xsl:when test="divetemperature/@water != ''">
<xsl:call-template name="temperature">
<xsl:with-param name="temp" select="divetemperature/@water"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="divecomputer[1]/temperature/@water != ''">
<xsl:call-template name="temperature">
<xsl:with-param name="temp" select="divecomputer[1]/temperature/@water"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- water temperature -->
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="cylinder">
<xsl:choose>
<xsl:when test="$cylinders < position()"/>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="@start|@end != ''">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@size, ' ') div 14.7 * 3000) * 0.035315, '#.#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@size, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:choose>
<xsl:when test="substring-before(../divecomputer[1]/sample[@pressure]/@pressure, ' ') > 0">
<xsl:value-of select="concat(format-number((substring-before(../divecomputer[1]/sample[@pressure]/@pressure, ' ') * 14.5037738007), '#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(../divecomputer[1]/sample[@pressure]/@pressure, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:choose>
<xsl:when test="substring-before(../divecomputer[1]/sample[@pressure][last()]/@pressure, ' ') > 0">
<xsl:value-of select="concat(format-number((substring-before(../divecomputer[1]/sample[@pressure][last()]/@pressure, ' ') * 14.5037738007), '#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(../divecomputer[1]/sample[@pressure][last()]/@pressure, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(@o2, '%')"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(@he, '%')"/>
<xsl:text>"</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:if test="count(cylinder) < $cylinders">
<xsl:call-template name="emptyCylinder">
<xsl:with-param name="count" select="$cylinders - count(cylinder)"/>
</xsl:call-template>
</xsl:if>
<xsl:choose>
<!-- Old location format -->
<xsl:when test="location != ''">
<xsl:apply-templates select="location"/>
<xsl:if test="string-length(location) = 0">
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:if>
</xsl:when>
<!-- Format with dive site managemenet -->
<xsl:otherwise>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:if test="string-length(@divesiteid) > 0">
<xsl:variable name="uuid">
<xsl:value-of select="@divesiteid" />
</xsl:variable>
<xsl:value-of select="//divesites/site[@uuid = $uuid]/@name"/>
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:if test="string-length(@divesiteid) > 0">
<xsl:variable name="uuid">
<xsl:value-of select="@divesiteid" />
</xsl:variable>
<xsl:value-of select="//divesites/site[@uuid = $uuid]/@gps"/>
</xsl:if>
<xsl:text>"</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="divemaster"/>
<xsl:if test="string-length(divemaster) = 0">
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:if>
<xsl:apply-templates select="buddy"/>
<xsl:if test="string-length(buddy) = 0">
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:if>
<xsl:apply-templates select="suit"/>
<xsl:if test="string-length(suit) = 0">
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:if>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@rating"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@visibility"/>
<xsl:text>"</xsl:text>
<xsl:apply-templates select="notes"/>
<xsl:if test="string-length(notes) = 0">
<xsl:value-of select="$fs"/>
<xsl:text>""</xsl:text>
</xsl:if>
<xsl:variable name="trimmedweightlist">
<xsl:for-each select="weightsystem">
<weight>
<xsl:value-of select="substring-before(@weight, ' ')"/>
</weight>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:if test="weightsystem">
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((sum(xt:node-set($trimmedweightlist)/node()) div 0.453592), '#.##'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(sum(xt:node-set($trimmedweightlist)/node()), '')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before(translate(translate(@tags, $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="remaining" select="substring-after(translate(translate(@tags, $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="all" select="@tags"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="divecomputer/depth">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@max, ' ') div 0.3048), '#.##'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@max, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@mean, ' ') div 0.3048), '#.##'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(substring-before(@mean, ' '), '#.##')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Temperature template -->
<xsl:template name="temperature">
<xsl:param name="temp"/>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:if test="substring-before($temp, ' ') > 0">
<xsl:value-of select="concat(format-number((substring-before($temp, ' ') * 1.8) + 32, '0.0'), '')"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($temp, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="cylinder">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@size, ' ') div 14.7 * 3000) * 0.035315, '#.#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@size, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@start, ' ') * 14.5037738007), '#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@start, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$units = 1">
<xsl:value-of select="concat(format-number((substring-before(@end, ' ') * 14.5037738007), '#'), '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(@end, ' ')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(@o2, '%')"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(@he, '%')"/>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="location">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="@gps"/>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="divemaster">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="remaining" select="substring-after(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="all" select="translate(., $fs, ' ')"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="buddy">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="remaining" select="substring-after(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="all" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="suit">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="remaining" select="substring-after(translate(translate(., $fs, ' '), $lf, ' '), '"')"/>
<xsl:with-param name="all" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="notes">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before(translate(translate(., $fs, ' '), $lf, '\n'), '"')"/>
<xsl:with-param name="remaining" select="substring-after(translate(translate(., $fs, ' '), $lf, '\n'), '"')"/>
<xsl:with-param name="all" select="translate(translate(., $fs, ' '), $lf, '\n')"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template name="quote">
<xsl:param name="line"/>
<xsl:param name="remaining"/>
<xsl:param name="all"/>
<xsl:choose>
<xsl:when test="$line = ''">
<xsl:value-of select="$all"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($line, '"', '"')"/>
<xsl:if test="$remaining != ''">
<xsl:choose>
<xsl:when test="substring-before($remaining, '"') != ''">
<xsl:call-template name="quote">
<xsl:with-param name="line" select="substring-before($remaining, '"')"/>
<xsl:with-param name="remaining" select="substring-after($remaining, '"')"/>
<xsl:with-param name="all" select="$remaining"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$remaining" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Template to fill in non-existent cylinder info to CSV -->
<xsl:template name="emptyCylinder">
<xsl:param name="count"/>
<xsl:if test="$count > 0">
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="$fs"/>
<xsl:text>"</xsl:text>
<xsl:text>"</xsl:text>
<xsl:call-template name="emptyCylinder">
<xsl:with-param name="count" select="$count - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>