[PATCH] XSLT to export dive log in CSV

Miika Turkia miika.turkia at gmail.com
Sat Mar 1 15:17:32 PST 2014


This implements divelog export into CSV format. This currently supports
only metric units and lacks user interface.

See #434

Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
I am sending this out as I'll be off the Internet for the next week and
it seems that there is some interest to implement this feature. It is
just better not to waste time on the part I have already implemented but
to do the rest that is still missing.

For the GUI, I think that we should support exporting selected dives
(right click on the divelist and select export CSV from menu) and
exporting all dives (this is currently done from the file menu for other
exports). Of course, if we are to support multiple export formats, the
UI would really need some loving, not to flood the menus.
---
 xslt/xml2csv.xslt | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 xslt/xml2csv.xslt

diff --git a/xslt/xml2csv.xslt b/xslt/xml2csv.xslt
new file mode 100644
index 0000000..2a1a1df
--- /dev/null
+++ b/xslt/xml2csv.xslt
@@ -0,0 +1,51 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+  <xsl:strip-space elements="*"/>
+  <xsl:output method="text" encoding="UTF-8"/>
+
+  <xsl:variable name="fs">,</xsl:variable>
+
+  <xsl:template match="/divelog/dives">
+    <xsl:value-of select='concat("dive number", $fs, "dive date", $fs, "dive time", $fs, "time", $fs, "depth", $fs, "temperature", $fs, "pressure")'/>
+    <xsl:text>
+</xsl:text>
+    <xsl:apply-templates select="dive|trip/dive"/>
+  </xsl:template>
+
+  <xsl:template match="dive">
+    <xsl:variable name="number">
+      <xsl:value-of select="@number"/>
+    </xsl:variable>
+    <xsl:variable name="date">
+      <xsl:value-of select="@date"/>
+    </xsl:variable>
+    <xsl:variable name="time">
+      <xsl:value-of select="@time"/>
+    </xsl:variable>
+    <xsl:for-each select="divecomputer/sample|sample">
+      <xsl:value-of select="concat('"', $number, '"')"/>
+      <xsl:value-of select="$fs"/>
+      <xsl:value-of select="concat('"', $date, '"')"/>
+      <xsl:value-of select="$fs"/>
+      <xsl:value-of select="concat('"', $time, '"')"/>
+      <xsl:value-of select="$fs"/>
+      <xsl:value-of select="concat('"', substring-before(@time, ' '), '"')"/>
+      <xsl:value-of select="$fs"/>
+      <xsl:value-of select="concat('"', substring-before(@depth, ' '), '"')"/>
+
+      <xsl:value-of select="$fs"/>
+      <xsl:if test="@temp != ''">
+        <xsl:value-of select="concat('"', substring-before(@temp, ' '), '"')"/>
+      </xsl:if>
+
+      <xsl:value-of select="$fs"/>
+      <xsl:if test="@pressure != ''">
+        <xsl:value-of select="concat('"', substring-before(@pressure, ' '), '"')"/>
+      </xsl:if>
+
+      <xsl:text>
+</xsl:text>
+    </xsl:for-each>
+  </xsl:template>
+
+</xsl:stylesheet>  
-- 
1.8.3.2



More information about the subsurface mailing list