<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:su="com.spinque.tools.importStream.Utils"
  xmlns:spinque="com.spinque.tools.importStream.EmitterWrapper"
  extension-element-prefixes="spinque">

  <xsl:output method="text" encoding="UTF-8"/>
  <xsl:template match="table/row">

 
    <xsl:variable name="strikeID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/', field[@name='id'])"/>
    <xsl:variable name="Year" select="substring (field[@name='startdate'], 1,4)" />
    <spinque:relation subject="{$strikeID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/LabourConflict" /> 
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/name" type="string" value="{field[@name='action_en']}{field[@name='andere_actie_en_1']}, {field[@name='demand_en_1']} - {$Year}" lang="en" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/name" type="string" value="{field[@name='action_nl']}{field[@name='andere_actie_nl_1']}, {field[@name='demand_nl_1']} - {$Year}" lang="nl" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/startDate" type="date" value="{field[@name='startdate']}" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/description" type="string" value="{field[@name='description']}" lang="nl" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/agent" type="string" value="{field[@name='character_en']}" lang="en" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/agent" type="string" value="{field[@name='character_nl']}" lang="nl" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/result" type="string" value="{field[@name='result_en']}" lang="en" />
    <spinque:attribute subject="{$strikeID}" attribute="https://schema.org/result" type="string" value="{field[@name='result_nl']}" lang="nl" />
    <spinque:attribute subject="{$strikeID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/numberOfStrikers" type="integer" value="{field[@name='TotalStakers']}" />
    <spinque:attribute subject="{$strikeID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/numberOfDays" type="integer" value="{field[@name='TotalGestaakteDagen']}" />
    <spinque:attribute subject="{$strikeID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/numberOfLockouts" type="integer" value="{field[@name='TotalUitgeslotenen']}" />
    <spinque:attribute subject="{$strikeID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/numberOfLostDays" type="integer" value="{field[@name='TotalVerlorenDagenUitgeslotene']}" />
    <spinque:attribute subject="{$strikeID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/numberOfActivists" type="integer" value="{field[@name='TotalAantalActievoerders']}" />
  
    <xsl:if test="field[@name='duration'] != ''">
    <xsl:variable name="durationID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/duration/', field[@name='duration'])"/>
    <spinque:relation subject="{$strikeID}" predicate="https://schema.org/duration" object="{$durationID}" />
    <spinque:relation subject="{$durationID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/Duration" />
    <spinque:attribute subject="{$durationID}" attribute="https://schema.org/name" type="string" value="P{field[@name='duration']}D"/>
    <spinque:attribute subject="{$durationID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{field[@name='duration']} dag(en)" lang="nl" />
    <spinque:attribute subject="{$durationID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{field[@name='duration']} day(s)" lang="en" />
    </xsl:if>

    <xsl:if test="field[@name='action_en'] != ''"> 
    <xsl:variable name="actionID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/', field[@name='id'], 'action', field[@name='action_en'])"/>
    <spinque:relation subject="{$strikeID}" predicate="https://iisg.amsterdam/vocab/labourconflicts/action" object="{$actionID}" />
    <spinque:relation subject="{$actionID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/Action" />
    <spinque:attribute subject="{$actionID}" attribute="https://schema.org/name" type="string" value="{field[@name='action_en']}" lang="en" />
    <spinque:attribute subject="{$actionID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{field[@name='action_nl']}" lang="nl" />
    <spinque:attribute subject="{$actionID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{field[@name='action_en']}" lang="en" />
    <spinque:attribute subject="{$actionID}" attribute="https://schema.org/category" type="string" value="{field[@name='type_of_strike_en']}" lang="en"/>
    <spinque:attribute subject="{$actionID}" attribute="https://schema.org/category" type="string" value="{field[@name='type_of_strike_nl']}" lang="nl"/>
    

    <xsl:choose>
      <xsl:when test="field[@name='action_en'] = 'Strike' ">
        <spinque:relation subject="{$actionID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/Strike"/>
      </xsl:when>
      <xsl:when test="field[@name='action_en'] = 'Lockout' ">
        <spinque:relation subject="{$actionID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/Lockout"/>
      </xsl:when>
    </xsl:choose>
