<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:ScriptNS0="http://schemas.microsoft.com/BizTalk/2003/ScriptNS0"
                xmlns:ScriptNS1="http://schemas.microsoft.com/BizTalk/2003/ScriptNS1"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                
                xmlns:DateTime="TM.Shared.Components.DateTime"
                xmlns:SSO="TM.Shared.Components.SSO"
                xmlns:String="TM.Shared.Components.String"
                
                xmlns:Helper="TM.TransSmart.Components"
                
                xmlns:s0="http://TM.TransSmart.Shipment"
                xmlns:ns0="http://TM.KLG.com/KLG/Order/v19A"
                
                exclude-result-prefixes="s0 ns0 ScriptNS0 ScriptNS1 msxsl DateTime SSO String Helper">

  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" indent="yes" />

  <!-- region global variable initialization -->

  <xsl:variable name="connectionString" select="SSO:GetValue('TM.TransSmart', 'ESBLookupConnectionString')"/>

  <xsl:variable name="receiver" select="ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:carrier , $connectionString , 'TM_TransSmart_Receiver' , 'TransSmart_carrier') , 'KLG_receiver')"/>

  <xsl:variable name="sender">
    <xsl:choose>
      <xsl:when test="$receiver='KLGEUEERSE01'">
        <xsl:choose>
          <xsl:when test="normalize-space(ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , concat(/s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId, /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:costcenter) , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId+TransSmart_costcentre') , 'KLGEUEERSE01')) != ''">
            <xsl:value-of select="ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , concat(/s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId, /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:costcenter) , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId+TransSmart_costcentre') , 'KLGEUEERSE01')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId') , 'KLGEUEERSE01')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$receiver='KLGEUVENLO01'">
        <xsl:choose>
          <xsl:when test="normalize-space(ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , concat(/s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId, /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:costcenter) , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId+TransSmart_costcentre') , 'KLGEUVENLO01')) != ''">
            <xsl:value-of select="ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , concat(/s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId, /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:costcenter) , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId+TransSmart_costcentre') , 'KLGEUVENLO01')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="ScriptNS0:DBValueExtract(ScriptNS0:DBLookup(0 , /s0:carrierBooking/s0:shipments[1]/s0:shipmentInfo[1]/s0:customerId , $connectionString , 'TM_TransSmart_CompanyCode' , 'TransSmart_customerId') , 'KLGEUVENLO01')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text></xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="leadtimeSetup" select="ScriptNS0:DBLookup(0 , concat($sender, $receiver), $connectionString , 'TM_TransSmart_Leadtimes' , 'Sender + Receiver')"/>
  <xsl:variable name="LeadtimeCalculation" select="ScriptNS0:DBValueExtract($leadtimeSetup, 'Active')" />
  <xsl:variable name="CorrectionDays" select="number(ScriptNS0:DBValueExtract($leadtimeSetup, 'CorrectionDays'))" />
  <xsl:variable name="DefaultLeadtimeDays" select="ScriptNS0:DBValueExtract($leadtimeSetup, 'DefaultDays')" />
  <xsl:variable name="DefaultDeliveryCountry" select="ScriptNS0:DBValueExtract($leadtimeSetup, 'DefaultDeliveryCountry')" />
  <xsl:variable name="DefaultDeliveryTimeFrom" select="ScriptNS0:DBValueExtract($leadtimeSetup, 'DefaultDeliveryTimeFrom')" />
  <xsl:variable name="DefaultDeliveryTimeTo" select="ScriptNS0:DBValueExtract($leadtimeSetup, 'DefaultDeliveryTimeTo')" />

  <xsl:variable name="currentDate" select="DateTime:GetCurrentDate()" />
  <xsl:variable name="currentDateTime" select="DateTime:GetCurrentDateTime()" />

  <!-- end region -->

  <!-- region templates -->

  <xsl:template match="/">
    <xsl:apply-templates select="/s0:carrierBooking" />
  </xsl:template>

  <xsl:template match="/s0:carrierBooking">
    <ns0:Order>
      <Sender>
        <xsl:value-of select="$sender"/>
      </Sender>
      <Receiver>
        <xsl:value-of select="$receiver"/>
      </Receiver>
      <GroupName>N/A</GroupName>
      <Consolidation>N/A</Consolidation>
      <ReceiverVersion>N/A</ReceiverVersion>
      <OrchestrationResult>N/A</OrchestrationResult>
      <Reference>
        <xsl:value-of select="s0:shipments[1]/s0:shipmentInfo[1]/s0:reference"/>
      </Reference>
      <TimeStamp>
        <xsl:value-of select="$currentDateTime" />
      </TimeStamp>
      <Status>NEW</Status>
      <Date>
        <xsl:value-of select="$currentDate" />
      </Date>
      <FullTruckLoad>false</FullTruckLoad>
      <xsl:for-each select="s0:shipments/s0:shipmentInfo">
        <Shipment>
          <xsl:call-template name="Shipment"/>
        </Shipment>
      </xsl:for-each>
    </ns0:Order>
  </xsl:template>

  <xsl:template name="Shipment">
    <Consignor>
      <xsl:value-of select="$sender"/>
    </Consignor>
    <ConsignorReference>
      <xsl:value-of select="s0:reference"/>
    </ConsignorReference>
    <Debtor>
      <xsl:value-of select="$sender"/>
    </Debtor>
    <DebtorReference>
      <xsl:choose>
        <xsl:when test="normalize-space(s0:additionalReferences/s0:additionalReference[s0:type='ORDER']/s0:value) != ''">
          <xsl:value-of select="s0:additionalReferences/s0:additionalReference[s0:type='ORDER']/s0:value"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="s0:reference"/>
        </xsl:otherwise>
      </xsl:choose>
    </DebtorReference>
    <Incoterm>
      <xsl:choose>
        <xsl:when test="s0:incoterms = 'DDU'">DAP</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="s0:incoterms"/>
        </xsl:otherwise>
      </xsl:choose>
    </Incoterm>
    <IncotermCity/>
    <IncotermCountryCode/>
    <xsl:variable name="deliveryFeatures">
      <xsl:if test="s0:serviceLevelOther!=''">
        <!-- Transsmart heeft een probleem met underscores in het serviceLevel element -->
        <!-- Daarom gebruiken zij ~ en moet KLG deze vervangen door _ -->
        <!-- Meerdere services worden gescheiden door een + teken -->
        <xsl:call-template name="features">
          <xsl:with-param name="splitString" select="translate(s0:serviceLevelOther,'~','_')"/>
          <xsl:with-param name="delimiter" select="'+'"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:variable>
    <xsl:call-template name="Address_SF" />
    <xsl:call-template name="Address_ST">
      <xsl:with-param name="deliveryFeatures" select="$deliveryFeatures"/>
    </xsl:call-template>
    <xsl:call-template name="Good">
      <xsl:with-param name="deliveryFeatures" select="$deliveryFeatures"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="Address_SF">

    <xsl:variable name="collectionDate" select="concat(substring(s0:pickupDate, 1, 4), '-', substring(s0:pickupDate, 5, 2), '-', substring(s0:pickupDate, 7, 2))"/>
    <xsl:variable name="collectionTimeFrom" select="concat(substring(s0:pickupTime, 1, 2), ':', substring(s0:pickupTime, 3, 2), ':00')"/>
    <xsl:variable name="collectionTimeUntill" select="concat(substring(s0:pickupTimeTo, 1, 2), ':', substring(s0:pickupTimeTo, 3, 2), ':00')"/>

    <xsl:variable name="loadingDateTimeFrom">
      <xsl:choose>
        <xsl:when test="not($collectionDate = '00000000') and not(normalize-space($collectionDate)='') and string-length($collectionDate) = 10">
          <xsl:choose>
            <xsl:when test="not(normalize-space($collectionTimeFrom)='') and string-length($collectionTimeFrom) = 8">
              <xsl:value-of select="concat($collectionDate, 'T', $collectionTimeFrom)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat($collectionDate, 'T07:00:00')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($currentDate, 'T07:00:00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="loadingDateTimeTo">
      <xsl:choose>
        <xsl:when test="not($collectionDate = '00000000') and not(normalize-space($collectionDate)='') and string-length($collectionDate) = 10">
          <xsl:choose>
            <xsl:when test="not(normalize-space($collectionTimeUntill)='') and string-length($collectionTimeUntill) = 8">
              <xsl:value-of select="concat($collectionDate, 'T', $collectionTimeUntill)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat($collectionDate, 'T16:00:00')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($currentDate, 'T16:00:00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="loadingReference">
      <xsl:choose>
        <xsl:when test="$sender = 'JOBESHEERE01' and substring(s0:instruction,1,3) = 'RDV'">
          <xsl:value-of select="s0:instruction"/>
        </xsl:when>
        <xsl:when test="normalize-space(s0:additionalReferences/s0:additionalReference[s0:type='ROUTE_ID']/s0:value) != ''">
          <xsl:value-of select="s0:additionalReferences/s0:additionalReference[s0:type='ROUTE_ID']/s0:value"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="s0:reference"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <Address>
      <AddressType>SF</AddressType>
      <xsl:call-template name="Address">
        <xsl:with-param name="addressCode" select="''" />
        <xsl:with-param name="name1" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:name" />
        <xsl:with-param name="name2" select = "''" />
        <xsl:with-param name="address1" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:street1" />
        <xsl:with-param name="address2" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:street2" />
        <xsl:with-param name="postalCode" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:zipCode" />
        <xsl:with-param name="city" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:city" />
        <xsl:with-param name="region" select = "s0:addresses/s0:address[s0:type = 'SEND' and s0:countryCode = 'IE']/s0:state" />
        <xsl:with-param name="countryCode" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:countryCode" />
        <xsl:with-param name="start" select = "$loadingDateTimeFrom" />
        <xsl:with-param name="end" select = "$loadingDateTimeTo" />
        <xsl:with-param name="reference" select = "$loadingReference" />
        <xsl:with-param name="externalRemark1" select = "''" />
        <xsl:with-param name="externalRemark2" select = "''" />
        <xsl:with-param name="internalRemark1" select = "''" />
        <xsl:with-param name="internalRemark2" select = "''" />
      </xsl:call-template>
      <xsl:call-template name="ContactPerson">
        <xsl:with-param name="name" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:contact" />
        <xsl:with-param name="phone" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:telNo" />
        <xsl:with-param name="fax" select = "''" />
        <xsl:with-param name="email" select = "s0:addresses/s0:address[s0:type = 'SEND']/s0:email" />
      </xsl:call-template>
    </Address>
  </xsl:template>

  <xsl:template name="Address_ST">
    <xsl:param name="reference" />
    <xsl:param name="deliveryFeatures" />
    
    <xsl:variable name="collectionDate" select="concat(substring(s0:pickupDate/text(), 1, 4), '-', substring(s0:pickupDate/text(), 5, 2), '-', substring(s0:pickupDate/text(), 7, 2))"/>

    <xsl:variable name ="adjustedDeliveryDateTimeFrom">
      <xsl:choose>
        <xsl:when test="number(DateTime:CompareDateTime($currentDate, $collectionDate)) &lt;= 0">
          <xsl:value-of select="concat(DateTime:GetWorkday($collectionDate, '2', 'NL'),'T07:00:00')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($currentDate, 'T07:00:00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name ="adjustedDeliveryDateTimeTo">
      <xsl:choose>
        <xsl:when test="number(DateTime:CompareDateTime($currentDate, $collectionDate)) &lt;= 0">
          <xsl:value-of select="concat(DateTime:GetWorkday($collectionDate, '2', 'NL'),'T16:00:00')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($currentDate, 'T16:00:00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="deliveryDate" select="concat(substring(s0:requestedDeliveryDate/text(), 1, 4), '-', substring(s0:requestedDeliveryDate/text(), 5, 2), '-', substring(s0:requestedDeliveryDate/text(), 7, 2))"/>
    <xsl:variable name="deliveryTimeFrom" select="concat(substring(s0:requestedDeliveryTime/text(), 1, 2), ':', substring(s0:requestedDeliveryTime/text(), 3, 2), ':00')"/>
    <xsl:variable name="deliveryTimeUntill" select="concat(substring(s0:requestedDeliveryTimeTo/text(), 1, 2), ':', substring(s0:requestedDeliveryTimeTo/text(), 3, 2), ':00')"/>

    <xsl:variable name="deliveryDateTimeFrom">
      <xsl:choose>
        <xsl:when test="not($deliveryDate = '00000000') and not(normalize-space($deliveryDate)='') and string-length($deliveryDate) = 10">
          <xsl:choose>
            <xsl:when test="not($deliveryTimeFrom = '00:00:00') and not(normalize-space($deliveryTimeFrom)='') and string-length($deliveryTimeFrom) = 8">
              <xsl:value-of select="concat($deliveryDate, 'T', $deliveryTimeFrom)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat($deliveryDate, 'T07:00:00')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$adjustedDeliveryDateTimeFrom"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="deliveryDateTimeTo">
      <xsl:choose>
        <xsl:when test="not($deliveryDate = '00000000') and not(normalize-space($deliveryDate)='') and string-length($deliveryDate) = 10">
          <xsl:choose>
            <xsl:when test="not($deliveryTimeUntill = '00:00:00') and not(normalize-space($deliveryTimeUntill)='') and string-length($deliveryTimeUntill) = 8">
              <xsl:value-of select="concat($deliveryDate, 'T', $deliveryTimeUntill)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat($deliveryDate, 'T16:00:00')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$adjustedDeliveryDateTimeTo"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <Address>
      <AddressType>ST</AddressType>

      <xsl:variable name="deliveryReference">
        <xsl:choose>
          <xsl:when test="s0:additionalReferences/s0:additionalReference[s0:type='YOUR_REFERENCE'] and normalize-space(s0:additionalReferences/s0:additionalReference[s0:type='YOUR_REFERENCE']/s0:value)!=''">
            <xsl:value-of select="s0:additionalReferences/s0:additionalReference[s0:type='YOUR_REFERENCE']/s0:value"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="s0:reference"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:call-template name="Address">
        <xsl:with-param name="addressCode" select="''" />
        <xsl:with-param name="name1" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:name" />
        <xsl:with-param name="name2" select = "''" />
        <xsl:with-param name="address1" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:street1" />
        <xsl:with-param name="address2" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:street2" />
        <xsl:with-param name="postalCode" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:zipCode" />
        <xsl:with-param name="city" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:city" />
        <xsl:with-param name="region" select = "s0:addresses/s0:address[s0:type = 'RECV' and s0:countryCode = 'IE']/s0:state" />
        <xsl:with-param name="countryCode" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:countryCode" />
        <xsl:with-param name="start" select = "$deliveryDateTimeFrom" />
        <xsl:with-param name="end" select = "$deliveryDateTimeTo" />
        <xsl:with-param name="reference" select = "$deliveryReference" />
        <xsl:with-param name="externalRemark1" select = "''" />
        <xsl:with-param name="externalRemark2" select = "s0:instruction" />
        <xsl:with-param name="internalRemark1" select = "''" />
        <xsl:with-param name="internalRemark2" select = "''" />
      </xsl:call-template>

      <!--<xsl:if test="s0:serviceLevelTime!=''">
        <xsl:call-template name="features">
          <xsl:with-param name="splitString" select="translate(s0:serviceLevelTime,'~','_')"/>
          <xsl:with-param name="delimiter" select="'+'"/>
        </xsl:call-template>
      </xsl:if>-->
 

      <xsl:copy-of select="$deliveryFeatures"/>
      <xsl:if test="$LeadtimeCalculation = 'True'">
        <Parametergroup>
          <Groupname>
            <xsl:text>LeadtimeData</xsl:text>
          </Groupname>
          <Parameter>
            <Qualifier>
              <xsl:text>ReceiverPostalcode</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="translate(s0:addresses/s0:address[s0:type = 'RECV']/s0:zipCode, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ', '')"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>SenderPostalcode</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="translate(s0:addresses/s0:address[s0:type = 'SEND']/s0:zipCode, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ', '')"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>ReceiverCountry</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'RECV']/s0:countryCode"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>SenderCountry</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'SEND']/s0:countryCode"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>PackageQuantity</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="sum(s0:shipmentLines/s0:shipmentLine/s0:quantity)"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>PackageKilogram</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="sum(s0:shipmentLines/s0:shipmentLine/s0:measurements/s0:weight)"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>ADR</xsl:text>
            </Qualifier>
            <Value>
              <xsl:text>false</xsl:text>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>defaultLeadDate</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="DateTime:GetWorkday(concat(substring(s0:pickupDate/text(), 1, 4), '-', substring(s0:pickupDate/text(), 5, 2), '-', substring(s0:pickupDate/text(), 7, 2)), $DefaultLeadtimeDays, $DefaultDeliveryCountry)"/>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>StartingDate</xsl:text>
            </Qualifier>
            <Value>
              <!--<xsl:value-of select="concat(substring(s0:pickupDate/text(), 1, 4), '-', substring(s0:pickupDate/text(), 5, 2), '-', substring(s0:pickupDate/text(), 7, 2))"/>
              <xsl:text> Correction </xsl:text>
              <xsl:value-of select="$CorrectionDays"/>
              <xsl:text> Result </xsl:text>-->
              <xsl:value-of select="DateTime:GetWorkday(concat(substring(s0:pickupDate/text(), 1, 4), '-', substring(s0:pickupDate/text(), 5, 2), '-', substring(s0:pickupDate/text(), 7, 2)), $CorrectionDays, s0:addresses/s0:address[s0:type = 'SEND']/s0:countryCode)"/>
              <!--<xsl:text> Loading </xsl:text>
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'SEND']/s0:countryCode"/>-->
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>timeFrom</xsl:text>
            </Qualifier>
            <Value>
              <xsl:choose>
                <xsl:when test="not($deliveryTimeFrom = '00:00:00') and not(normalize-space($deliveryTimeFrom)='') and string-length($deliveryTimeFrom) = 8">
                   <xsl:value-of select="$deliveryTimeFrom"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$DefaultDeliveryTimeFrom"/>
                </xsl:otherwise>
              </xsl:choose>              
             </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>timeTo</xsl:text>
            </Qualifier>
            <Value>
              <xsl:choose>
                <xsl:when test="not($deliveryTimeUntill = '00:00:00') and not(normalize-space($deliveryTimeUntill)='') and string-length($deliveryTimeUntill) = 8">
                  <xsl:value-of select="$deliveryTimeUntill"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$DefaultDeliveryTimeTo"/>
                </xsl:otherwise>
              </xsl:choose>
            </Value>
          </Parameter>
          <Parameter>
            <Qualifier>
              <xsl:text>CompanyCode</xsl:text>
            </Qualifier>
            <Value>
              <xsl:value-of select="$sender"/>
            </Value>
          </Parameter>
        </Parametergroup>
      </xsl:if>

      <xsl:call-template name="ContactPerson">
        <xsl:with-param name="name" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:contact" />
        <xsl:with-param name="phone" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:telNo" />
        <xsl:with-param name="fax" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:faxNo" />
        <xsl:with-param name="email" select = "s0:addresses/s0:address[s0:type = 'RECV']/s0:email" />
      </xsl:call-template>
    </Address>
  </xsl:template>

  <xsl:template name="Address">
    <xsl:param name="addressCode" />
    <xsl:param name="name1" />
    <xsl:param name="name2" />
    <xsl:param name="address1" />
    <xsl:param name="address2" />
    <xsl:param name="postalCode" />
    <xsl:param name="city" />
    <xsl:param name="region" />
    <xsl:param name="countryCode" />
    <xsl:param name="start" />
    <xsl:param name="end" />
    <xsl:param name="reference" />
    <xsl:param name="externalRemark1" />
    <xsl:param name="externalRemark2" />
    <xsl:param name="internalRemark1" />
    <xsl:param name="internalRemark2" />
    <AddressCode>
      <xsl:value-of select="$addressCode" />
    </AddressCode>
    <Name1>
      <xsl:value-of select="$name1" />
    </Name1>
    <Name2>
      <xsl:value-of select="$name2" />
    </Name2>
    <Address1>
      <xsl:value-of select="$address1" />
    </Address1>
    <Address2>
      <xsl:value-of select="$address2" />
    </Address2>
    <PostalCode>
      <xsl:value-of select="$postalCode" />
    </PostalCode>
    <City>
      <xsl:value-of select="$city" />
    </City>
    <Region>
      <xsl:value-of select="$region" />
    </Region>
    <CountryCode>
      <xsl:value-of select="$countryCode" />
    </CountryCode>
    <Start>
      <xsl:value-of select="$start" />
    </Start>
    <End>
      <xsl:value-of select="$end" />
    </End>
    <Reference>
      <xsl:value-of select="$reference" />
    </Reference>
    <ExternalRemark1>
      <xsl:value-of select="$externalRemark1" />
    </ExternalRemark1>
    <ExternalRemark2>
      <xsl:value-of select="$externalRemark2" />
    </ExternalRemark2>
    <InternalRemark1>
      <xsl:value-of select="$internalRemark1" />
    </InternalRemark1>
    <InternalRemark2>
      <xsl:value-of select="$internalRemark2" />
    </InternalRemark2>
  </xsl:template>

  <xsl:template name="features">
    <xsl:param name="splitString" select="."/>
    <xsl:param name="delimiter" select="'+'"/>
    <xsl:choose>
      <xsl:when test="contains($splitString,$delimiter)">
        <xsl:call-template name="feature">
          <xsl:with-param name="service" select="substring-before($splitString,$delimiter)"/>
        </xsl:call-template>
        <xsl:call-template name="features">
          <xsl:with-param name="splitString" select="substring-after($splitString,$delimiter)"/>
          <xsl:with-param name="delimiter" select="$delimiter"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="feature">
          <xsl:with-param name="service" select="$splitString"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="feature">
    <xsl:param name="service"/>
    <!--Het Transsmart service level STANDARD wordt door KLG genegeerd
    c.q. niet als feature opgenomen
    <xsl:if test="$service!='STANDARD'">-->
      <Feature>
        <FeatureCode>
          <xsl:value-of select="$service"/>
        </FeatureCode>
        <xsl:if test="$service='APP_REQ_EMAIL' or $service='APP_REQ_FAX' or $service='APP_REQ_INTERNET' or $service='APP_REQ_PHONE'">
          <AdditionalValue>
            <xsl:if test="$service='APP_REQ_EMAIL'">
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'RECV']/s0:email"/>
            </xsl:if>
            <xsl:if test="$service='APP_REQ_FAX'">
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'RECV']/s0:faxNo"/>
            </xsl:if>
            <xsl:if test="$service='APP_REQ_INTERNET'">
            </xsl:if>
            <xsl:if test=" $service='APP_REQ_PHONE'">
              <xsl:value-of select="s0:addresses/s0:address[s0:type = 'RECV']/s0:telNo"/>
            </xsl:if>
          </AdditionalValue>
        </xsl:if>
      </Feature>
    <!--</xsl:if>-->
  </xsl:template>

  <xsl:template name="ContactPerson">
    <xsl:param name="name" />
    <xsl:param name="phone" />
    <xsl:param name="fax" />
    <xsl:param name="email" />
    <ContactPerson>
      <Name>
        <xsl:value-of select="$name" />
      </Name>
      <Phone>
        <xsl:value-of select="$phone" />
      </Phone>
      <Fax>
        <xsl:value-of select="$fax" />
      </Fax>
      <Email>
        <xsl:value-of select="$email" />
      </Email>
    </ContactPerson>
  </xsl:template>

  <xsl:template name="Good">
    <xsl:param name="deliveryFeatures" />
    
    <xsl:for-each select="s0:shipmentLines/s0:shipmentLine">

      <!--Current Line values ...-->
      <xsl:variable name="cType" select="s0:packageType" />
      <xsl:variable name="cDesc" select="s0:description" />
      <xsl:variable name="cLeng" select="s0:measurements/s0:length" />
      <xsl:variable name="cWidt" select="s0:measurements/s0:width" />
      <xsl:variable name="cHeig" select="s0:measurements/s0:height" />

      <xsl:if test="generate-id() = generate-id(../s0:shipmentLine[s0:packageType=$cType][s0:description=$cDesc][s0:measurements/s0:length=$cLeng][s0:measurements/s0:width=$cWidt][s0:measurements/s0:height=$cHeig][1])">

        <xsl:variable name="tQuantity" select="sum(../s0:shipmentLine[s0:packageType=$cType][s0:description=$cDesc][s0:measurements/s0:length=$cLeng][s0:measurements/s0:width=$cWidt][s0:measurements/s0:height=$cHeig]/s0:quantity)" />
        
        <xsl:variable name="tWeight" select="round(sum(../s0:shipmentLine[s0:packageType=$cType][s0:description=$cDesc][s0:measurements/s0:length=$cLeng][s0:measurements/s0:width=$cWidt][s0:measurements/s0:height=$cHeig]/s0:measurements/s0:weight))"/>


        
        <Good>

          <xsl:variable name="packageType" select="Helper:GetDelimitedValueByIndex(s0:packageType, '|', 0)"/>
          <xsl:variable name="packageExchange" select="Helper:GetDelimitedValueByIndex(s0:packageType, '|', 1)"/>
          <xsl:variable name="Stackable" select="Helper:GetDelimitedValueByIndex(s0:packageType, '|', 2)"/>
          
          <!--<DEBUG>
            <TYPE>
              <xsl:value-of select="$packageType"/>
            </TYPE>
            <EXCHANGE>
              <xsl:value-of select="$packageExchange"/>
            </EXCHANGE>
            <STACKABLE>
              <xsl:value-of select="$Stackable"/>
            </STACKABLE>
          </DEBUG>-->
          
          <PackingQty>
            <xsl:value-of select="$tQuantity"/>
          </PackingQty>
          <PackingType>
            <!--<xsl:choose>
              <xsl:when test="contains(s0:packageType, '|')">
                <xsl:value-of select="substring-before(s0:packageType, '|')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="s0:packageType"/>
              </xsl:otherwise>
            </xsl:choose>-->
            <xsl:value-of select="$packageType"/>
          </PackingType>
          <!-- igv Feature NR exchange  negeren -->
          <!--<xsl:if test="contains(s0:packageType, '|') and not(msxsl:node-set($deliveryFeatures)/Feature/FeatureCode = 'NR')">-->
          <xsl:if test="$packageExchange!='' and not(msxsl:node-set($deliveryFeatures)/Feature/FeatureCode = 'NR')">
            <PackingExchange>
              <!--<xsl:value-of select="substring-after(s0:packageType, '|')"/>-->
              <xsl:value-of select="$packageExchange"/>
            </PackingExchange>
          </xsl:if>
          <ProductDescription>
            <xsl:value-of select="s0:description"/>
          </ProductDescription>
          <BrutoWeight>
            <xsl:value-of select="$tWeight"/>
          </BrutoWeight>
          <NettoWeight>
            <xsl:value-of select="$tWeight"/>
          </NettoWeight>
          <LoadingMeter>
            <xsl:value-of select="format-number(((((round($cLeng) * round($cWidt)) div 24000) * $tQuantity) * 100) div 100,'##0.00' )"/>
          </LoadingMeter>
          <Volume>
            <xsl:value-of select="((round($cLeng) * round($cWidt) * round($cHeig)) * $tQuantity) div 1000000"/>
          </Volume>
          <xsl:if test="String:IsNumeric($Stackable)">
            <StackingLayers>
              <xsl:value-of select="$Stackable"/>
            </StackingLayers>
          </xsl:if>
          <PackingLength>
            <xsl:value-of select="$cLeng"/>
          </PackingLength>
          <PackingWidth>
            <xsl:value-of select="$cWidt"/>
          </PackingWidth>
          <PackingHeight>
            <xsl:value-of select="$cHeig"/>
          </PackingHeight>
      

      <xsl:for-each select="../s0:shipmentLine[s0:packageType=$cType][s0:description=$cDesc][s0:measurements/s0:length=$cLeng][s0:measurements/s0:width=$cWidt][s0:measurements/s0:height=$cHeig]/s0:dangerousGoods/s0:dangerousGoodsInfo">
          
          
        <DangerousGood>
          <DGSADRCode/>
          <UNNumber>
            <xsl:value-of select="s0:unCode/text()"/>
          </UNNumber>
          <Class/>
          <SubClass1>
            <xsl:value-of select="s0:hazardClassPrimary/text()"/>
          </SubClass1>
          <SubClass2>
            <xsl:value-of select="s0:hazardClassSecondary/text()"/>
          </SubClass2>
          <SubClass3/>
          <Classification>
            <xsl:value-of select="s0:classificationCode/text()"/>
          </Classification>
          <Labels/>
          <Qty>
            <xsl:value-of select="s0:quantity/text()"/>
          </Qty>
          <Weight>
            <xsl:value-of select="s0:measurements[s0:weightUom='KG']/s0:weight/text()"/>
          </Weight>
          <Volume/>
          <ProductDescription>
            <xsl:value-of select="s0:description/text()"/>
          </ProductDescription>
          <PackageType>
            <xsl:value-of select="s0:packingType/text()"/>
          </PackageType>
          <PackingGroup>
            <xsl:choose>
              <xsl:when test="s0:packingGroup/text() = '0'">
                <xsl:text>0</xsl:text>
              </xsl:when>
              <xsl:when test="s0:packingGroup/text() = '1'">
                <xsl:text>I</xsl:text>
              </xsl:when>
              <xsl:when test="s0:packingGroup/text() = '2'">
                <xsl:text>II</xsl:text>
              </xsl:when>
              <xsl:when test="s0:packingGroup/text() = '3'">
                <xsl:text>III</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="s0:packingGroup/text()"/>
              </xsl:otherwise>
            </xsl:choose>            
          </PackingGroup>
          <TransportCategory/>
          <NOSText/>
          <TunnelRestrictionCode>
            <xsl:value-of select="s0:tunnelCode/text()"/>
          </TunnelRestrictionCode>
          <EnvironmentallyHazardous/>
          <MarinePollutant/>
          <xsl:choose>
            <xsl:when test="s0:limitedQuantity > '0' and s0:limitedQuantity != 'N'">
              <LimitedQuantity><xsl:text>Y</xsl:text></LimitedQuantity>
            </xsl:when>
            <xsl:otherwise>
              <LimitedQuantity/>
            </xsl:otherwise>            
          </xsl:choose>          
          <LocalizedDescription/>
        </DangerousGood>

      </xsl:for-each>

        </Good>
      </xsl:if>
    </xsl:for-each>
    
    
  </xsl:template>

  <!-- end region -->

  <xsl:variable name="dbClose" select="ScriptNS0:DBLookupShutdown()"/>

</xsl:stylesheet>
