XSLT for GBIFResponse to CSV format.
authorCherian Mathew <c.mathew@bgbm.org>
Thu, 1 Mar 2012 15:10:45 +0000 (15:10 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Thu, 1 Mar 2012 15:10:45 +0000 (15:10 +0000)
To be used to generate input test cases for the Taxonomy Data Cleaning Workflow (based on https://wiki.biovel.eu/display/BioVeL/Data+Cleaning+Workflow+Elements ). Also used in the Data Cleaning workflow to convert GBIF response to csv format.

Includes handling of commas within strings (by wrapping strings in double quotes) and escaping double quotes with double quotes.

.gitattributes
DataCleaning/xml/gbifResponse_to_csv.xsl [new file with mode: 0755]

index befaaedcb314beee35aea36ca6b2f1e7981e64b0..dc04c85285aae008e4b5d01f05cd62be09146307 100644 (file)
@@ -1,3 +1,4 @@
 * text=auto !eol
 DataCleaning/README.txt -text
+DataCleaning/xml/gbifResponse_to_csv.xsl -text svneol=unset#application/xml
 /README.txt -text
diff --git a/DataCleaning/xml/gbifResponse_to_csv.xsl b/DataCleaning/xml/gbifResponse_to_csv.xsl
new file mode 100755 (executable)
index 0000000..7b0923f
--- /dev/null
@@ -0,0 +1,222 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<!--\r
+     XSLT for GBIFResponse to CSV format\r
+\r
+     Maps GBIFResponse elements to CSV header elements,\r
+     TaxonConcept : http://rs.tdwg.org/ontology/voc/TaxonConcept\r
+     TaxonOccurrence : http://rs.tdwg.org/ontology/voc/TaxonOccurrence\r
+     TaxonName : http://rs.tdwg.org/ontology/voc/TaxonName \r
+\r
+     To be used to generate input test cases for the Taxonomy Data Cleaning Workflow \r
+     -->\r
+\r
+<xsl:stylesheet version="1.0"    \r
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" \r
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \r
+  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \r
+  xmlns:tax="http://rs.tdwg.org/ontology/voc/TaxonOccurrence#" \r
+  xmlns:tax1="http://rs.tdwg.org/ontology/voc/TaxonConcept#" \r
+  xmlns:tax2="http://rs.tdwg.org/ontology/voc/TaxonName#"\r
+  xmlns:gbif="http://portal.gbif.org/ws/response/gbif">\r
+  <xsl:output method="text"/>\r
+\r
+  \r
+  <xsl:variable name="newline">\r
+    <xsl:text>&#xa;</xsl:text>\r
+  </xsl:variable>\r
+\r
+  <xsl:variable name="singlespace">\r
+    <xsl:text> </xsl:text>\r
+  </xsl:variable>\r
+\r
+  <xsl:template name="string-replace-all">\r
+  <xsl:param name="text"/>\r
+  <xsl:param name="replace"/>\r
+  <xsl:param name="by"/>\r
+  <xsl:choose>\r
+    <xsl:when test="contains($text,$replace)">\r
+      <xsl:value-of select="substring-before($text,$replace)"/>\r
+      <xsl:value-of select="$by"/>\r
+      <xsl:call-template name="string-replace-all">\r
+        <xsl:with-param name="text" select="substring-after($text,$replace)"/>\r
+        <xsl:with-param name="replace" select="$replace"/>\r
+        <xsl:with-param name="by" select="$by"/>\r
+      </xsl:call-template>\r
+    </xsl:when>\r
+    <xsl:otherwise>\r
+      <xsl:value-of select="$text"/>\r
+    </xsl:otherwise>\r
+  </xsl:choose>\r
+</xsl:template>\r
+\r
+  <xsl:template match="/"> \r
+  <xsl:apply-templates select="//tax:TaxonOccurrence"/>        \r
+</xsl:template>\r
+\r
+<xsl:template match="tax:TaxonOccurrence">    \r
+\r
+<xsl:text>"</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="./tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:TaxonName/tax2:authorship/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:genusPart/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+    \r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:infragenericEpithet/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:specificEpithet/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:infraspecificEpithet/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="./tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:nameComplete/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/tax1:hasName/tax2:TaxonName/tax2:uninomial/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxonName/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:identifiedTo/tax:Identification/tax:taxon/tax1:TaxonConcept/@gbifKey/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>,</xsl:text>\r
+<xsl:value-of select="tax:decimalLatitude/text()"/>\r
+\r
+<xsl:text>,</xsl:text>\r
+<xsl:value-of select="tax:decimalLongitude/text()"/>\r
+\r
+<xsl:text>,"</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:earliestDateCollected/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:latestDateCollected/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>",</xsl:text>\r
+<xsl:value-of select="tax:coordinateUncertaintyInMeters/text()"/>\r
+\r
+<xsl:text>,"</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:country/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:collector/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:fieldNotes/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:locality/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>",</xsl:text>\r
+<xsl:value-of select="tax:maximumDepthInMeters/text()"/>\r
+\r
+<xsl:text>,</xsl:text>\r
+<xsl:value-of select="tax:maximumElevationInMeters/text()"/>\r
+\r
+<xsl:text>,</xsl:text>\r
+<xsl:value-of select="tax:minimumDepthInMeters/text()"/>\r
+\r
+<xsl:text>,</xsl:text>\r
+<xsl:value-of select="tax:minimumElevationInMeters/text()"/>\r
+\r
+<xsl:text>,"</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="tax:value/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="../../../../gbif:name/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="../../gbif:name/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="../../gbif:rights/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:text>","</xsl:text>\r
+<xsl:call-template name="string-replace-all">\r
+  <xsl:with-param name="text" select="../../gbif:citation/text()"/>\r
+  <xsl:with-param name="replace" select="'&quot;'"/>\r
+  <xsl:with-param name="by" select="'&quot;&quot;'"/>\r
+</xsl:call-template>\r
+\r
+<xsl:value-of select="$newline"/>   \r
+</xsl:template>\r
+\r
+</xsl:stylesheet>\r