</xsl:if>
    <xsl:call-template name="templateForPlaces">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

    <xsl:call-template name="templateForCompany">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

    <xsl:call-template name="templateForOccupation">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

    <xsl:call-template name="templateForUnions">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

    <xsl:call-template name="templateForDemands">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

    <xsl:call-template name="templateForActions">
      <xsl:with-param name="strikeID" select="$strikeID" />
    </xsl:call-template>

  </xsl:template>

  <xsl:template name="templateForPlaces">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="132" />

    <xsl:variable name="place_name" select="field[@name=concat('place_', $index)]"/>
    <xsl:variable name="province_name" select="field[@name=concat('province_', $index)]"/>

    <xsl:if test="$place_name != ''">
      <xsl:variable name="placeID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/place/', $place_name)"/>

      <spinque:relation subject="{$placeID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/City" />
      <spinque:attribute subject="{$placeID}" attribute="https://schema.org/name" type="string" value="{$place_name}" />

      <xsl:variable name="provinceID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/province/', $province_name)"/>

      <spinque:relation subject="{$provinceID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/AdministrativeArea" />
      <spinque:attribute subject="{$provinceID}" attribute="https://schema.org/name" type="string" value="{$province_name}" />

      <spinque:relation subject="{$placeID}" predicate="http://www.opengis.net/ont/geosparql#sfWithin" object="{$provinceID}" />

      <spinque:relation subject="{$strikeID}" predicate="https://schema.org/location" object="{$placeID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForPlaces">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>

  </xsl:template>

   <xsl:template name="templateForCompany">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="83" />

    <xsl:variable name="company_name" select="field[@name=concat('company_name_', $index)]"/>
    <xsl:variable name="company_place" select="field[@name=concat('company_place_', $index)]"/>
    <xsl:variable name="company_province" select="field[@name=concat('company_province_', $index)]"/>

    <xsl:if test="$company_name != ''">
      
       <xsl:variable name="companyID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/organization/', $company_name)"/>

      <spinque:relation subject="{$companyID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/Corporation" />
      <spinque:attribute subject="{$companyID}" attribute="https://schema.org/name" type="string" value="{$company_name}" />

      <xsl:if test="$company_place != ''">

      <xsl:variable name="company_placeID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/place/', $company_place)"/>

      <spinque:relation subject="{$company_placeID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/City" />
      <spinque:attribute subject="{$company_placeID}" attribute="https://schema.org/name" type="string" value="{$company_place}" />

      <xsl:if test="$company_province != ''">

      <xsl:variable name="company_provinceID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/province/', $company_province)"/>

      <spinque:relation subject="{$company_provinceID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/AdministrativeArea" />
      <spinque:attribute subject="{$company_provinceID}" attribute="https://schema.org/name" type="string" value="{$company_province}" />
      <spinque:relation subject="{$company_placeID}" predicate="http://www.opengis.net/ont/geosparql#sfWithin" object="{$company_provinceID}" />
      
      </xsl:if>

      <spinque:relation subject="{$companyID}" predicate="https://schema.org/location" object="{$company_placeID}" />

      </xsl:if>

      <spinque:relation subject="{$strikeID}" predicate="https://schema.org/organization" object="{$companyID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForCompany">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>

  </xsl:template>

  <xsl:template name="templateForOccupation">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="11" />

    <xsl:variable name="occupation" select="field[@name=concat('occupation_nl_', $index)]"/>
    <xsl:variable name="occupation_en" select="field[@name=concat('occupation_en_', $index)]"/>
    <xsl:variable name="occupation_hisco" select="field[@name=concat('occupation_hisco_', $index)]"/>

    <xsl:if test="$occupation != ''">
      <xsl:variable name="occupationID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/occupation/', $occupation)"/>

      <spinque:relation subject="{$occupationID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/Occupation" />
      <spinque:attribute subject="{$occupationID}" attribute="https://schema.org/name" type="string" value="{$occupation}" lang="nl"/>
      <spinque:attribute subject="{$occupationID}" attribute="https://schema.org/name" type="string" value="{$occupation_en}" lang="en"/>
      <xsl:variable name="hiscoID" select="su:uri('https://iisg.amsterdam/resource/hisco/code/hisco/', $occupation_hisco)"/>

      <spinque:relation subject="{$hiscoID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/resource/hisco/MicroGroup" />
      <spinque:relation subject="{$hiscoID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="http://schema.org/CategoryCode" />

      <spinque:relation subject="{$occupationID}" predicate="https://schema.org/occupationalCategory" object="{$hiscoID}" />

      <spinque:relation subject="{$strikeID}" predicate="https://iisg.amsterdam/vocab/labourconflicts/profession" object="{$occupationID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForOccupation">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>

  </xsl:template>

  <xsl:template name="templateForUnions">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="6" />

    <xsl:variable name="labourunion_abbreviation" select="field[@name=concat('labourunion_abbreviation_', $index)]"/>
    <xsl:variable name="labourunion_name" select="field[@name=concat('labourunion_name_', $index)]"/>
    <xsl:variable name="labourunionfederation_abbreviation" select="field[@name=concat('labourunionfederation_abbreviation_', $index)]"/>
    <xsl:variable name="labourunionfederation_name" select="field[@name=concat('labourunionfederation_name_', $index)]"/>

    <xsl:if test="$labourunion_abbreviation != ''">
      <xsl:variable name="labourunionID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/union/', $labourunion_abbreviation)"/>

      <spinque:relation subject="{$labourunionID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/Union" />
      <spinque:attribute subject="{$labourunionID}" attribute="https://schema.org/name" type="string" value="{$labourunion_name}" lang="nl"/>
      <spinque:attribute subject="{$labourunionID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/unionAbbrev" type="string" value="{$labourunion_abbreviation}" />

      <xsl:if test="$labourunionfederation_abbreviation != ''">

         <xsl:variable name="labourunionfederationID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/unionfed/', $labourunionfederation_abbreviation)"/>

         <spinque:relation subject="{$labourunionfederationID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/UnionFederation" />
         <spinque:attribute subject="{$labourunionfederationID}" attribute="https://schema.org/name" type="string" value="{$labourunionfederation_name}" lang="nl"/>
         <spinque:relation subject="{$labourunionID}" predicate="http://www.w3.org/ns/org#unitOf" object="{$labourunionfederationID}" />
      </xsl:if>

      <spinque:relation subject="{$strikeID}" predicate="https://iisg.amsterdam/vocab/labourconflicts/union" object="{$labourunionID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForUnions">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>

  </xsl:template>

  <xsl:template name="templateForDemands">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="6" />

    <xsl:variable name="reason" select="field[@name=concat('reason_', $index)]"/>
    <xsl:variable name="demand" select="field[@name=concat('demand_nl_', $index)]"/>
    <xsl:variable name="demand_en" select="field[@name=concat('demand_en_', $index)]"/>
    <xsl:variable name="main_reason" select="field[@name=concat('main_reason_', $index)]"/>

    <xsl:if test="$reason != ''">
      <xsl:variable name="reasonID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/', field[@name='id'], 'demand', $reason)"/>

      <spinque:relation subject="{$reasonID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://iisg.amsterdam/vocab/labourconflicts/Demand" />
      <spinque:attribute subject="{$reasonID}" attribute="https://schema.org/name" type="string" value="{$demand}" lang="nl"/>
      <spinque:attribute subject="{$reasonID}" attribute="https://schema.org/name" type="string" value="{$demand_en}" lang="en"/>
      <spinque:attribute subject="{$reasonID}" attribute="https://iisg.amsterdam/vocab/labourconflicts/mainDemand" type="string" value="{$main_reason}" lang="en"/>

      <spinque:relation subject="{$strikeID}" predicate="https://iisg.amsterdam/vocab/labourconflicts/demand" object="{$reasonID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForDemands">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>

  </xsl:template>

  <xsl:template name="templateForActions">
    <xsl:param name="strikeID" />
    <xsl:param name="index" select="1" />
    <xsl:param name="maxValue" select="4" />

    <xsl:variable name="andere_actie_nl" select="field[@name=concat('andere_actie_nl_', $index)]"/>
    <xsl:variable name="andere_actie_en" select="field[@name=concat('andere_actie_en_', $index)]"/>

    <xsl:if test="$andere_actie_en != ''">
      <xsl:variable name="andereactieID" select="su:uri('https://iisg.amsterdam/id/labourconflicts/', field[@name='id'], 'action', $andere_actie_en)"/>
      <spinque:relation subject="{$andereactieID}" predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" object="https://schema.org/Action" />
      <spinque:attribute subject="{$andereactieID}" attribute="https://schema.org/name" type="string" value="{$andere_actie_en}" lang="en" />
      <spinque:attribute subject="{$andereactieID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{$andere_actie_nl}" lang="nl" />
      <spinque:attribute subject="{$andereactieID}" attribute="http://www.w3.org/2000/01/rdf-schema#label" type="string" value="{$andere_actie_en}" lang="en" />
      <spinque:relation subject="{$strikeID}" predicate="https://iisg.amsterdam/vocab/labourconflicts/action" object="{$andereactieID}" />

      <xsl:if test="$index &lt; $maxValue">
        <xsl:call-template name="templateForActions">
          <xsl:with-param name="strikeID" select="$strikeID" />
          <xsl:with-param name="index" select="$index + 1" />
          <xsl:with-param name="total" select="$maxValue" />
        </xsl:call-template>
      </xsl:if>

    </xsl:if>
  </xsl:template>

</xsl:stylesheet>