Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created July 13, 2021 09:54
Show Gist options
  • Save sotolf2/08fd374f226928c62f8ca1cd5d846d07 to your computer and use it in GitHub Desktop.
Save sotolf2/08fd374f226928c62f8ca1cd5d846d07 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
extension-element-prefixes="exsl" >
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="UTF-8"/>
<!-- variable declarations -->
<xsl:variable name="LF" select="'&#xA;'"/>
<xsl:variable name="SEP" select="';'"/>
<xsl:variable name="whitespace" select="'&#09;&#10;&#13; '" />
<!-- postprocessing -->
<xsl:template match="/">
<xsl:variable name="content">
<xsl:apply-imports/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($content)" mode="columnMapping"/>
</xsl:template>
<!--postprocessing: Mapping for all CSV columns -->
<xsl:template match="column29" mode="columnMapping">
<zeilenNummer><xsl:value-of select="count(../preceding-sibling::*)+1"/></zeilenNummer>
<mastBetriebsBezeichnung><xsl:value-of select="text()"/></mastBetriebsBezeichnung>
</xsl:template>
<xsl:template match="column30" mode="columnMapping">
<mastStrasse><xsl:value-of select="text()"/></mastStrasse>
</xsl:template>
<xsl:template match="column31" mode="columnMapping">
<mastPlz><xsl:value-of select="text()"/></mastPlz>
</xsl:template>
<xsl:template match="column32" mode="columnMapping">
<mastOrt><xsl:value-of select="text()"/></mastOrt>
</xsl:template>
<xsl:template match="column2" mode="columnMapping">
<schlachtTag><xsl:value-of select="concat(substring(text(), 7, 4),'-', substring(text(),4,2), '-',substring(text(),1,2))"/></schlachtTag>
<lieferSchein><xsl:value-of select="concat(substring(text(), 7, 4), substring(text(),4,2),substring(text(),1,2))"/></lieferSchein>
</xsl:template>
<xsl:template match="column8" mode="columnMapping">
<mastLand>DE</mastLand>
</xsl:template>
<xsl:template match="column3" mode="columnMapping">
<anzahl><xsl:text>1</xsl:text></anzahl>
<tier>
<xsl:choose>
<xsl:when test="text() = 'R'"><xsl:text>Rind</xsl:text></xsl:when>
<xsl:when test="text() = 'SW'"><xsl:text>Schwein</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>Schwein</xsl:text></xsl:otherwise>
</xsl:choose>
</tier>
</xsl:template>
<xsl:template match="column1" mode="columnMapping">
<schlachtCharge><xsl:value-of select="text()"/></schlachtCharge>
</xsl:template>
<xsl:template match="column9" mode="columnMapping">
<warmGewicht><xsl:value-of select="translate(text(),',','.')"/></warmGewicht>
</xsl:template>
<xsl:template match="column14" mode="columnMapping">
<phWert><xsl:value-of select="text()"/></phWert>
</xsl:template>
<xsl:template match="column13" mode="columnMapping">
<muskelFleischAnteil><xsl:value-of select="translate(text(),',','.')"/></muskelFleischAnteil>
</xsl:template>
<!-- Default postprocessing is just copying of nodes -->
<xsl:template match="@*|*|text()" mode="columnMapping">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()" mode="columnMapping"/>
</xsl:copy>
</xsl:template>
<!-- template that matches the root node-->
<xsl:template match="root">
<herkunftsImport>
<xsl:call-template name="texttorows">
<xsl:with-param name="StringToTransform" select="/root"/>
</xsl:call-template>
</herkunftsImport>
</xsl:template>
<!-- template that actually does the CSV conversion-->
<xsl:template name="texttorows">
<!-- import $StringToTransform-->
<xsl:param name="StringToTransform" select="''"/>
<xsl:choose>
<!-- Skip the header (hack using a hardcoded string) -->
<xsl:when test="contains($StringToTransform,'Laufende Nummer')">
<xsl:call-template name="texttorows">
<xsl:with-param name="StringToTransform">
<xsl:value-of select="substring-after($StringToTransform,$LF)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<!-- string contains linefeed-->
<xsl:when test="contains($StringToTransform,$LF)">
<!-- Get everything up to the first carriage return-->
<herkunft>
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform" select="substring-before($StringToTransform,$LF)"/>
</xsl:call-template>
</herkunft>
<!-- repeat for the remainder of the original string-->
<xsl:call-template name="texttorows">
<xsl:with-param name="StringToTransform">
<xsl:value-of select="substring-after($StringToTransform,$LF)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<!-- string does not contain newline, so just output it-->
<xsl:otherwise>
<herkunft>
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform" select="$StringToTransform"/>
</xsl:call-template>
</herkunft>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="csvtoxml">
<!-- import $StringToTransform-->
<xsl:param name="StringToTransform" select="''"/>
<xsl:param name="ColumnNum" select="1"/>
<xsl:choose>
<!-- string contains linefeed-->
<xsl:when test="contains($StringToTransform,$SEP)">
<!-- Get everything up to the first carriage return-->
<xsl:element name="{concat('column', $ColumnNum)}">
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="substring-before($StringToTransform,$SEP)"/>
</xsl:call-template>
</xsl:element>
<!-- repeat for the remainder of the original string-->
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform" select="substring-after($StringToTransform,$SEP)" />
<xsl:with-param name="ColumnNum" select="$ColumnNum + 1" />
</xsl:call-template>
</xsl:when>
<!-- string does not contain newline, so just output it-->
<xsl:otherwise>
<xsl:element name="{concat('column', $ColumnNum)}">
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="$StringToTransform"/>
</xsl:call-template>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Strips trailing whitespace characters from 'string' -->
<xsl:template name="string-rtrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:variable name="length" select="string-length($string)" />
<xsl:if test="$length &gt; 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, $length, 1))">
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="substring($string, 1, $length - 1)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading whitespace characters from 'string' -->
<xsl:template name="string-ltrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:if test="string-length($string) &gt; 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, 1, 1))">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="substring($string, 2)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading and trailing whitespace characters from 'string' -->
<xsl:template name="string-trim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment