<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:user="http://mycompany.com/mynamespace" xmlns:js="urn:the-xml-files:xslt-csharp" xmlns:ntd='http://www.ntd.ru/ntd/'>
  <xsl:output method="text" />

  <xsl:template match="text()"/>

  <xsl:template match="root/Dictionaries">
    DECLARE @dbid int

    <xsl:for-each select="Dictionary">
      exec @dbid = sp_GenerateIdForEntity 'DICTIONARY_BASE', 'PrimaryKey', 1
      INSERT INTO AKUZ.T_DICTIONARY_BASE (DictionaryID, Code, Name, DateAdd, type_)
      VALUES(@dbid, <xsl:value-of select="@Code" />, '<xsl:value-of select="@Name" />', GETDATE(), <xsl:value-of select="@Type" />)
    </xsl:for-each>
    GO
  </xsl:template>

  <xsl:template match="root/InsuranceCompanies">
    <xsl:for-each select="InsuranceCompany">
      -- Страх.организация
      DECLARE
      @orgID uniqueidentifier,
      @type int

      BEGIN
      SET @orgID = '<xsl:value-of select="@OrganizationID"/>'
      SELECT @type = EntityID FROM VCLib.T_ENTITIES
      WHERE EntName='INSURANCE_COMPANY' and [Schema]='AKUZ'

      INSERT INTO AKUZ.T_ORGANIZATION
      (OrganizationID, Name, ShortName, type_)
      VALUES
      (@orgID, '<xsl:value-of select="Name"/>', '<xsl:value-of select="ShortName"/>', @type)

      INSERT INTO AKUZ.T_INSURANCE_COMPANY
      (OrganizationID, Code, PolicyTemplate)
      VALUES
      (@orgID, '<xsl:value-of select="@Code"/>'
      ,
      <xsl:choose>
        <xsl:when test="string(@PolicyTemplate)">
          '<xsl:value-of select="@PolicyTemplate"/>'
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>)

      END
      GO
    </xsl:for-each>
  </xsl:template>

  <!-- V009_RESULT: V009.xml -->
  <xsl:template match="packet[zglv[type='Rezult']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="RezultZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V009 "<zap>" template -->
  <xsl:template name="RezultZap" >
    if not exists(select * from [AKUZ].[T_V009_RESULT] r where r.[IDRMP] = <xsl:value-of select="IDRMP" />)
    INSERT INTO [AKUZ].[T_V009_RESULT] ([IDRMP], [RMPNAME], [DL_USLOV], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDRMP" />
    , '<xsl:value-of select="RMPNAME" />'
    , <xsl:value-of select="DL_USLOV" />
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update r
    set [RMPNAME] = '<xsl:value-of select="RMPNAME" />'
    , [DL_USLOV]= <xsl:value-of select="DL_USLOV" />
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V009_RESULT] r
    where r.[IDRMP] = <xsl:value-of select="IDRMP" />
    GO
  </xsl:template>

  <!-- V020_STRUCTURE_BED: V020.xml -->
  <xsl:template match="packet[zglv[type='KoPr']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="KoPrZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V020 "<zap>" template -->
  <xsl:template name="KoPrZap" >
    if not exists(select * from [AKUZ].[T_V020_STRUCTURE_BED] r where r.[IDK_PR] = <xsl:value-of select="IDK_PR" />)
    INSERT INTO [AKUZ].[T_V020_STRUCTURE_BED] ([IDK_PR], [K_PRNAME], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDK_PR" />
    , '<xsl:value-of select="K_PRNAME" />'
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)</xsl:if>
	  <xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update r
    set [K_PRNAME] = '<xsl:value-of select="K_PRNAME" />'
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V020_STRUCTURE_BED] r
    where r.[IDK_PR] = <xsl:value-of select="IDK_PR" />
    GO
  </xsl:template>

  <!-- V021_SPECIALITY: V021.xml -->
  <xsl:template match="packet[zglv[type='MedSpec']]">
    <xsl:for-each select="zap">
      <!-- insert or update V021_SPECIALITY -->
      <xsl:call-template name="MedSpecZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V021 "<zap>" template -->
  <xsl:template name="MedSpecZap" >
    if not exists(select * from [AKUZ].[T_V021_SPECIALITY] s where s.[Id] = <xsl:value-of select="IDSPEC" /> and s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V021_SPECIALITY] ([Id], [Name], [DateBegin], [DateEnd], [V021SpecialityID], [POSTNAME], [IDPOST_MZ])
    SELECT <xsl:value-of select="IDSPEC" />
    , '<xsl:value-of select="SPECNAME" />'
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    ,NEWID()
    ,<xsl:if test="POSTNAME != ''">
      <xsl:text>'</xsl:text><xsl:value-of select="POSTNAME" /><xsl:text>'</xsl:text>
    </xsl:if>
    <xsl:if test="POSTNAME = ''">NULL</xsl:if>
    ,<xsl:if test="IDPOST_MZ != ''">
      <xsl:value-of select="IDPOST_MZ" />
    </xsl:if>
    <xsl:if test="IDPOST_MZ = ''">NULL</xsl:if>
    else
    update s
    set s.[Name] = '<xsl:value-of select="SPECNAME" />'
    , s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , s.[DateEnd] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    ,s.[POSTNAME] = <xsl:if test="POSTNAME != ''">
      <xsl:text>'</xsl:text><xsl:value-of select="POSTNAME" /><xsl:text>'</xsl:text>
    </xsl:if>
    <xsl:if test="POSTNAME = ''">NULL</xsl:if>
    ,s.[IDPOST_MZ] = <xsl:if test="IDPOST_MZ != ''">
      <xsl:value-of select="IDPOST_MZ" />
    </xsl:if>
    <xsl:if test="IDPOST_MZ = ''">NULL</xsl:if>
    from [AKUZ].[T_V021_SPECIALITY] s
    where s.[Id] = <xsl:value-of select="IDSPEC" /> and s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V016_DISP_TYPE: V016.xml -->
  <xsl:template match="packet[zglv[type='DispT']]">
    <xsl:for-each select="zap">
      <!-- insert or update V016_DISP_TYPE -->
      <xsl:call-template name="DispTZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V016 "<zap>" template -->
  <xsl:template name="DispTZap" >
    if not exists(select * from [AKUZ].[T_V016_DISP_TYPE] s where s.[IDDT] = LEFT('<xsl:value-of select="IDDT" />', 3)
	and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V016_DISP_TYPE] ([IDDT], [DTNAME], [DATEBEG], [DATEEND], [DTRULE])
    SELECT LEFT('<xsl:value-of select="IDDT" />', 3)
    , LEFT('<xsl:value-of select="DTNAME" />', 256)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>;</xsl:text>
    </xsl:for-each>'
    else
    update s
    set s.[DTNAME] = LEFT('<xsl:value-of select="DTNAME" />', 256)
    , s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , s.[DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , s.[DTRULE] = '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>;</xsl:text>
    </xsl:for-each>'
    from [AKUZ].[T_V016_DISP_TYPE] s
    where s.[IDDT] = LEFT('<xsl:value-of select="IDDT" />', 3)
    and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>
	
  <!-- V017_DISP_RESULT: V017.xml -->
  <xsl:template match="packet[zglv[type='DispR']]">
    <xsl:for-each select="zap">
      <!-- insert or update V017_DISP_RESULT -->
      <xsl:call-template name="DispR" />
    </xsl:for-each>
  </xsl:template>
  <!-- V017 "<zap>" template -->
  <xsl:template name="DispR" >
    if not exists(select * from [AKUZ].[T_V017_DISP_RESULT] s where s.[IDDR] = <xsl:value-of select="IDDR" /> and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V017_DISP_RESULT] ([IDDR], [DRNAME], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDDR" />
    , LEFT('<xsl:value-of select="DRNAME" />', 256)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update s
    set s.[DRNAME] = LEFT('<xsl:value-of select="DRNAME" />', 256)
    , s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , s.[DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V017_DISP_RESULT] s
    where s.[IDDR] = <xsl:value-of select="IDDR" /> and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- VMP_SERVICE: V019.xml elements -->
  <xsl:template match="packet[(zglv[type='HMet'])]">
    <!-- Create MedicalServiceType -->
    if not exists(select * from [AKUZ].[T_MEDICAL_SERVICE_TYPE] ms where ms.[MedicalServiceTypeID] = '642809B3-0C46-4673-8E97-76A6D60C14F8')
    INSERT INTO [AKUZ].[T_MEDICAL_SERVICE_TYPE] ([MedicalServiceTypeID], [Name])
    SELECT '642809B3-0C46-4673-8E97-76A6D60C14F8', 'ВМП(V019)'
    GO

    <xsl:for-each select="zap">

      <!-- clear MedicalServiceMkbLinks -->
      delete msml
      from [AKUZ].[T_MEDICAL_SERVICE_MKB_LINK] msml
      join [AKUZ].[T_MEDICAL_SERVICE] ms on ms.MedicalServiceID = msml.MedicalService
      where ms.[type_] = 1113 and ms.[MedicalServiceType] = '642809B3-0C46-4673-8E97-76A6D60C14F8'
      and ms.[Code] = '<xsl:value-of select="IDHM" />'
      and ms.[EconomicCode] = '<xsl:value-of select="HVID" />'
      and (ms.[PatientModelCode] = '<xsl:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
      and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
      GO

    </xsl:for-each>

    <xsl:for-each select="zap">

      <!-- insert or update MedicalService -->
      <xsl:call-template name="HMetZapElements" />

      <!-- insert new MedicalServiceMkbLinks -->
      <xsl:call-template name="HMetDiagnosisElements" />

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

  <!-- V019 "<zap>" template -->
  <xsl:template name="HMetZapElements" >
    if not exists(select * from [AKUZ].[T_MEDICAL_SERVICE] ms where ms.[type_] = 1113 and ms.[MedicalServiceType] = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.[Code] = '<xsl:value-of select="IDHM" />'
    and ms.[EconomicCode] = '<xsl:value-of select="HVID" />'
    and (ms.[PatientModelCode] = '<xsl:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
    and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_MEDICAL_SERVICE] ([Code], [Name], [NameFull], [Profile], [MedicalServiceID], [type_], [MedicalServiceType], [ShortName], [PatientModelCode], [EconomicCode], [InternalCode], [PayType], [DateBegin], [DateEnd], [IsCommonService])
    SELECT '<xsl:value-of select="IDHM" />'
    , LEFT('<xsl:value-of select="HMNAME" />', 256)
    , LEFT('<xsl:value-of select="HMNAME" />', 256)
    , 3
    , NEWID()
    , 1113
    , '642809B3-0C46-4673-8E97-76A6D60C14F8'
    , LEFT('<xsl:value-of select="HMNAME" />', 24)
    , '<xsl:value-of select="IDMODP" />'
    , '<xsl:value-of select="HVID" />'
    , '<xsl:value-of select="HGR" />'
    , 1
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , 0
    else
    update ms
    set [Name] = LEFT('<xsl:value-of select="HMNAME" />', 256)
    , [NameFull] = LEFT('<xsl:value-of select="HMNAME" />', 256)
    , [ShortName] = LEFT('<xsl:value-of select="HMNAME" />', 24)
    , [PatientModelCode] = '<xsl:value-of select="IDMODP" />'
    , [InternalCode] = '<xsl:value-of select="HGR" />'
    , [DateEnd] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_MEDICAL_SERVICE] ms
    where ms.[type_] = 1113 and ms.[MedicalServiceType] = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.[Code] = '<xsl:value-of select="IDHM" />'
    and ms.[EconomicCode] = '<xsl:value-of select="HVID" />'
    and (ms.[PatientModelCode] = '<xsl:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
    and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V019 "DIAG" template -->
  <xsl:template name="HMetDiagnosisElements" >
    INSERT INTO AKUZ.T_MEDICAL_SERVICE_MKB_LINK (MedicalServiceMkbLinkID, MedicalService, MkbCode)
    SELECT
    NEWID() as MedicalServiceMkbLinkID
    , ms.MedicalServiceID
    , MkbCode.Split as MkbCode
    from [AKUZ].[T_MEDICAL_SERVICE] ms
    cross apply dbo.fn_Split(REPLACE(REPLACE('<xsl:value-of select="DIAG" />',' ',''),',',';'), ';') as MkbCode
    where ms.[type_] = 1113 and ms.[MedicalServiceType] = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.[Code] = '<xsl:value-of select="IDHM" />'
    and ms.[EconomicCode] = '<xsl:value-of select="HVID" />'
    and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    and not exists
    (
    select top 1 MedicalServiceMkbLinkID
    from AKUZ.T_MEDICAL_SERVICE_MKB_LINK msl
    where ms.MedicalServiceID = msl.MedicalService
    and MkbCode.Split = msl.MkbCode
    )
    GO
  </xsl:template>
	
  <!-- V002_PROFILE: V002.xml -->
  <xsl:template match="packet[zglv[type='ProfOt']]">
    <xsl:for-each select="zap">
      <!-- insert or update MedicalService -->
      <xsl:call-template name="ProfOtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V002 "<zap>" template -->
  <xsl:template name="ProfOtZap">
    if not exists(select * from [AKUZ].[T_V002_PROFILE] p where p.[Id] = <xsl:value-of select="IDPR" /> and p.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V002_PROFILE] ([Id], [Name], [DateBegin], [DateEnd])
    SELECT <xsl:value-of select="IDPR" />
    , LEFT('<xsl:value-of select="PRNAME" />', 256)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set [Name] = LEFT('<xsl:value-of select="PRNAME" />', 256)
    , [DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [DateEnd] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V002_PROFILE] p
    where p.[Id] = <xsl:value-of select="IDPR" /> and p.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
	  GO
  </xsl:template>

  <!-- V011_DRUG: V011.xml -->
  <xsl:template match="book[(entries/zglv[type='V011_Drugs'])]">
    <xsl:for-each select="entry">
      <xsl:call-template name="Drugs" />
    </xsl:for-each>
  </xsl:template>
  <!-- V011 "<zap>" template -->
  <xsl:template name="Drugs" >
    if not exists(select * from [AKUZ].[T_V011_DRUG] p where p.[ID] = <xsl:value-of select="ID" />)
    INSERT INTO [AKUZ].[T_V011_DRUG] ([ID], [S_NUM_REG], [DATE_REG], [DATE_END_REG], [S_ORG_NAME], [S_ORG_COUNTRY], [S_TORG_NAME], [S_MNN], [S_FORMS], [S_PROD_PHASE], [S_DOCS], [S_FARM_GROUP], [S_CODES])
    SELECT <xsl:value-of select="ID" />
    , LEFT('<xsl:value-of select="S_NUM_REG" />', 50)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , LEFT('<xsl:value-of select="S_ORG_NAME" />', 255)
    , LEFT('<xsl:value-of select="S_ORG_COUNTRY" />', 255)
    , LEFT('<xsl:value-of select="S_TORG_NAME" />', 255)
    , LEFT('<xsl:value-of select="S_MNN" />', 255)
    , LEFT('<xsl:value-of select="S_FORMS" />', 255)
    , LEFT('<xsl:value-of select="S_PROD_PHASE" />', 255)
    , LEFT('<xsl:value-of select="S_DOCS" />', 255)
    , LEFT('<xsl:value-of select="S_FARM_GROUP" />', 255)
    , LEFT('<xsl:value-of select="S_CODES" />', 255)
    else
    update p
    set [S_NUM_REG] = LEFT('<xsl:value-of select="S_NUM_REG" />', 50)
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , [S_ORG_NAME] = LEFT('<xsl:value-of select="S_ORG_NAME" />', 255)
    , [S_ORG_COUNTRY] = LEFT('<xsl:value-of select="S_ORG_COUNTRY" />', 255)
    , [S_TORG_NAME] = LEFT('<xsl:value-of select="S_TORG_NAME" />', 255)
    , [S_MNN] = LEFT('<xsl:value-of select="S_MNN" />', 255)
    , [S_FORMS] = LEFT('<xsl:value-of select="S_FORMS" />', 255)
    , [S_PROD_PHASE] = LEFT('<xsl:value-of select="S_PROD_PHASE" />', 255)
    , [S_DOCS] = LEFT('<xsl:value-of select="S_DOCS" />', 255)
    , [S_FARM_GROUP] = LEFT('<xsl:value-of select="S_FARM_GROUP" />', 255)
    , [S_CODES] = LEFT('<xsl:value-of select="S_CODES" />', 255)
    from [AKUZ].[T_V011_DRUG] p
    where p.[ID] = <xsl:value-of select="ID" />
    GO
  </xsl:template>

  <!-- V024_DopKr: V024.xml -->
  <xsl:template match="packet[zglv[type='DopKr']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="DopKrZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V024 "<zap>" template -->
  <xsl:template name="DopKrZap" >
    if not exists (select * from [AKUZ].[T_V024] p where p.[IDDKK] = LEFT('<xsl:value-of select="IDDKK" />', 255)
    and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
    INSERT INTO [AKUZ].[T_V024] ([V024DoprKrID], [IDDKK], [DKKNAME], [DATEBEG], [DATEEND])    
    SELECT NEWID()
    , LEFT('<xsl:value-of select="IDDKK" />', 255)
    , LEFT('<xsl:value-of select="DKKNAME" />', 500)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set <xsl:if test="DKKNAME != ''">
      <xsl:text>p.[DKKNAME] = LEFT('</xsl:text><xsl:value-of select="DKKNAME" /><xsl:text>', 500),</xsl:text>
    </xsl:if>
    p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104),
    p.[DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V024] p
    where p.[IDDKK] = LEFT('<xsl:value-of select="IDDKK" />', 255) and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
    GO
  </xsl:template>

  <!-- V028_RefType: V028.xml -->
  <xsl:template match="packet[zglv[type='NAPR_V']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="NAPR_VZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V028 "<zap>" template -->
  <xsl:template name="NAPR_VZap">
    if not exists(select * from [AKUZ].[T_V028_REFERRAL_TYPE] p where p.[IDVN] = <xsl:value-of select="IDVN" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V028_REFERRAL_TYPE] ([IDVN], [N_VN], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDVN" />
    , LEFT('<xsl:value-of select="N_VN" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set <xsl:if test="N_VN != ''">
      <xsl:text>[N_VN] = LEFT('</xsl:text><xsl:value-of select="N_VN" /><xsl:text>', 255),</xsl:text>
    </xsl:if>
    [DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V028_REFERRAL_TYPE] p
    where p.[IDVN] = <xsl:value-of select="IDVN" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V029_ResMet: V029.xml -->
  <xsl:template match="packet[zglv[type='MET_ISSL']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="MET_ISSLZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V029 "<zap>" template -->
  <xsl:template name="MET_ISSLZap">
    if not exists(select * from [AKUZ].[T_V029_RESEARCH_METHOD] p where p.[IDMET] = <xsl:value-of select="IDMET" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V029_RESEARCH_METHOD] ([IDMET], [N_MET], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDMET" />
    , LEFT('<xsl:value-of select="N_MET" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set <xsl:if test="N_MET != ''">
      <xsl:text>[N_MET] = LEFT('</xsl:text><xsl:value-of select="N_MET" /><xsl:text>', 255),</xsl:text>
    </xsl:if>
    [DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V029_RESEARCH_METHOD] p
    where p.[IDMET] = <xsl:value-of select="IDMET" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V031_GroupDrug: V031.xml -->
  <xsl:template match="packet[zglv[type='GroupDrug']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="GroupDrugZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V031 "<zap>" template -->
  <xsl:template name="GroupDrugZap">
    if not exists(
    select * from [AKUZ].[T_V031_GROUP_DRUG] p
    where p.[DrugGroupCode] = <xsl:value-of select="DrugGroupCode" />
    and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V031_GROUP_DRUG] ([V031GroupDrugID], [DrugGroupCode], [ManIndMNN], [DrugGroup], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="DrugGroupCode" />
    , <xsl:if test="ManIndMNN != ''"><xsl:value-of select="ManIndMNN" />
    </xsl:if><xsl:if test="ManIndMNN = ''">NULL</xsl:if>
    , LEFT('<xsl:value-of select="DrugGroup" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p    
    set 
    p.[ManIndMNN] = <xsl:if test="ManIndMNN != ''"><xsl:value-of select="ManIndMNN" /></xsl:if>
	  <xsl:if test="ManIndMNN = ''">NULL</xsl:if>
    , <xsl:if test="DrugGroup != ''">
      <xsl:text>p.[DrugGroup] = LEFT('</xsl:text><xsl:value-of select="DrugGroup" /><xsl:text>', 255)</xsl:text>
    </xsl:if>
    , p.[DATEEND] = <xsl:if test="DATEEND != ''">
        <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
        </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V031_GROUP_DRUG] p
    where p.[DrugGroupCode] = <xsl:value-of select="DrugGroupCode" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V032_DrugGrReg: V032.xml -->
  <xsl:template match="packet[zglv[type='DrugGrReg']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="DrugGrRegZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V032 "<zap>" template -->
  <xsl:template name="DrugGrRegZap">
    if not exists(
    select * from [AKUZ].[T_V032_DRUG_GR_REG] p
    where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
    and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
    INSERT INTO [AKUZ].[T_V032_DRUG_GR_REG] ([V032DrugGrRegID], [ScheDrugGrCd], [Name], [SchemCode], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
    , LEFT('<xsl:value-of select="Name" />', 255)
    , LEFT('<xsl:value-of select="SchemCode" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set p.[Name] = <xsl:choose>
		<xsl:when test="Name != '' and string-length(Name) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="Name" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , p.[SchemCode] = <xsl:choose>
		<xsl:when test="SchemCode != '' and string-length(SchemCode) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="SchemCode" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , p.[DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_V032_DRUG_GR_REG] p
    where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255) and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
    GO
  </xsl:template>

	<!-- V033_SchemCodDrug: V033.xml -->
	<xsl:template match="packet[zglv[type='SchemCodDrug']]">
		<xsl:for-each select="zap">
			<xsl:call-template name="SchemCodDrugZap" />
		</xsl:for-each>
	</xsl:template>
	<!-- V033 "<zap>" template -->
	<xsl:template name="SchemCodDrugZap" >
		if not exists(
		select * from [AKUZ].[T_V033_SCHEM_COD_DRUG] p
		where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		and p.[DrugCode] = LEFT('<xsl:value-of select="DrugCode" />', 255)
		and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
		INSERT INTO [AKUZ].[T_V033_SCHEM_COD_DRUG] ([V033SchemCodDrugID], [ScheDrugGrCd], [DrugCode], [DATEBEG], [DATEEND])
		SELECT NEWID()
		, LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		, LEFT('<xsl:value-of select="DrugCode" />', 255)
		, CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, <xsl:if test="DATEEND != ''">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		else
		update p
		set p.[DATEEND] = <xsl:choose>
			<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
				<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		from [AKUZ].[T_V033_SCHEM_COD_DRUG] p
		where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		and p.[DrugCode] = LEFT('<xsl:value-of select="DrugCode" />', 255)
		and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		GO
	</xsl:template>

  <!-- V022_ModPac: V022.xml -->
  <xsl:template match="packet[zglv[type='ModPac']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="ModPacZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V022 "<zap>" template -->
  <xsl:template name="ModPacZap" >
    if not exists(select * from [AKUZ].[T_V022_PATIENT_MODEL] p where p.[IDMPAC] = <xsl:value-of select="IDMPAC" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V022_PATIENT_MODEL] ([V022PatientModelID], [IDMPAC], [MPACNAME], [DATEBEG], [DATEEND])
    SELECT newid()
    , <xsl:value-of select="IDMPAC" />
    , <xsl:choose>
        <xsl:when test="normalize-space(MPACNAME) != ''">
          '<xsl:value-of select="MPACNAME"/>'
        </xsl:when>
        <xsl:otherwise>''</xsl:otherwise>
    </xsl:choose>
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set p.[MPACNAME] = <xsl:choose>
      <xsl:when test="normalize-space(MPACNAME) != ''">
        '<xsl:value-of select="MPACNAME"/>'
      </xsl:when>
      <xsl:otherwise>''</xsl:otherwise>
    </xsl:choose>
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V022_PATIENT_MODEL] p
    where p.[IDMPAC] = <xsl:value-of select="IDMPAC" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N001_OnkPrOt: N001.xml -->
  <xsl:template match="packet[zglv[type='OnkPrOt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkPrOtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N001 "<zap>" template -->
  <xsl:template name="OnkPrOtZap" >
    if not exists(select * from [AKUZ].[T_N001_OnkPrOt] p where p.[ID_PrOt] = <xsl:value-of select="ID_PrOt" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N001_OnkPrOt] ([N001ID], [ID_PrOt], [PrOt_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_PrOt" />
    , LEFT('<xsl:value-of select="PrOt_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set [PrOt_NAME] = <xsl:choose>
		<xsl:when test="PrOt_NAME != '' and string-length(PrOt_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="PrOt_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N001_OnkPrOt] p
    where p.[ID_PrOt] = <xsl:value-of select="ID_PrOt" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N002_OnkStad: N002.xml -->
  <xsl:template match="packet[zglv[type='OnkStad']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkStadZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N002 "<zap>" template -->
  <xsl:template name="OnkStadZap">
    if not exists(select * from [AKUZ].[T_N002_OnkStad] p where p.[ID_St] = <xsl:value-of select="ID_St" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N002_OnkStad] ([N002ID], [ID_St], [DS_St], [KOD_St], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_St" />
    , LEFT('<xsl:value-of select="DS_St" />', 5)
    , LEFT('<xsl:value-of select="KOD_St" />', 5)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set [DS_St] = LEFT('<xsl:value-of select="DS_St" />', 5)
    , [KOD_St] = LEFT('<xsl:value-of select="KOD_St" />', 5)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N002_OnkStad] p
    where p.[ID_St] = <xsl:value-of select="ID_St" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N003_OnkT: N003.xml -->
  <xsl:template match="packet[zglv[type='OnkT']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkTZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N003 "<zap>" template -->
  <xsl:template name="OnkTZap">
    if not exists(select * from [AKUZ].[T_N003_OnkT] p where p.[ID_T] = <xsl:value-of select="ID_T" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N003_OnkT] ([N003ID], [ID_T], [DS_T], [KOD_T], [T_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_T" />
    , LEFT('<xsl:value-of select="DS_T" />', 5)
    , LEFT('<xsl:value-of select="KOD_T" />', 255)
    , <xsl:choose>
		<xsl:when test="T_NAME != '' and string-length(T_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="T_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set [DS_T] = LEFT('<xsl:value-of select="DS_T" />', 5)
    , [KOD_T] = LEFT('<xsl:value-of select="KOD_T" />', 255)
    , [T_NAME] = <xsl:choose>
		<xsl:when test="T_NAME != '' and string-length(T_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="T_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N003_OnkT] p
    where p.[ID_T] = <xsl:value-of select="ID_T" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N004_OnkN: N004.xml -->
  <xsl:template match="packet[zglv[type='OnkN']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkNZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N004 "<zap>" template -->
  <xsl:template name="OnkNZap">
    if not exists(select * from [AKUZ].[T_N004_OnkN] p where p.[ID_N] = <xsl:value-of select="ID_N" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N004_OnkN] ([N004ID], [ID_N], [DS_N], [KOD_N], [N_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_N" />
    , <xsl:choose>
		<xsl:when test="DS_N != '' and string-length(DS_N) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="DS_N" /><xsl:text>', 5)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , LEFT('<xsl:value-of select="KOD_N" />', 5)
    , <xsl:choose>
		<xsl:when test="N_NAME != '' and string-length(N_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="N_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set [DS_N] = <xsl:choose>
		<xsl:when test="DS_N != '' and string-length(DS_N) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="DS_N" /><xsl:text>', 5)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [KOD_N] = LEFT('<xsl:value-of select="KOD_N" />', 5)
    , [N_NAME] = <xsl:choose>
		<xsl:when test="N_NAME != '' and string-length(N_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="N_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      <xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_N004_OnkN] p
    where p.[ID_N] = <xsl:value-of select="ID_N" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N005_OnkM: N005.xml -->
  <xsl:template match="packet[zglv[type='OnkM']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N005 "<zap>" template -->
  <xsl:template name="OnkMZap" >
    if not exists(select * from [AKUZ].[T_N005_OnkM] p where p.[ID_M] = <xsl:value-of select="ID_M" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N005_OnkM] ([N005ID], [ID_M], [DS_M], [KOD_M], [M_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_M" />
    , <xsl:choose>
		<xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="DS_M" /><xsl:text>', 5)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , LEFT('<xsl:value-of select="KOD_M" />', 5)
    , <xsl:choose>
		<xsl:when test="M_NAME != '' and string-length(M_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="M_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [DS_M] = <xsl:choose>
		<xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="DS_M" /><xsl:text>', 5)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [KOD_M] = LEFT('<xsl:value-of select="KOD_M" />', 5)
    , [M_NAME] = <xsl:choose>
		<xsl:when test="M_NAME != '' and string-length(M_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="M_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N005_OnkM] p
    where p.[ID_M] = <xsl:value-of select="ID_M" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N006_OnkTNM: N006.xml -->
  <xsl:template match="packet[zglv[type='OnkTNM']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkTNMZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N006 "<zap>" template -->
  <xsl:template name="OnkTNMZap" >
    if not exists(select * from [AKUZ].[T_N006_OnkTNM] p where p.[ID_gr] = <xsl:value-of select="ID_gr" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N006_OnkTNM] ([N006ID], [ID_gr], [DS_gr], [ID_St], [ID_T], [ID_N], [ID_M], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_gr" />
    , LEFT('<xsl:value-of select="DS_gr" />', 5)
    , <xsl:value-of select="ID_St" />
    , <xsl:value-of select="ID_T" />
    , <xsl:value-of select="ID_N" />
    , <xsl:value-of select="ID_M" />
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [DS_gr] = LEFT('<xsl:value-of select="DS_gr" />', 5)
    , [ID_St] = <xsl:value-of select="ID_St" />
    , [ID_T] = <xsl:value-of select="ID_T" />
    , [ID_N] = <xsl:value-of select="ID_N" />
    , [ID_M] = <xsl:value-of select="ID_M" />
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N006_OnkTNM] p
    where p.[ID_gr] = <xsl:value-of select="ID_gr" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N007_OnkMrf: N007.xml -->
  <xsl:template match="packet[zglv[type='OnkMrf']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrfZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N007 "<zap>" template -->
  <xsl:template name="OnkMrfZap" >
    if not exists(select * from [AKUZ].[T_N007_OnkMrf] p where p.[ID_Mrf] = <xsl:value-of select="ID_Mrf" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N007_OnkMrf] ([N007ID], [ID_Mrf], [Mrf_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_Mrf" />
    , LEFT('<xsl:value-of select="Mrf_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [Mrf_NAME] = LEFT('<xsl:value-of select="Mrf_NAME" />', 255)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N007_OnkMrf] p
    where p.[ID_Mrf] = <xsl:value-of select="ID_Mrf" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N008_OnkMrfRt: N008.xml -->
  <xsl:template match="packet[zglv[type='OnkMrfRt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrfRtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N008 "<zap>" template -->
  <xsl:template name="OnkMrfRtZap">
    if not exists(select * from [AKUZ].[T_N008_OnkMrfRt] p where p.[ID_R_M] = <xsl:value-of select="ID_R_M" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N008_OnkMrfRt] ([N008ID], [ID_R_M], [ID_Mrf], [R_M_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_R_M" />
    , <xsl:value-of select="ID_Mrf" />
    , LEFT('<xsl:value-of select="R_M_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [ID_Mrf] = <xsl:value-of select="ID_Mrf" />
    , [R_M_NAME] = LEFT('<xsl:value-of select="R_M_NAME" />', 255)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N008_OnkMrfRt] p
    where p.[ID_R_M] = <xsl:value-of select="ID_R_M" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N009_OnkMrtDS: N009.xml -->
  <xsl:template match="packet[zglv[type='OnkMrtDS']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrtDSZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N009 "<zap>" template -->
  <xsl:template name="OnkMrtDSZap" >
    if not exists(select * from [AKUZ].[T_N009_OnkMrtDS] p where p.[ID_M_D] = <xsl:value-of select="ID_M_D" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N009_OnkMrtDS] ([N009ID], [ID_M_D], [DS_Mrf], [ID_Mrf], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_M_D" />
    , LEFT('<xsl:value-of select="DS_Mrf" />', 5)
    , <xsl:value-of select="ID_Mrf" />
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [DS_Mrf] = LEFT('<xsl:value-of select="DS_Mrf" />', 5)
    , [ID_Mrf] = <xsl:value-of select="ID_Mrf" />
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N009_OnkMrtDS] p
    where p.[ID_M_D] = <xsl:value-of select="ID_M_D" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N010_OnkIgh: N010.xml -->
  <xsl:template match="packet[zglv[type='OnkIgh']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N010 "<zap>" template -->
  <xsl:template name="OnkIghZap">
    if not exists(select * from [AKUZ].[T_N010_OnkIgh] p where p.[ID_Igh] = <xsl:value-of select="ID_Igh" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N010_OnkIgh] ([N010ID], [ID_Igh], [KOD_Igh], [Igh_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="KOD_Igh" />', 255)
    , LEFT('<xsl:value-of select="Igh_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [KOD_Igh] = LEFT('<xsl:value-of select="KOD_Igh" />', 255)
    , [Igh_NAME] = LEFT('<xsl:value-of select="Igh_NAME" />', 255)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N010_OnkIgh] p
    where p.[ID_Igh] = <xsl:value-of select="ID_Igh" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N011_OnkIghRt: N011.xml -->
  <xsl:template match="packet[zglv[type='OnkIghRt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghRtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N011 "<zap>" template -->
  <xsl:template name="OnkIghRtZap" >
    if not exists(select * from [AKUZ].[T_N011_OnkIghRt] p where p.[ID_R_I] = <xsl:value-of select="ID_R_I" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N011_OnkIghRt] ([N011ID], [ID_R_I], [ID_Igh], [KOD_R_I], [R_I_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_R_I" />
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="KOD_R_I" />', 255)
    , LEFT('<xsl:value-of select="R_I_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [ID_Igh] = <xsl:value-of select="ID_Igh" />
    , [KOD_R_I] = LEFT('<xsl:value-of select="KOD_R_I" />', 255)
    , [R_I_NAME] = <xsl:if test="R_I_NAME != ''">
		<xsl:text>LEFT('</xsl:text><xsl:value-of select="R_I_NAME" /><xsl:text>', 255)</xsl:text>
    </xsl:if>
	<xsl:if test="R_I_NAME = ''">NULL</xsl:if>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N011_OnkIghRt] p
    where p.[ID_R_I] = <xsl:value-of select="ID_R_I" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N012_OnkIghDS: N012.xml -->
  <xsl:template match="packet[zglv[type='OnkIghDS']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghDSZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N012 "<zap>" template -->
  <xsl:template name="OnkIghDSZap">
    if not exists(select * from [AKUZ].[T_N012_OnkIghDS] p where p.[ID_I_D] = <xsl:value-of select="ID_I_D" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N012_OnkIghDS] ([N012ID], [ID_I_D], [ID_Igh], [DS_Igh], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_I_D" />
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="DS_Igh" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [ID_Igh] = <xsl:value-of select="ID_Igh" />
    , [DS_Igh] = LEFT('<xsl:value-of select="DS_Igh" />', 255)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N012_OnkIghDS] p
    where p.[ID_I_D] = <xsl:value-of select="ID_I_D" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N013_OnkLech: N013.xml -->
  <xsl:template match="packet[zglv[type='OnkLech']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLechZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N013 "<zap>" template -->
  <xsl:template name="OnkLechZap" >
    if not exists(select * from [AKUZ].[T_N013_OnkLech] p where p.[ID_TLech] = <xsl:value-of select="ID_TLech" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N013_OnkLech] ([N013ID], [ID_TLech], [TLech_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLech" />
    , LEFT('<xsl:value-of select="TLech_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [TLech_NAME] = <xsl:choose>
		<xsl:when test="TLech_NAME != '' and string-length(TLech_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="TLech_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N013_OnkLech] p
    where p.[ID_TLech] = <xsl:value-of select="ID_TLech" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N014_OnkHir: N014.xml -->
  <xsl:template match="packet[zglv[type='OnkHir']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkHirZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N014 "<zap>" template -->
  <xsl:template name="OnkHirZap">
    if not exists(select * from [AKUZ].[T_N014_OnkHir] p where p.[ID_THir] = <xsl:value-of select="ID_THir" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N014_OnkHir] ([N014ID], [ID_THir], [THir_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_THir" />
    , LEFT('<xsl:value-of select="THir_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [THir_NAME] = <xsl:choose>
		<xsl:when test="THir_NAME != '' and string-length(THir_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="THir_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N014_OnkHir] p
    where p.[ID_THir] = <xsl:value-of select="ID_THir" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N015_OnkLek_L: N015.xml -->
  <xsl:template match="packet[zglv[type='OnkLek_L']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLek_LZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N015 "<zap>" template -->
  <xsl:template name="OnkLek_LZap">
    if not exists(select * from [AKUZ].[T_N015_OnkLek_L] p where p.[ID_TLek_L] = <xsl:value-of select="ID_TLek_L" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N015_OnkLek_L] ([N015ID], [ID_TLek_L], [TLek_NAME_L], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLek_L" />
    , LEFT('<xsl:value-of select="TLek_NAME_L" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [TLek_NAME_L] = <xsl:choose>
		<xsl:when test="TLek_NAME_L != '' and string-length(TLek_NAME_L) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="TLek_NAME_L" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N015_OnkLek_L] p
    where p.[ID_TLek_L] = <xsl:value-of select="ID_TLek_L" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N016_OnkLek_V: N016.xml -->
  <xsl:template match="packet[zglv[type='OnkLek_V']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLek_VZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N016 "<zap>" template -->
  <xsl:template name="OnkLek_VZap" >
    if not exists(select * from [AKUZ].[T_N016_OnkLek_V] p where p.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N016_OnkLek_V] ([N016ID], [ID_TLek_V], [TLek_NAME_V], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLek_V" />
    , LEFT('<xsl:value-of select="TLek_NAME_V" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [TLek_NAME_V] = <xsl:choose>
		<xsl:when test="TLek_NAME_V != '' and string-length(TLek_NAME_V) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="TLek_NAME_V" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N016_OnkLek_V] p
    where p.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N017_OnkLuch: N017.xml -->
  <xsl:template match="packet[zglv[type='OnkLuch']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLuchZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N017 "<zap>" template -->
  <xsl:template name="OnkLuchZap">
    if not exists(select * from [AKUZ].[T_N017_OnkLuch] p where p.[ID_TLuch] = <xsl:value-of select="ID_TLuch" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N017_OnkLuch] ([N017ID], [ID_TLuch], [TLuch_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLuch" />
    , LEFT('<xsl:value-of select="TLuch_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [TLuch_NAME] = <xsl:choose>
		<xsl:when test="TLuch_NAME != '' and string-length(TLuch_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="TLuch_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N017_OnkLuch] p
    where p.[ID_TLuch] = <xsl:value-of select="ID_TLuch" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N018_OnkReas: N018.xml -->
  <xsl:template match="packet[zglv[type='']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkReasZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N018 "<zap>" template -->
  <xsl:template name="OnkReasZap">
    if not exists(select * from [AKUZ].[T_N018_OnkReas] p where p.[ID_REAS] = <xsl:value-of select="ID_REAS" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N018_OnkReas] ([N018ID], [ID_REAS], [REAS_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_REAS" />
    , LEFT('<xsl:value-of select="REAS_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [REAS_NAME] = <xsl:choose>
		<xsl:when test="REAS_NAME != '' and string-length(REAS_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="REAS_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N018_OnkReas] p
    where p.[ID_REAS] = <xsl:value-of select="ID_REAS" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N019_OnkCons: N019.xml -->
  <xsl:template match="packet[zglv[type='OnkCons']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkConsZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N019 "<zap>" template -->
  <xsl:template name="OnkConsZap">
    if not exists(select * from [AKUZ].[T_N019_OnkCons] p where p.[ID_CONS] = <xsl:value-of select="ID_CONS" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N019_OnkCons] ([N019ID], [ID_CONS], [CONS_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_CONS" />
    , LEFT('<xsl:value-of select="CONS_NAME" />', 255)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [CONS_NAME] = <xsl:choose>
		<xsl:when test="CONS_NAME != '' and string-length(CONS_NAME) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="CONS_NAME" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N019_OnkCons] p
    where p.[ID_CONS] = <xsl:value-of select="ID_CONS" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N021_OnkLpsh: N021.xml -->
  <xsl:template match="packet[zglv[type='OnkLpsh']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLpshZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N021 "<zap>" template -->
  <xsl:template name="OnkLpshZap" >
    if not exists(select * from [AKUZ].[T_N021_OnkLpsh] p where p.[ID_ZAP] = '<xsl:value-of select="ID_ZAP" />' and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N021_OnkLpsh] ([N021ID], [ID_ZAP], [CODE_SH], [ID_LEKP], [DATEBEG], [DATEEND], [ID_LEKP_EXT], [LEKP_EXT])
    SELECT NEWID()
    , <xsl:value-of select="ID_ZAP" />
    , LEFT('<xsl:value-of select="CODE_SH" />', 10)
    , LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , <xsl:choose>
	  <xsl:when test="ID_LEKP_EXT != '' and string-length(ID_LEKP_EXT) &gt; 0">
		  <xsl:text>'</xsl:text><xsl:value-of select="ID_LEKP_EXT" /><xsl:text>'</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    , <xsl:choose>
	  <xsl:when test="LEKP_EXT != '' and string-length(LEKP_EXT) &gt; 0">
		  <xsl:text>'</xsl:text><xsl:value-of select="LEKP_EXT" /><xsl:text>'</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    else
    update p
    set [CODE_SH] = LEFT('<xsl:value-of select="CODE_SH" />', 10)
    , [ID_LEKP] = LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [ID_LEKP_EXT] = <xsl:choose>
	  <xsl:when test="ID_LEKP_EXT != '' and string-length(ID_LEKP_EXT) &gt; 0">
		  <xsl:text>'</xsl:text><xsl:value-of select="ID_LEKP_EXT" /><xsl:text>'</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    , [LEKP_EXT] = <xsl:choose>
	  <xsl:when test="LEKP_EXT != '' and string-length(LEKP_EXT) &gt; 0">
		  <xsl:text>'</xsl:text><xsl:value-of select="LEKP_EXT" /><xsl:text>'</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    from [AKUZ].[T_N021_OnkLpsh] p
    where p.[ID_ZAP] = LEFT('<xsl:value-of select="ID_ZAP" />', 15) and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N020_OnkLekp: N020.xml -->
  <xsl:template match="packet[zglv[type='OnkLekp']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLekpZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N020 "<zap>" template -->
  <xsl:template name="OnkLekpZap">
    if not exists(select * from [AKUZ].[T_N020_OnkLekp] p where p.[ID_LEKP] = '<xsl:value-of select="ID_LEKP" />' and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N020_OnkLekp] ([N020ID], [ID_LEKP], [MNN], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , LEFT('<xsl:value-of select="MNN" />', 300)
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    else
    update p
    set [MNN] = <xsl:choose>
		<xsl:when test="MNN != '' and string-length(MNN) &gt; 0">
			<xsl:text>LEFT('</xsl:text><xsl:value-of select="MNN" /><xsl:text>', 255)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    , [DATEEND] = <xsl:choose>
		<xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
			<xsl:text>CONVERT(datetime, '</xsl:text><xsl:value-of select="DATEEND" /><xsl:text>', 104)</xsl:text>
		</xsl:when>
		<xsl:otherwise>NULL</xsl:otherwise>
	</xsl:choose>
    from [AKUZ].[T_N020_OnkLekp] p
    where p.[ID_LEKP] = LEFT('<xsl:value-of select="ID_LEKP" />', 6) and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- CouncilGoals: N019.xml -->
  <xsl:template match="packet[(zglv[type='N019CouncilGoals'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="N019CouncilGoals" />
    </xsl:for-each>
  </xsl:template>
  <!-- N019 "<zap>" template -->
  <xsl:template name="N019CouncilGoals" >
    if not exists(select * from [AKUZ].[T_COUNCIL_GOAL] p
    where p.[Code] = <xsl:value-of select="ID_CONS" /> and p.[Type] = 1)
    INSERT INTO [AKUZ].[T_COUNCIL_GOAL] ([CouncilGoalID], [Code], [Name], [Type], [DateBegin], [DateEnd])
    SELECT
    NEWID()
    , <xsl:value-of select="ID_CONS" />
    , LEFT('<xsl:value-of select="CONS_NAME" />', 255)
    , 1
    , CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    else
    update p
    set <xsl:if test="CONS_NAME != ''">
      [Name] = LEFT('<xsl:value-of select="CONS_NAME" />', 255) ,
    </xsl:if>
    [DateEnd] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_COUNCIL_GOAL] p
    where p.[Code] = <xsl:value-of select="ID_CONS" /> and p.[Type] = 1 and [DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>
	
	<!-- F014_RESULT_DENY_REASON: F014.xml -->
	<xsl:template match="packet[zglv[type='OplOtk']]">
		<xsl:for-each select="zap">
			<!-- insert or update F014_RESULT_DENY_REASON -->
			<xsl:call-template name="OplOtkZap" />
		</xsl:for-each>
	</xsl:template>
	<!-- F014_RESULT_DENY_REASON "<zap>" template -->
	<xsl:template name="OplOtkZap" >
		if not exists(select * from [AKUZ].[T_F014_DENY_REASON] s where s.[Kod] = <xsl:value-of select="Kod" /> and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
		INSERT INTO [AKUZ].[T_F014_DENY_REASON] ([F014DenyReasonID], [Kod], [Naim], [DATEBEG], [DATEEND], [Osn], [Komment], [IDVID], [KodPG])
		SELECT
		NEWID()
		, <xsl:value-of select="Kod" />
		, '<xsl:value-of select="Naim" />'
		, CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, <xsl:if test="DATEEND != ''">
			<xsl:text>CONVERT(datetime, '</xsl:text>
			    <xsl:value-of select="DATEEND" />
			<xsl:text>', 104)</xsl:text>
		</xsl:if>
		<xsl:if test="DATEEND = ''">NULL</xsl:if>
		, '<xsl:value-of select="Osn" />'
		, '<xsl:value-of select="Komment" />'
		, <xsl:if test="IDVID != ''"><xsl:value-of select="IDVID" /></xsl:if>
		  <xsl:if test="IDVID = ''">NULL</xsl:if>
		, <xsl:if test="KodPG != ''">'<xsl:value-of select="KodPG" />'</xsl:if>
		  <xsl:if test="KodPG = ''">NULL</xsl:if> 
		else
		update s
		set 
		  s.[Kod] = <xsl:value-of select="Kod" />
		, s.[Naim] = '<xsl:value-of select="Naim" />'
		, s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, s.[DATEEND] = <xsl:if test="DATEEND != ''">
			<xsl:text>CONVERT(datetime, '</xsl:text>
			    <xsl:value-of select="DATEEND" />
			<xsl:text>', 104)</xsl:text>
		</xsl:if>
		<xsl:if test="DATEEND = ''">NULL</xsl:if>
		, s.[Osn] = '<xsl:value-of select="Osn" />'
		, s.[Komment] = '<xsl:value-of select="Komment" />'
		from [AKUZ].[T_F014_DENY_REASON] s
		where s.[Kod] = <xsl:value-of select="Kod" /> and s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		GO
	</xsl:template>

	<!-- F002 INSURER: F002.xml -->
	<xsl:template match="PACKET[insCompany]">
		<xsl:for-each select="insCompany">
			<!-- insert or update F002 INSURER -->
			<xsl:call-template name="InsCompany"/>
		</xsl:for-each>
	</xsl:template>
	<!-- F002 INSURER "<insCompany>" template -->
	<xsl:template name="InsCompany">
		if not exists(select * from [AKUZ].[T_INSURER] s
		where s.[SmoCod] = '<xsl:value-of select="smocod"/>' and s.[Gk_start] = CONVERT(datetime, '<xsl:value-of select="insInclude/d_begin"/>', 104))
		INSERT INTO [AKUZ].[T_INSURER]
		([InsurerID], [OKATO], [SmoCod], [Name], [ShortName], [INN], [OGRN], [KPP], [IndexLegal], [AddressLegal],
		[IndexReal], [Address], [OKOPF], [DirectorLastName], [DirectorFirstName], [DirectorFatherName], [Phone], [Fax], [hot_line], [EMail], [SAIT],
		[N_DOC], [Date_b], [Date_e], [D_TERM], [ORG], [Gk_start], [Gk_end], [NAME_E], [NAL_P], [YEAR_WORK], [DUVED],
		[KOL_ZL], [Date_red])
		SELECT
		NEWID()
		, '<xsl:value-of select="TF_OKATO"/>'
		, '<xsl:value-of select="smocod"/>'
		, '<xsl:value-of select="nam_smop"/>'
		, '<xsl:value-of select="nam_smok"/>'
		, '<xsl:value-of select="INN"/>'
		, '<xsl:value-of select="Ogrn"/>'
		, '<xsl:value-of select="KPP"/>'
		, '<xsl:value-of select="jurAddress/INDEX_J"/>'
		, '<xsl:value-of select="jurAddress/ADDR_J"/>'
		, '<xsl:value-of select="pstAddress/INDEX_F"/>'
		, '<xsl:value-of select="pstAddress/addr_f"/>'
		, '<xsl:value-of select="OKOPF"/>'
		, '<xsl:value-of select="fam_ruk"/>'
		, '<xsl:value-of select="im_ruk"/>'
		, '<xsl:value-of select="ot_ruk"/>'
		, '<xsl:value-of select="Phone"/>'
		, '<xsl:value-of select="Fax"/>'
		, '<xsl:value-of select="hot_line"/>'
		, '<xsl:value-of select="e_mail"/>'
		, '<xsl:value-of select="WWW"/>'
		, '<xsl:value-of select="licenziy/N_DOC"/>'
		, CONVERT(datetime, '<xsl:value-of select="insInclude/d_begin"/>', 104)
		, <xsl:choose>
		    <xsl:when test="insInclude/d_end != '' and string-length(insInclude/d_end) &gt; 0">
		        CONVERT(datetime, '<xsl:value-of select="insInclude/d_end"/>', 104)
		    </xsl:when>
		    <xsl:otherwise>NULL</xsl:otherwise>
	    </xsl:choose>
		, CONVERT(datetime, '<xsl:value-of select="licenziy/D_TERM"/>', 104)
		, <xsl:value-of select="ORG" />
		, CONVERT(datetime, '<xsl:value-of select="licenziy/D_START"/>', 104)
		, <xsl:choose>
			<xsl:when test="licenziy/DATE_E != '' and string-length(licenziy/DATE_E) &gt; 0">
				CONVERT(datetime, '<xsl:value-of select="licenziy/DATE_E"/>', 104)
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="insInclude/NAME_E != '' and string-length(insInclude/NAME_E) &gt; 0">
				<xsl:value-of select="insInclude/NAME_E"/>
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="insInclude/NAL_P != '' and string-length(insInclude/NAL_P) &gt; 0">
				<xsl:value-of select="insInclude/NAL_P"/>
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		, '<xsl:value-of select="insAdvice/YEAR_WORK"/>'
		, CONVERT(datetime, '<xsl:value-of select="insAdvice/DUVED"/>', 104)
		, <xsl:choose>
			<xsl:when test="insAdvice/KOL_ZL != '' and string-length(insAdvice/KOL_ZL) &gt; 0">
				<xsl:value-of select="insAdvice/KOL_ZL"/>
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		, CONVERT(datetime, '<xsl:value-of select="D_EDIT"/>', 104)
		else
		update s
		set
		s.[OKATO] = '<xsl:value-of select="TF_OKATO"/>'
		, s.[SmoCod] = '<xsl:value-of select="smocod"/>'
		, s.[Name] = '<xsl:value-of select="nam_smop"/>'
		, s.[ShortName] = '<xsl:value-of select="nam_smok"/>'
		, s.[OGRN] = '<xsl:value-of select="Ogrn"/>'
		, s.[KPP] = '<xsl:value-of select="KPP"/>'
		, s.[IndexLegal] = '<xsl:value-of select="jurAddress/INDEX_J"/>'
		, s.[AddressLegal] = '<xsl:value-of select="jurAddress/ADDR_J"/>'
		, s.[IndexReal] = '<xsl:value-of select="pstAddress/INDEX_F"/>'
		, s.[Address] = '<xsl:value-of select="pstAddress/addr_f"/>'
		, s.[OKOPF] = '<xsl:value-of select="OKOPF"/>'
		, s.[DirectorLastName] = '<xsl:value-of select="fam_ruk"/>'
		, s.[DirectorFirstName] = '<xsl:value-of select="im_ruk"/>'
		, s.[DirectorFatherName] = '<xsl:value-of select="ot_ruk"/>'
		, s.[Phone] = '<xsl:value-of select="Phone"/>'
		, s.[Fax] = '<xsl:value-of select="Fax"/>'
		, s.[hot_line] = '<xsl:value-of select="hot_line"/>'
		, s.[EMail] = '<xsl:value-of select="e_mail"/>'
		, s.[SAIT] = '<xsl:value-of select="WWW"/>'
		, s.[N_DOC] = '<xsl:value-of select="licenziy/N_DOC"/>'
		, s.[Date_b] = CONVERT(datetime, '<xsl:value-of select="insInclude/d_begin" />', 104)
		, s.[Date_e] = <xsl:choose>
			<xsl:when test="insInclude/d_end != '' and string-length(insInclude/d_end) &gt; 0">
				CONVERT(datetime, '<xsl:value-of select="insInclude/d_end"/>', 104)
			</xsl:when>
			<xsl:otherwise>NULL</xsl:otherwise>
		</xsl:choose>
		, s.[D_TERM] = CONVERT(datetime, '<xsl:value-of select="licenziy/D_TERM"/>', 104)
		, s.[ORG] = <xsl:value-of select="ORG" />
		, s.[Gk_start] = CONVERT(datetime, '<xsl:value-of select="licenziy/D_START"/>', 104)
		, s.[Gk_end] = CONVERT(datetime, '<xsl:value-of select="licenziy/DATE_E"/>', 104)
		, s.[NAME_E] = <xsl:choose>
		    <xsl:when test="insInclude/NAME_E != '' and string-length(insInclude/NAME_E) &gt; 0">
		        <xsl:value-of select="insInclude/NAME_E"/>
		    </xsl:when>
		    <xsl:otherwise>NULL</xsl:otherwise>
	    </xsl:choose>
		, s.[NAL_P] = <xsl:choose>
		<xsl:when test="insInclude/NAL_P != '' and string-length(insInclude/NAL_P) &gt; 0">
		    <xsl:value-of select="insInclude/NAL_P"/>
		        </xsl:when>
		    <xsl:otherwise>NULL</xsl:otherwise>
	    </xsl:choose>
		, s.[YEAR_WORK] = '<xsl:value-of select="insAdvice/YEAR_WORK"/>'
		, s.[DUVED] = CONVERT(datetime, '<xsl:value-of select="insAdvice/DUVED"/>', 104)
		, s.[KOL_ZL] =  <xsl:choose>
		<xsl:when test="insAdvice/KOL_ZL != '' and string-length(insAdvice/KOL_ZL) &gt; 0">
		    <xsl:value-of select="insAdvice/KOL_ZL"/>
		        </xsl:when>
		    <xsl:otherwise>NULL</xsl:otherwise>
	        </xsl:choose>
		, s.[Date_red] = CONVERT(datetime, '<xsl:value-of select="D_EDIT"/>', 104)
		from [AKUZ].[T_INSURER] s
		where s.[SmoCod] = '<xsl:value-of select="smocod"/>' and s.[Gk_start] = CONVERT(datetime, '<xsl:value-of select="insInclude/d_begin"/>', 104)
		GO
	</xsl:template>

  <!-- F032 TRMO: F032.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'TRMO-')]]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        MERGE AKUZ.T_F032_TRMO AS target
        USING (
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
            UNION ALL
          </xsl:if>
          SELECT
          NEWID(),
          '<xsl:value-of select="UIDMO"/>',
          '<xsl:value-of select="IDMO"/>',
          '<xsl:value-of select="MCOD"/>',
          '<xsl:value-of select="OKTMO_P"/>',
          '<xsl:value-of select="SUBJ"/>',
          CONVERT(datetime, '<xsl:value-of select="D_BEGIN"/>', 104),
          CONVERT(datetime, '<xsl:value-of select="D_BEGIN_OMS"/>', 104),
          <xsl:choose>
            <xsl:when test="normalize-space(D_END) != ''">
              <xsl:text>CONVERT(datetime, '</xsl:text>
              <xsl:value-of select="D_END"/>
              <xsl:text>', 104)</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          <xsl:choose>
            <xsl:when test="normalize-space(NAME_E) != ''">
              <xsl:value-of select="NAME_E"/>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          <xsl:value-of select="OSP"/>,
          '<xsl:value-of select="PARENT_IDMO"/>',
          '<xsl:value-of select="PARENT_UIDMO"/>',
          '<xsl:value-of select="VID_MO"/>',
          <xsl:choose>
            <xsl:when test="normalize-space(OID_MO) != ''">
              '<xsl:value-of select="OID_MO"/>'
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          '<xsl:value-of select="OID_SPMO"/>',
          '<xsl:value-of select="NAM_MOP"/>',
          '<xsl:value-of select="NAM_MOK"/>',
          '<xsl:value-of select="INN"/>',
          '<xsl:value-of select="KPP"/>',
          '<xsl:value-of select="OGRN"/>',
          '<xsl:value-of select="JURADDRESS_INDEX"/>',
          '<xsl:value-of select="JURADDRESS_ADDRESS"/>',
          '<xsl:value-of select="GAR_ADDRESS"/>',
          '<xsl:value-of select="OKFS"/>',
          '<xsl:value-of select="VEDPRI"/>',
          '<xsl:value-of select="PHONE"/>',
          '<xsl:value-of select="FAX"/>',
          '<xsl:value-of select="EMAIL"/>',
          CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>CONVERT(datetime, '</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 104)</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        ) AS source (F032TrmoID, UIDMO, IDMO, MCOD, OKTMO_P, SUBJ, D_BEGIN, D_BEGIN_OMS, D_END, NAME_E, OSP, PARENT_IDMO
        , PARENT_UIDMO, VID_MO, OID_MO, OID_SPMO, NAM_MOP, NAM_MOK, INN, KPP, OGRN, JURADDRESS_INDEX, JURADDRESS_ADDRESS, GAR_ADDRESS
        , OKFS, VEDPRI, PHONE, FAX, EMAIL, DATEBEG, DATEEND)
        ON (
        target.UIDMO = source.UIDMO
        AND target.DATEBEG = source.DATEBEG
        )
        WHEN MATCHED THEN
        UPDATE SET
        IDMO = source.IDMO,
        MCOD = source.MCOD,
        OKTMO_P = source.OKTMO_P,
        SUBJ = source.SUBJ,
        D_BEGIN = source.D_BEGIN,
        D_BEGIN_OMS = source.D_BEGIN_OMS,
        D_END = source.D_END,
        NAME_E = source.NAME_E,
        OSP = source.OSP,
        PARENT_IDMO = source.PARENT_IDMO,
        PARENT_UIDMO = source.PARENT_UIDMO,
        VID_MO = source.VID_MO,
        OID_MO = source.OID_MO,
        OID_SPMO = source.OID_SPMO,
        NAM_MOP = source.NAM_MOP,
        NAM_MOK = source.NAM_MOK,
        INN = source.INN,
        KPP = source.KPP,
        OGRN = source.OGRN,
        JURADDRESS_INDEX = source.JURADDRESS_INDEX,
        JURADDRESS_ADDRESS = source.JURADDRESS_ADDRESS,
        GAR_ADDRESS = source.GAR_ADDRESS,
        OKFS = source.OKFS,
        VEDPRI = source.VEDPRI,
        PHONE = source.PHONE,
        FAX = source.FAX,
        EMAIL = source.EMAIL,
        DATEEND = source.DATEEND
        WHEN NOT MATCHED THEN
        INSERT (F032TrmoID, UIDMO, IDMO, MCOD, OKTMO_P, SUBJ, D_BEGIN, D_BEGIN_OMS, D_END, NAME_E, OSP, PARENT_IDMO
        , PARENT_UIDMO, VID_MO, OID_MO, OID_SPMO, NAM_MOP, NAM_MOK, INN, KPP, OGRN, JURADDRESS_INDEX, JURADDRESS_ADDRESS, GAR_ADDRESS
        , OKFS, VEDPRI, PHONE, FAX, EMAIL, DATEBEG, DATEEND)
        VALUES (source.F032TrmoID, source.UIDMO, source.IDMO, source.MCOD, source.OKTMO_P, source.SUBJ, source.D_BEGIN, source.D_BEGIN_OMS, source.D_END, source.NAME_E, source.OSP, source.PARENT_IDMO
        , source.PARENT_UIDMO, source.VID_MO, source.OID_MO, source.OID_SPMO, source.NAM_MOP, source.NAM_MOK, source.INN, source.KPP, source.OGRN, source.JURADDRESS_INDEX, source.JURADDRESS_ADDRESS, source.GAR_ADDRESS
        , source.OKFS, source.VEDPRI, source.PHONE, source.FAX, source.EMAIL, source.DATEBEG, source.DATEEND);
        GO
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <!-- F033 SPMO: F033.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'SPMO-')]]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        MERGE AKUZ.T_F033_SPMO AS target
        USING (
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
            UNION ALL
          </xsl:if>
          SELECT
          NEWID() AS F033SpmoID,
          '<xsl:value-of select="UIDSPMO"/>',
          '<xsl:value-of select="IDSPMO"/>',
          '<xsl:value-of select="NAM_SPMO"/>',
          '<xsl:value-of select="NAM_SK_SPMO"/>',
          '<xsl:value-of select="PHONE"/>',
          '<xsl:value-of select="OSP"/>',
          <xsl:choose>
            <xsl:when test="normalize-space(VID_SPMO) != ''">
              '<xsl:value-of select="VID_SPMO"/>'
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          <xsl:choose>
            <xsl:when test="normalize-space(OID_SPMO) != ''">
              '<xsl:value-of select="OID_SPMO"/>'
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>CONVERT(datetime, '</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 104)</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        ) AS source (F033SpmoID, UIDSPMO, IDSPMO, NAM_SPMO, NAM_SK_SPMO, PHONE, OSP, VID_SPMO, OID_SPMO, DATEBEG, DATEEND)
        ON (
        target.UIDSPMO = source.UIDSPMO
        AND target.DATEBEG = source.DATEBEG
        )
        WHEN MATCHED THEN
        UPDATE SET
        IDSPMO = source.IDSPMO,
        NAM_SPMO = source.NAM_SPMO,
        NAM_SK_SPMO = source.NAM_SK_SPMO,
        PHONE = source.PHONE,
        OSP = source.OSP,
        VID_SPMO = source.VID_SPMO,
        OID_SPMO = source.OID_SPMO,
        DATEEND = source.DATEEND
        WHEN NOT MATCHED THEN
        INSERT (F033SpmoID, UIDSPMO, IDSPMO, NAM_SPMO, NAM_SK_SPMO, PHONE, OSP, VID_SPMO, OID_SPMO, DATEBEG, DATEEND)
        VALUES (source.F033SpmoID, source.UIDSPMO, source.IDSPMO, source.NAM_SPMO, source.NAM_SK_SPMO, source.PHONE, source.OSP, source.VID_SPMO, source.OID_SPMO, source.DATEBEG, source.DATEEND);
        GO
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <!-- F034 VUP: F034.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'VUP-')]]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        MERGE AKUZ.T_F034_VUP AS target
        USING (
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
        UNION ALL
          </xsl:if>
        SELECT
          NEWID() AS F034VupID,
          '<xsl:value-of select="UIDSPMO"/>',
          '<xsl:value-of select="IDADDRESS"/>',
          <xsl:value-of select="MPVID"/> AS MPVID,
          <xsl:value-of select="MPUSL"/> AS MPUSL,
          <xsl:value-of select="MPROF"/> AS MPROF,
          CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>CONVERT(datetime, '</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 104)</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        ) AS source (F034VupID, UIDSPMO, IDADDRESS, MPVID, MPUSL, MPROF, DATEBEG, DATEEND)
        ON (
          target.UIDSPMO = source.UIDSPMO
          AND target.IDADDRESS = source.IDADDRESS
          AND target.MPVID = source.MPVID
          AND target.MPUSL = source.MPUSL
          AND target.MPROF = source.MPROF
          AND target.DATEBEG = source.DATEBEG
        )
        WHEN MATCHED THEN
        UPDATE SET DATEEND = source.DATEEND
        WHEN NOT MATCHED THEN
        INSERT (F034VupID, UIDSPMO, IDADDRESS, MPVID, MPUSL, MPROF, DATEBEG, DATEEND)
        VALUES (source.F034VupID, source.UIDSPMO, source.IDADDRESS, source.MPVID, source.MPUSL, source.MPROF, source.DATEBEG, source.DATEEND);
        GO
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
  
  <!-- F038 ADDRMP: F038.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'ADDRMP-')]]">
    <xsl:for-each select="zap">
      <!-- insert or update F038 ADDRMP -->
      <xsl:call-template name="addrmpZap"/>
    </xsl:for-each>
  </xsl:template>
  
<xsl:template name="escape-sql-string">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test='contains($text, "&apos;")'>
      <xsl:value-of select='substring-before($text, "&apos;")'/>
      <xsl:text>''</xsl:text>
      <xsl:call-template name="escape-sql-string">
        <xsl:with-param name="text" select='substring-after($text, "&apos;")'/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
  
  <!-- F038 ADDRMP "<addrmp>" template -->
  <xsl:template name="addrmpZap">
  <xsl:variable name="addrEscaped">
    <xsl:call-template name="escape-sql-string">
      <xsl:with-param name="text" select="ADDR"/>
    </xsl:call-template>
  </xsl:variable>
    if not exists(select * from AKUZ.T_F038_ADDRMP s where s.IDADDRESS = '<xsl:value-of select="IDADDRESS"/>' and s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104))
    INSERT INTO AKUZ.T_F038_ADDRMP (F038AddrmpID, IDADDRESS, UIDMO, UIDSPMO, N_DOC, ADDR, ADDR_GAR, DATEBEG, DATEEND)
    SELECT
      NEWID()
      , '<xsl:value-of select="IDADDRESS"/>'
      , '<xsl:value-of select="UIDMO"/>'
      , '<xsl:value-of select="UIDSPMO"/>'
      , '<xsl:value-of select="N_DOC"/>'
      , '<xsl:value-of select="$addrEscaped"/>'
      , '<xsl:value-of select="ADDR_GAR"/>'
      , CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104)
      , <xsl:choose>
        <xsl:when test="DATEEND != ''">
          <xsl:text>CONVERT(datetime, '</xsl:text>
          <xsl:value-of select="DATEEND"/>
          <xsl:text>', 104)</xsl:text>
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    else
    update s
    set
      s.UIDMO = '<xsl:value-of select="UIDMO"/>'
      , s.UIDSPMO = '<xsl:value-of select="UIDSPMO"/>'
      , s.N_DOC = '<xsl:value-of select="N_DOC"/>'
      , s.ADDR = '<xsl:value-of select="$addrEscaped"/>'
      , s.ADDR_GAR = '<xsl:value-of select="ADDR_GAR"/>'
      , s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104)
      , s.DATEEND = <xsl:choose>
        <xsl:when test="DATEEND != ''">
          <xsl:text>CONVERT(datetime, '</xsl:text>
          <xsl:value-of select="DATEEND"/>
          <xsl:text>', 104)</xsl:text>
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    from AKUZ.T_F038_ADDRMP s
    where s.IDADDRESS = '<xsl:value-of select="IDADDRESS"/>' and s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104)
    GO
  </xsl:template>

  <!-- V039 KVZ: V039.xml -->
  <xsl:template match="packet[zglv[type='KVZ']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="kvzZap"/>
    </xsl:for-each>
  </xsl:template>

  <!-- V039 KVZ "<kvz>" template -->
  <xsl:template name="kvzZap">
    if not exists(select * from AKUZ.T_V039_KVZ s where s.ID_VZ = <xsl:value-of select="ID_VZ"/> and s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104))
    INSERT INTO AKUZ.T_V039_KVZ (V039KvzID, ID_VZ, N_VZ, DATEBEG, DATEEND)
    SELECT
      NEWID(),
      <xsl:value-of select="ID_VZ"/>,
      '<xsl:value-of select="N_VZ"/>',
      CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104),
      <xsl:choose>
        <xsl:when test="DATEEND != ''">
          CONVERT(datetime, '<xsl:value-of select="DATEEND"/>', 104)
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    else
    update s
    set
      s.N_VZ = '<xsl:value-of select="N_VZ"/>',
      s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104),
      s.DATEEND = <xsl:choose>
        <xsl:when test="DATEEND != ''">
          CONVERT(datetime, '<xsl:value-of select="DATEEND"/>', 104)
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    from AKUZ.T_V039_KVZ s
    where s.ID_VZ = <xsl:value-of select="ID_VZ"/> and s.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG"/>', 104)
    GO
  </xsl:template>
  
  <!-- Шаблон загрузки настроек сводной информации ВИМИС регистра -->
  <xsl:template match="VimisConsolidationSettings/Settings">
    <xsl:variable name="priorityPath">
      <xsl:call-template name="replace">
        <xsl:with-param name="text"><xsl:value-of select="@priorityXpath" /></xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="priorityLexem">
      <xsl:call-template name="replace">
        <xsl:with-param name="text"><xsl:value-of select="@priorityLexem" /></xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="xsltTemplate">
      <xsl:call-template name="replace">
        <xsl:with-param name="text"><xsl:value-of select="@template" /></xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

INSERT INTO AKUZ.S_VIMIS_CONSOLIDATION_GroupType(value) select '<xsl:value-of select="@group" />' where not exists (select * from AKUZ.S_VIMIS_CONSOLIDATION_GroupType where value = '<xsl:value-of select="@group" />');

INSERT INTO AKUZ.T_VIMIS_CONSOLIDATION_SETTING
           (VimisConsolidationSettingID
           ,Entities
           ,GroupType
           ,PriorityXpath
           ,PriorityLexem
           ,XsltTemplate)
SELECT
            NEWID()--VimisConsolidationSettingID, uniqueidentifier,
           ,(select top 1 e.EntityID from VCLib.T_ENTITIES e where e.[Schema] + '.' + e.EntName = '<xsl:value-of select="@entity" />')--Entities, int,
           ,(select top 1 v.id from AKUZ.S_VIMIS_CONSOLIDATION_GroupType v where v.value = '<xsl:value-of select="@group" />')--GroupType, int,
           ,<xsl:choose><xsl:when test="@priorityXpath!=''">'<xsl:value-of select="$priorityPath" />'</xsl:when><xsl:otherwise>NULL</xsl:otherwise></xsl:choose>--PriorityXpath, varchar(1000),
           ,<xsl:choose><xsl:when test="@priorityLexem!=''">'<xsl:value-of select="$priorityLexem" />'</xsl:when><xsl:otherwise>NULL</xsl:otherwise></xsl:choose>--PriorityLexem, varchar(1000),
           ,'<xsl:value-of select="$xsltTemplate" />'--XsltTemplate, varchar(max),)
where not exists
    (
        select *
        from AKUZ.T_VIMIS_CONSOLIDATION_SETTING cs
            join VCLib.T_ENTITIES e on e.EntityID = cs.Entities
            join AKUZ.S_VIMIS_CONSOLIDATION_GroupType v on v.id = cs.GroupType
        where e.[Schema] + '.' + e.EntName = '<xsl:value-of select="@entity" />'
                and
            v.value = '<xsl:value-of select="@group" />'
                and
            cs.PriorityXpath <xsl:choose><xsl:when test="@priorityXpath!=''"> = '<xsl:value-of select="$priorityPath" />'</xsl:when><xsl:otherwise>is NULL</xsl:otherwise></xsl:choose>
                and
            cs.PriorityLexem <xsl:choose><xsl:when test="@priorityLexem!=''"> = '<xsl:value-of select="$priorityLexem" />'</xsl:when><xsl:otherwise>is NULL</xsl:otherwise></xsl:choose>
    );
update cs
set XsltTemplate = '<xsl:value-of select="$xsltTemplate" />'
from AKUZ.T_VIMIS_CONSOLIDATION_SETTING cs
    join VCLib.T_ENTITIES e on e.EntityID = cs.Entities
    join AKUZ.S_VIMIS_CONSOLIDATION_GroupType v on v.id = cs.GroupType
where e.[Schema] + '.' + e.EntName = '<xsl:value-of select="@entity" />'
        and
    v.value = '<xsl:value-of select="@group" />'
        and
    PriorityXpath <xsl:choose><xsl:when test="@priorityXpath!=''"> = '<xsl:value-of select="$priorityPath" />'</xsl:when><xsl:otherwise>is NULL</xsl:otherwise></xsl:choose>
        and
    PriorityLexem <xsl:choose><xsl:when test="@priorityLexem!=''"> = '<xsl:value-of select="$priorityLexem" />'</xsl:when><xsl:otherwise>is NULL</xsl:otherwise></xsl:choose>;
  </xsl:template>

  <xsl:template name ="replace">
    <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="replace">
          <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>
