<?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()"/>

	<!-- F007: F007.xml -->
	<xsl:template match="packet[(zglv[type='Vedom'])]">
		<xsl:for-each select="zap">
			<xsl:call-template name="Vedom" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="Vedom" >
		if not exists(select * from [ISZL].[T_F007_DEPARTMENT] dk where dk.[IDVED] = '<xsl:value-of select="IDVED" />')
		INSERT INTO [ISZL].[T_F007_DEPARTMENT] ([IDVED], [VEDNAME], [DATEBEG], [DATEEND])
		SELECT
		'<xsl:value-of select="IDVED" />'
		, LEFT('<xsl:value-of select="VEDNAME" />', 255)
		, 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 dk
		set [VEDNAME] = LEFT('<xsl:value-of select="VEDNAME" />', 255)
		, dk.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, dk.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_F007_DEPARTMENT] dk
		where dk.[IDVED] = '<xsl:value-of select="IDVED" />'
		GO
	</xsl:template>

	<!-- F003_MO: F003.xml -->
	<xsl:template match="packet[(medCompany)]">
		<xsl:for-each select="medCompany">
			<!-- insert or update F003 -->
			<xsl:call-template name="medCompany" />
		</xsl:for-each>
	</xsl:template>
	<!-- F003 "<medCompany>" template -->
	<xsl:template name="medCompany" >
		if not exists(select * from [ISZL].[T_F003] s where s.[MCOD] = '<xsl:value-of select="mcod" />')
		INSERT INTO [ISZL].[T_F003] ([TF_OKATO] ,[MCOD] ,[NAM_MOP] ,[NAM_MOK] ,[INN] ,[OGRN] ,[KPP] ,[INDEX_J] ,[ADDR_J] ,[OKOPF] ,[VEDPRI] ,[ORG] ,[FAM_RUK] ,[IM_RUK] ,[OT_RUK] ,[PHONE] ,[FAX] ,[E_MAIL] ,[WWW] ,[D_BEGIN] ,[D_END] ,[D_EDIT] ,[Holidays] ,[IsActive], [MP], [Local])
		SELECT
		LEFT('<xsl:value-of select="tf_okato" />', 5)
		, '<xsl:value-of select="mcod" />'
		, LEFT('<xsl:value-of select="nam_mop" />', 250)
		, LEFT('<xsl:value-of select="nam_mok" />', 250)
		, LEFT('<xsl:value-of select="inn" />', 12)
		, LEFT('<xsl:value-of select="Ogrn" />', 15)
		, LEFT('<xsl:value-of select="KPP" />', 9)
		, LEFT('<xsl:value-of select="jurAddress/index_j" />', 6)
		, LEFT('<xsl:value-of select="jurAddress/addr_j" />', 254)
		, (select top 1 o005.O005OkopfID from ISZL.T_O005_OKOPF o005 where KOD = '<xsl:value-of select="okopf" />')
		, <xsl:value-of select="vedpri" />
		, LEFT('<xsl:value-of select="org" />', 2)
		, LEFT('<xsl:value-of select="fam_ruk" />', 40)
		, LEFT('<xsl:value-of select="im_ruk" />', 40)
		, LEFT('<xsl:value-of select="ot_ruk" />', 40)
		, LEFT('<xsl:value-of select="phone" />', 40)
		, LEFT('<xsl:value-of select="fax" />', 40)
		, LEFT('<xsl:value-of select="e_mail" />', 50)
		, LEFT('<xsl:value-of select="www" />', 50)
		, CONVERT(datetime, '<xsl:value-of select="medInclude/d_begin" />', 104)
		, <xsl:choose>
			<xsl:when test="d_end != ''">
				CONVERT(datetime, '<xsl:value-of select="d_end" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, CONVERT(datetime, '<xsl:value-of select="d_edit" />', 104)
		, 64
		, 1
		, ''
		, 0
		else
		update l
		set l.TF_OKATO = LEFT('<xsl:value-of select="tf_okato" />', 5)
		, l.NAM_MOP = LEFT('<xsl:value-of select="nam_mop" />', 250)
		, l.NAM_MOK = LEFT('<xsl:value-of select="nam_mok" />', 250)
		, l.INN = LEFT('<xsl:value-of select="inn" />', 12)
		, l.OGRN = LEFT('<xsl:value-of select="Ogrn" />', 15)
		, l.KPP = LEFT('<xsl:value-of select="KPP" />', 9)
		, l.INDEX_J = LEFT('<xsl:value-of select="jurAddress/index_j" />', 6)
		, l.ADDR_J = LEFT('<xsl:value-of select="jurAddress/addr_j" />', 254)
		, l.OKOPF = (select top 1 o005.O005OkopfID from ISZL.T_O005_OKOPF o005 where KOD = '<xsl:value-of select="okopf" />')
		, l.VEDPRI = <xsl:value-of select="vedpri" />
		, l.ORG = LEFT('<xsl:value-of select="org" />', 3)
		, l.FAM_RUK = LEFT('<xsl:value-of select="fam_ruk" />', 40)
		, l.IM_RUK = LEFT('<xsl:value-of select="im_ruk" />', 40)
		, l.OT_RUK = LEFT('<xsl:value-of select="ot_ruk" />', 40)
		, l.PHONE = LEFT('<xsl:value-of select="phone" />', 40)
		, l.FAX = LEFT('<xsl:value-of select="fax" />', 40)
		, l.E_MAIL = LEFT('<xsl:value-of select="e_mail" />', 50)
		, l.WWW = LEFT('<xsl:value-of select="www" />', 50)
		, l.D_BEGIN = CONVERT(datetime, '<xsl:value-of select="medInclude/d_begin" />', 104)
		, l.D_END = <xsl:choose>
			<xsl:when test="d_end != ''">
				CONVERT(datetime, '<xsl:value-of select="d_end" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, l.D_EDIT = CONVERT(datetime, '<xsl:value-of select="d_edit" />', 104)
		, l.Holidays = 64
		, l.IsActive = 1
		from ISZL.T_F003 l
		where l.MCOD = '<xsl:value-of select="mcod" />'

		update l
		set l.IsActive = case when l.D_END is NULL or l.D_END &gt; GETDATE() then 1 else 0 end
		from ISZL.T_F003 l
		where l.MCOD = '<xsl:value-of select="mcod" />'
		GO

		<!--Organization creating-->
		if not exists(select * from [ISZL].[T_ORGANIZATION] s where s.[CODE] = LEFT('<xsl:value-of select="mcod" />', 6) and type_ = (select entityid from vclib.t_entities where entname = 'MO'))
		INSERT INTO ISZL.T_ORGANIZATION (ORGANIZATIONID, NAME, TYPE_, CODE)
		SELECT NEWID()
		, LEFT('<xsl:value-of select="nam_mok" />', 250)
		, (select entityid from vclib.t_entities where entname = 'MO')
		, LEFT('<xsl:value-of select="mcod" />', 6)
		else
		update s
		set s.[NAME] = LEFT('<xsl:value-of select="nam_mok" />', 250)
		from [ISZL].[T_ORGANIZATION] s
		where s.[CODE] = LEFT('<xsl:value-of select="mcod" />', 6)
		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 [ISZL].[T_V002_PROFILE] p where p.[Id] = '<xsl:value-of select="IDPR" />')
		INSERT INTO [ISZL].[T_V002_PROFILE] ([Id], [Name], [DateBegin], [DateEnd])
		SELECT '<xsl:value-of select="IDPR" />'
		, LEFT('<xsl:value-of select="PRNAME" />', 255)
		, 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 [Name] = LEFT('<xsl:value-of select="PRNAME" />', 255)
		, [DateBegin] = 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 [ISZL].[T_V002_PROFILE] p
		where p.[Id] = '<xsl:value-of select="IDPR" />'
		GO
		if not exists(select * from [ISZL].[T_STRUCTURE_BED] p where p.[Code] = '<xsl:value-of select="IDPR" />')
		INSERT INTO [ISZL].[T_STRUCTURE_BED] ([Code], [CodeMz], [Name], [DateBegin], [DateEnd], [StructureBedType])
		SELECT '<xsl:value-of select="IDPR" />'
		, LEFT('<xsl:value-of select="IDPR" />', 3)
		, LEFT('<xsl:value-of select="PRNAME" />', 1000)
		, 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 p
		set [Name] = LEFT('<xsl:value-of select="PRNAME" />', 1000)
		, [CodeMz] = LEFT('<xsl:value-of select="IDPR" />', 3)
		, [DateBegin] = 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 [ISZL].[T_STRUCTURE_BED] p
		where p.[Code] = '<xsl:value-of select="IDPR" />'
		GO
	</xsl:template>

	<!-- F002_SMO: F002.xml -->
	<xsl:template match="packet[(insCompany)]">
		<xsl:for-each select="insCompany">
			<!-- insert or update F002_SMO -->
			<xsl:call-template name="insCompany" />
		</xsl:for-each>
	</xsl:template>
	<!-- F002 "<insCompany>" template -->
	<xsl:template name="insCompany" >
		<xsl:variable name="d_start">
			<xsl:for-each select="licenziy">
				<xsl:sort select="d_start" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="d_start"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="n_doc">
			<xsl:for-each select="licenziy">
				<xsl:sort select="d_start" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="n_doc"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="date_e">
			<xsl:for-each select="licenziy">
				<xsl:sort select="d_start" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="date_e"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="d_begin">
			<xsl:for-each select="insInclude">
				<xsl:sort select="d_begin" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="d_begin"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="d_end">
			<xsl:for-each select="insInclude">
				<xsl:sort select="d_begin" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="d_end"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="name_e">
			<xsl:for-each select="insInclude">
				<xsl:sort select="d_begin" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="name_e"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="DUVED">
			<xsl:for-each select="insAdvice">
				<xsl:sort select="DUVED" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="DUVED"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="Nal_p">
			<xsl:for-each select="insInclude">
				<xsl:sort select="d_begin" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="Nal_p"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="kol_zl">
			<xsl:for-each select="insAdvice">
				<xsl:sort select="DUVED" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="kol_zl"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		if not exists(select * from [ISZL].[T_F002_SMO] s where s.[SMOCOD] = '<xsl:value-of select="smocod" />')
		INSERT INTO [ISZL].[T_F002_SMO]
		([TF_OKATO] ,[SMOCOD] ,[NAM_SMOP] ,[NAM_SMOK] ,[INN] ,[OGRN] ,[KPP] ,[INDEX_J] ,[ADDR_J] ,[OKOPF] ,[ORG] ,[FAM_RUK] ,[IM_RUK] ,[OT_RUK] ,[PHONE] ,[FAX] ,[E_MAIL] ,[N_DOC] ,[D_START] ,[DATA_E] ,[WWW] ,[D_BEGIN] ,[D_END] ,[NAME_E] ,[DUVED] ,[D_EDIT] ,[INDEX_F] ,[ADDR_F] ,[NAL_P] ,[KOL_ZL] ,[IsActive] ,[Local])
		SELECT
		LEFT('<xsl:value-of select="tf_okato" />', 5)
		, LEFT('<xsl:value-of select="smocod" />', 5)
		, LEFT('<xsl:value-of select="nam_smop" />', 254)
		, LEFT('<xsl:value-of select="nam_smok" />', 250)
		, LEFT('<xsl:value-of select="inn" />', 12)
		, LEFT('<xsl:value-of select="Ogrn" />', 15)
		, LEFT('<xsl:value-of select="KPP" />', 9)
		, LEFT('<xsl:value-of select="jurAddress/index_j" />', 6)
		, LEFT('<xsl:value-of select="jurAddress/addr_j" />', 254)
		, (select top 1 o005.O005OkopfID from ISZL.T_O005_OKOPF o005 where KOD = '<xsl:value-of select="okopf" />')
		, <xsl:value-of select="org" />
		, LEFT('<xsl:value-of select="fam_ruk" />', 40)
		, LEFT('<xsl:value-of select="im_ruk" />', 40)
		, LEFT('<xsl:value-of select="ot_ruk" />', 40)
		, LEFT('<xsl:value-of select="phone" />', 40)
		, LEFT('<xsl:value-of select="fax" />', 40)
		, LEFT('<xsl:value-of select="e_mail" />', 50)
		, LEFT('<xsl:value-of select="$n_doc" />', 20)
		, CONVERT(datetime, '<xsl:value-of select="$d_start" />', 104)
		, <xsl:choose>
			<xsl:when test="$date_e != ''">
				CONVERT(datetime, '<xsl:value-of select="$date_e" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, LEFT('<xsl:value-of select="www" />', 100)
		, CONVERT(datetime, '<xsl:value-of select="$d_begin" />', 104)
		, <xsl:choose>
			<xsl:when test="$d_end != ''">
				CONVERT(datetime, '<xsl:value-of select="$d_end" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, LEFT('<xsl:value-of select="$name_e" />', 10)
		, <xsl:choose>
			<xsl:when test="$DUVED != ''">
				CONVERT(datetime, '<xsl:value-of select="$DUVED" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="d_edit != ''">
				CONVERT(datetime, '<xsl:value-of select="d_edit" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, LEFT('<xsl:value-of select="pstAddress/index_f" />', 254)
		, LEFT('<xsl:value-of select="pstAddress/addr_f" />', 254)
		, LEFT('<xsl:value-of select="$Nal_p" />', 1)
		, <xsl:choose>
			<xsl:when test="$kol_zl != ''">
				<xsl:value-of select="$kol_zl" />
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:if test="$d_end != ''">
			0
		</xsl:if><xsl:if test="$d_end = ''">1</xsl:if>
		, 0
		else
		UPDATE s
		set
		s.TF_OKATO = LEFT('<xsl:value-of select="tf_okato" />', 5)
		, s.NAM_SMOP = LEFT('<xsl:value-of select="nam_smop" />', 254)
		, s.NAM_SMOK = LEFT('<xsl:value-of select="nam_smok" />', 250)
		, s.INN = LEFT('<xsl:value-of select="inn" />', 12)
		, s.OGRN = LEFT('<xsl:value-of select="Ogrn" />', 15)
		, s.KPP = LEFT('<xsl:value-of select="KPP" />', 9)
		, s.INDEX_J = LEFT('<xsl:value-of select="jurAddress/index_j" />', 6)
		, s.ADDR_J = LEFT('<xsl:value-of select="jurAddress/addr_j" />', 254)
		, s.OKOPF = (select top 1 o005.O005OkopfID from ISZL.T_O005_OKOPF o005 where KOD = '<xsl:value-of select="okopf" />')
		, s.ORG = <xsl:value-of select="org" />
		, s.FAM_RUK = LEFT('<xsl:value-of select="fam_ruk" />', 40)
		, s.IM_RUK = LEFT('<xsl:value-of select="im_ruk" />', 40)
		, s.OT_RUK = LEFT('<xsl:value-of select="ot_ruk" />', 40)
		, s.PHONE = LEFT('<xsl:value-of select="phone" />', 40)
		, s.FAX = LEFT('<xsl:value-of select="fax" />', 40)
		, s.E_MAIL = LEFT('<xsl:value-of select="e_mail" />', 50)
		, s.N_DOC = LEFT('<xsl:value-of select="$n_doc" />', 20)
		, s.D_START = CONVERT(datetime, '<xsl:value-of select="$d_start" />', 104)
		, s.DATA_E = <xsl:choose>
			<xsl:when test="$date_e != ''">
				CONVERT(datetime, '<xsl:value-of select="$date_e" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, s.WWW = LEFT('<xsl:value-of select="www" />', 100)
		, s.D_BEGIN = CONVERT(datetime, '<xsl:value-of select="$d_begin" />', 104)
		, s.D_END = <xsl:choose>
			<xsl:when test="$d_end != ''">
				CONVERT(datetime, '<xsl:value-of select="$d_end" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, s.NAME_E = LEFT('<xsl:value-of select="$name_e" />', 10)
		, s.DUVED = <xsl:choose>
			<xsl:when test="$DUVED != ''">
				CONVERT(datetime, '<xsl:value-of select="$DUVED" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, s.D_EDIT = <xsl:choose>
			<xsl:when test="d_edit != ''">
				CONVERT(datetime, '<xsl:value-of select="d_edit" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, s.INDEX_F = LEFT('<xsl:value-of select="pstAddress/index_f" />', 254)
		, s.ADDR_F = LEFT('<xsl:value-of select="pstAddress/addr_f" />', 254)
		, s.NAL_P = LEFT('<xsl:value-of select="$Nal_p" />', 1)
		, s.KOL_ZL = <xsl:choose>
			<xsl:when test="$kol_zl != ''">
				<xsl:value-of select="$kol_zl" />
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		FROM [ISZL].[T_F002_SMO] s
		where s.SMOCOD = LEFT('<xsl:value-of select="smocod" />', 5)
		GO
		<!--Creation of organization with SMO type-->
		if not exists(select * from ISZL.T_ORGANIZATION where code = LEFT('<xsl:value-of select="smocod"/>', 5) and type_ = (select entityid from vclib.t_entities where entname = 'SMO'))
		INSERT INTO ISZL.T_ORGANIZATION (ORGANIZATIONID, NAME, TYPE_, CODE)
		SELECT NEWID()
		, LEFT('<xsl:value-of select="nam_smok" />', 250)
		, (select entityid from vclib.t_entities where entname = 'SMO')
		, LEFT('<xsl:value-of select="smocod" />', 5)
		else
		update s
		set s.[NAME] = LEFT('<xsl:value-of select="nam_smok" />', 250)
		from [ISZL].[T_ORGANIZATION] s
		where s.[CODE] = LEFT('<xsl:value-of select="smocod"/>', 5)
		GO
	</xsl:template>

	<!-- F001: F001.xml -->
	<xsl:template match="packet[(TFOMS)]">
		<xsl:for-each select="TFOMS">
			<!-- insert or update F001_SMO -->
			<xsl:call-template name="TFOMS" />
		</xsl:for-each>
	</xsl:template>
	<!--F001 TFOMS template-->
	<xsl:template name="TFOMS" >
		INSERT INTO ISZL.T_F001_TFOMS (TF_KOD, TF_OKATO, TF_OGRN, NAME_TFP, NAME_TFK, INDEX_, ADDRESS_, FAM_DIR, IM_DIR, OT_DIR, PHONE, FAX, E_MAIL, KF_TF, WWW, BIK, INN, KPP, KBK, OKTMO, L_NAIM, L_B, L_RS, T_NAIM, T_B, T_RS, D_EDIT, D_END)
		SELECT
		LEFT('<xsl:value-of select="tf_kod" />', 2)
		, LEFT('<xsl:value-of select="tf_okato" />', 5)
		, LEFT('<xsl:value-of select="tf_ogrn" />', 13)
		, LEFT('<xsl:value-of select="name_tfp" />', 250)
		, LEFT('<xsl:value-of select="name_tfk" />', 250)
		, LEFT('<xsl:value-of select="index" />', 6)
		, LEFT('<xsl:value-of select="address" />', 250)
		, LEFT('<xsl:value-of select="fam_dir" />', 40)
		, LEFT('<xsl:value-of select="im_dir" />', 40)
		, LEFT('<xsl:value-of select="ot_dir" />', 40)
		, LEFT('<xsl:value-of select="phone" />', 40)
		, LEFT('<xsl:value-of select="fax" />', 40)
		, LEFT('<xsl:value-of select="e_mail" />', 50)
		, '<xsl:value-of select="kf_tf" />'
		, LEFT('<xsl:value-of select="www" />', 100)
		, LEFT('<xsl:value-of select="bic" />', 11)
		, LEFT('<xsl:value-of select="inn" />', 13)
		, LEFT('<xsl:value-of select="kpp" />', 10)
		, '<xsl:value-of select="kbk" />'
		, LEFT('<xsl:value-of select="oktmo" />', 13)
		, LEFT('<xsl:value-of select="L_NAIM" />', 250)
		, LEFT('<xsl:value-of select="L_B" />', 100)
		, LEFT('<xsl:value-of select="L_RS" />', 20)
		, LEFT('<xsl:value-of select="T_NAIM" />', 250)
		, LEFT('<xsl:value-of select="T_B" />', 100)
		, LEFT('<xsl:value-of select="T_RS" />', 20)
		, <xsl:choose>
			<xsl:when test="d_edit != ''">
				CONVERT(datetime, '<xsl:value-of select="d_edit" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="d_end != ''">
				CONVERT(datetime, '<xsl:value-of select="d_end" />', 104)
			</xsl:when>
			<xsl:otherwise>
				NULL
			</xsl:otherwise>
		</xsl:choose>
		where not exists(select * from ISZL.T_F001_TFOMS s where s.tf_kod = LEFT('<xsl:value-of select="tf_kod" />', 2))
		GO
		<!--Updating current not-null values-->
		update ISZL.T_F001_TFOMS
		set TF_OKATO = LEFT('<xsl:value-of select="tf_okato" />', 5)
		, TF_OGRN = LEFT('<xsl:value-of select="tf_ogrn" />', 13)
		, NAME_TFP = LEFT('<xsl:value-of select="name_tfp" />', 250)
		, NAME_TFK = LEFT('<xsl:value-of select="name_tfk" />', 250)
		, ADDRESS_ = LEFT('<xsl:value-of select="address" />', 250)
		where TF_KOD = LEFT('<xsl:value-of select="tf_kod" />', 2)
		GO
		<!--Creation of organization with FOND type-->
		INSERT INTO ISZL.T_ORGANIZATION (ORGANIZATIONID, NAME, TYPE_, CODE)
		SELECT NEWID()
		, LEFT('<xsl:value-of select="name_tfk" />', 250)
		, (select entityid from vclib.t_entities where entname = 'FOND')
		, LEFT('<xsl:value-of select="tf_kod" />', 2)
		where not exists(select * from ISZL.T_ORGANIZATION where code = LEFT('<xsl:value-of select="tf_kod"/>', 2) and type_ = (select entityid from vclib.t_entities where entname = 'FOND'))
		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 [ISZL].[T_V021_SPECIALITY] s where s.[Id] = <xsl:value-of select="IDSPEC" />)
		INSERT INTO [ISZL].[T_V021_SPECIALITY] ([Id], [Name], [DateBegin], [DateEnd])
		SELECT <xsl:value-of select="IDSPEC" />
		, LEFT('<xsl:value-of select="SPECNAME" />', 255)
		, 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 s
		set s.[Name] = LEFT('<xsl:value-of select="SPECNAME" />', 255)
		, s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, s.[DateEnd] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V021_SPECIALITY] s
		where s.[Id] = <xsl:value-of select="IDSPEC" />
		GO
	</xsl:template>

	<!-- V009_RESULT: V009.xml -->
	<xsl:template match="packet[(zglv[type='Rezult'])]">
		<xsl:for-each select="zap">
			<!-- insert or update-->
			<xsl:call-template name="ResultZap" />
		</xsl:for-each>
	</xsl:template>
	<!-- V009 "<zap>" template -->
	<xsl:template name="ResultZap" >
		if not exists(select * from [ISZL].[T_V009_RESULT] p where p.[IDRMP] = '<xsl:value-of select="IDRMP" />')
		INSERT INTO [ISZL].[T_V009_RESULT] ([IDRMP], [RMPNAME], [DL_USLOV], [DATEBEG], [DATEEND])
		SELECT '<xsl:value-of select="IDRMP" />'
		, LEFT('<xsl:value-of select="RMPNAME" />', 254)
		, LEFT('<xsl:value-of select="DL_USLOV" />', 255)
		, 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 [RMPNAME] = LEFT('<xsl:value-of select="RMPNAME" />', 254)
		, p.[DL_USLOV] = LEFT('<xsl:value-of select="DL_USLOV" />', 255)
		, p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, p.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V009_RESULT] p
		where p.[IDRMP] = '<xsl:value-of select="IDRMP" />'
		GO
	</xsl:template>

	<!-- V001_NOMENCLATURE: V001.xml -->
	<xsl:template match="PACKET[(type='Nom_Serv')]">
		<xsl:for-each select="PART/SERVICES">
			<!-- insert or update -->
			<xsl:call-template name="Nom_Serv" />
		</xsl:for-each>
	</xsl:template>
	<!-- V001 "<zap>" template -->
	<xsl:template name="Nom_Serv" >
		if not exists(select * from [ISZL].[T_V001_NOMENCLATURE] p where p.[Code] = '<xsl:value-of select="SERVICE_CODE" />')
		INSERT INTO [ISZL].[T_V001_NOMENCLATURE] ([NomenclatureID], [Code], [Name], [DATEBEG], [DATEEND], [IsOperation])
		SELECT
		NewID()
		, LEFT('<xsl:value-of select="SERVICE_CODE" />', 15)
		, LEFT('<xsl:value-of select="SERVICE_NAME" />', 255)
		, CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 120)
		<xsl:if test="DATEEND != ''">
			, CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 120)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, '<xsl:value-of select="NEW_NOM" />'
		else
		update p
		set [Name] = LEFT('<xsl:value-of select="SERVICE_NAME" />', 255)
		, p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 120)
		, p.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 120)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, p.[IsOperation] = '<xsl:value-of select="NEW_NOM" />'
		from [ISZL].[T_V001_NOMENCLATURE] p
		where p.[Code] = '<xsl:value-of select="IDRMP" />'
		GO
	</xsl:template>

	<!-- V008_MEDICAL_TYPE: V008.xml -->
	<xsl:template match="packet[(zglv[type='VidMp'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="VidMp" />
		</xsl:for-each>
	</xsl:template>
	<!-- V008 "<zap>" template -->
	<xsl:template name="VidMp" >
		if not exists(select * from [ISZL].[T_V008_MEDICAL_TYPE] mt where mt.[IDVMP] = '<xsl:value-of select="IDVMP" />')
		INSERT INTO [ISZL].[T_V008_MEDICAL_TYPE] ([IDVMP], [VMPNAME], [DATEBEG], [DATEEND])
		SELECT '<xsl:value-of select="IDVMP" />'
		, LEFT('<xsl:value-of select="VMPNAME" />', 255)
		, 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 mt
		set [VMPNAME] = LEFT('<xsl:value-of select="VMPNAME" />', 255)
		, mt.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, mt.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V008_MEDICAL_TYPE] mt
		where mt.[IDVMP] = '<xsl:value-of select="IDVMP" />'
		GO
	</xsl:template>

	<!-- F015_FEDERAL_DISTRICT: F015.xml -->
	<xsl:template match="PACKET[(zglv[type='Okrug'])]">
		<xsl:for-each select="ZAP">
			<!-- insert or update -->
			<xsl:call-template name="Okrug" />
		</xsl:for-each>
	</xsl:template>
	<!-- F015 "<zap>" template -->
	<xsl:template name="Okrug" >
		if not exists(select * from [ISZL].[T_F015_FEDERAL_DISTRICT] f where f.[KOD_OK] = '<xsl:value-of select="KOD_OK" />')
		INSERT INTO [ISZL].[T_F015_FEDERAL_DISTRICT] ([KOD_OK], [OKRNAME], [DATEBEG], [DATEEND])
		SELECT '<xsl:value-of select="KOD_OK" />'
		, LEFT('<xsl:value-of select="OKRNAME" />', 255)
		, 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 f
		set [OKRNAME] = LEFT('<xsl:value-of select="OKRNAME" />', 255)
		, f.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, f.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_F015_FEDERAL_DISTRICT] f
		where f.[KOD_OK] = '<xsl:value-of select="KOD_OK" />'
		GO
	</xsl:template>

	<!-- F010_SUBJECT: F010.xml -->
	<xsl:template match="packet[(zglv[type='Subekti'])]">
		<xsl:for-each select="zap">
			<!-- insert or update  -->
			<xsl:call-template name="Subekti" />
		</xsl:for-each>
	</xsl:template>
	<!-- F010 "<zap>" template -->
	<xsl:template name="Subekti" >
		if not exists(select * from [ISZL].[T_F010_SUBJECT] s where s.[KOD_OKATO] = '<xsl:value-of select="KOD_OKATO" />')
		INSERT INTO [ISZL].[T_F010_SUBJECT] ([SubjectID], [KOD_TF],[KOD_OKATO], [SUBNAME],[OKRUG],[DATEBEG],[DATEEND])
		SELECT
		'<xsl:value-of select="KOD_TF" />'
		, '<xsl:value-of select="KOD_TF" />'
		, LEFT('<xsl:value-of select="KOD_OKATO" />', 256)
		, LEFT('<xsl:value-of select="SUBNAME" />', 256)
		, LEFT('<xsl:value-of select="OKRUG" />', 256)
		, 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 s
		set [KOD_OKATO] = LEFT('<xsl:value-of select="KOD_OKATO" />', 256)
		, s.[SUBNAME] = LEFT('<xsl:value-of select="SUBNAME" />', 256)
		, s.[OKRUG] = LEFT('<xsl:value-of select="OKRUG" />', 256)
		, s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, s.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_F010_SUBJECT] s
		where s.[KOD_TF] = '<xsl:value-of select="KOD_TF" />'
		GO
	</xsl:template>

	<!-- N002_OnkStad: N002.xml -->
	<xsl:template match="packet[(zglv[type='OnkStad'])]">
		<xsl:for-each select="zap">
			<!-- insert or update-->
			<xsl:call-template name="OnkStad" />
		</xsl:for-each>
	</xsl:template>
	<!-- N002 "<zap>" template -->
	<xsl:template name="OnkStad" >
		if not exists(select * from [ISZL].[T_N002_OnkStad] os where os.[ID_St] = <xsl:value-of select="ID_St" />)
		INSERT INTO [ISZL].[T_N002_OnkStad] ([ID_St], [DS_St],[KOD_St], [DATEBEG], [DATEEND],[N002ID])
		SELECT <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 != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update os
		set [DS_St] = LEFT('<xsl:value-of select="DS_St" />', 5)
		, os.[KOD_St] = LEFT('<xsl:value-of select="KOD_St" />', 5)
		, os.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, os.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N002_OnkStad] os
		where os.[ID_St] = <xsl:value-of select="ID_St" />
		GO
	</xsl:template>


	<!-- N001_OnkPrOt: N001.xml -->
	<xsl:template match="packet[(zglv[type='OnkPrOt'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="OnkPrOt" />
		</xsl:for-each>
	</xsl:template>
	<!-- N001 "<zap>" template -->
	<xsl:template name="OnkPrOt" >
		if not exists(select * from [ISZL].[T_N001_OnkPrOt] opo where opo.[ID_PrOt] = '<xsl:value-of select="ID_PrOt" />')
		INSERT INTO [ISZL].[T_N001_OnkPrOt] ([ID_PrOt], [PrOt_NAME], [DATEBEG], [DATEEND],[N001ID])
		SELECT '<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 != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update opo
		set [PrOt_NAME] = LEFT('<xsl:value-of select="PrOt_NAME" />', 255)
		, opo.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, opo.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N001_OnkPrOt] opo
		where opo.[ID_PrOt] = '<xsl:value-of select="ID_PrOt" />'
		GO
	</xsl:template>


	<!-- V024: V024.xml -->
	<xsl:template match="packet[(zglv[type='DopKr'])]">
		<xsl:for-each select="zap">
			<!-- insert or update  -->
			<xsl:call-template name="DopKr" />
		</xsl:for-each>
	</xsl:template>
	<!-- N002 "<zap>" template -->
	<xsl:template name="DopKr" >
		if not exists(select * from [ISZL].[T_V024] dk where dk.[IDDKK] = '<xsl:value-of select="IDDKK" />')
		INSERT INTO [ISZL].[T_V024] ([IDDKK], [DKKNAME], [DATEBEG], [DATEEND])
		SELECT
		LEFT('<xsl:value-of select="IDDKK" />', 255)
		, LEFT('<xsl:value-of select="DKKNAME" />', 255)
		, 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 dk
		set [DKKNAME] = LEFT('<xsl:value-of select="DKKNAME" />', 255)
		, dk.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, dk.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V024] dk
		where dk.[IDDKK] = '<xsl:value-of select="IDDKK" />'
		GO
	</xsl:template>

	<!-- F008_OMS_TYPE: F008.xml -->
	<xsl:template match="packet[(zglv[type='TipOMS'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="tipOms" />
		</xsl:for-each>
	</xsl:template>
	<!-- F008 "<zap>" template -->
	<xsl:template name="tipOms" >
		if not exists(select * from [ISZL].[T_F008_OMS_TYPE] f8 where f8.IDDOC = <xsl:value-of select="IDDOC" />)
		INSERT INTO [ISZL].[T_F008_OMS_TYPE]
		([IDDOC] ,[DOCNAME] ,[DATEBEG] ,[DATEEND])
		SELECT
		<xsl:value-of select="IDDOC" />--IDDOC, int,
		,LEFT('<xsl:value-of select="DOCNAME" />', 254)--DOCNAME, varchar(254),
		,CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)--DATEBEG, datetime,
		,<xsl:choose>
			<xsl:when test="DATEEND!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>--DATEEND, datetime,
		else
		update f8
		set f8.DOCNAME = LEFT('<xsl:value-of select="DOCNAME" />', 254)
		,f8.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,f8.DATEEND = <xsl:choose>
			<xsl:when test="DATEEND!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>--DATEEND, datetime,
		from [ISZL].[T_F008_OMS_TYPE] f8
		where f8.IDDOC = <xsl:value-of select="IDDOC" />
		GO
	</xsl:template>

	<!-- F011_DOCUMENT_TYPE: F011.xml -->
	<xsl:template match="packet[(zglv[type='Tipdoc'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="tipDoc" />
		</xsl:for-each>
	</xsl:template>
	<!-- F011 "<zap>" template -->
	<xsl:template name="tipDoc" >
		if not exists(select * from [ISZL].[T_F011_DOCUMENT_TYPE] f11 where f11.IDDOC = '<xsl:value-of select="IDDoc" />')
		INSERT INTO [ISZL].[T_F011_DOCUMENT_TYPE]
		([IDDOC] ,[DOCNAME] ,[DOCSER] ,[DOCNUM] ,[DATEBEG] ,[DATEEND])
		SELECT
		'<xsl:value-of select="IDDoc" />'--IDDOC, varchar(2),
		,LEFT('<xsl:value-of select="DocName" />', 254)--DOCNAME, varchar(254),
		,LEFT('<xsl:value-of select="DocSer" />', 10)--DOCSER, varchar(10),
		,LEFT('<xsl:value-of select="DocNum" />', 20)--DOCNUM, varchar(20),
		,CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)--DATEBEG, datetime,
		,<xsl:choose>
			<xsl:when test="DATEEND!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>--DATEEND, datetime,
		else
		update f11
		set f11.DOCNAME = LEFT('<xsl:value-of select="DocName" />', 254)
		,f11.DOCSER = LEFT('<xsl:value-of select="DocSer" />', 10)
		,f11.DOCNUM = LEFT('<xsl:value-of select="DocNum" />', 20)
		,f11.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,f11.DATEEND = <xsl:choose>
			<xsl:when test="DATEEND!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>--DATEEND, datetime,
		from [ISZL].[T_F011_DOCUMENT_TYPE] f11
		where f11.IDDOC = '<xsl:value-of select="IDDoc" />'
		GO
	</xsl:template>

	<!-- V017: V017.xml -->
	<xsl:template match="packet[(zglv[type='DispR'])]">
		<xsl:for-each select="zap">
			<xsl:call-template name="DispR" />
		</xsl:for-each>
	</xsl:template>
	<!-- V017 "<zap>" template -->
	<xsl:template name="DispR" >
		if not exists(select * from [ISZL].[T_V017_DISP_RESULT] dk where dk.[IDDR] = <xsl:value-of select="IDDR" />)
		INSERT INTO [ISZL].[T_V017_DISP_RESULT] ([IDDR], [DRNAME], [DATEBEG], [DATEEND])
		SELECT
		<xsl:value-of select="IDDR" />
		, LEFT('<xsl:value-of select="DRNAME" />', 255)
		, 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 d
		set [DRNAME] = LEFT('<xsl:value-of select="DRNAME" />', 255)
		, d.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, d.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V017_DISP_RESULT] d
		where d.[IDDR] = <xsl:value-of select="IDDR" />
		GO
	</xsl:template>

	<!-- N003_OnkT: N003.xml -->
	<xsl:template match="packet[(zglv[type='OnkT'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkT" />
		</xsl:for-each>
	</xsl:template>
	<!-- N003 "<zap>" template -->
	<xsl:template name="OnkT" >
		if not exists(select * from [ISZL].[T_N003_OnkT] onk where onk.[ID_T] = '<xsl:value-of select="ID_T" />')
		INSERT INTO [ISZL].[T_N003_OnkT] ([ID_T], [DS_T],[KOD_T],[T_NAME],[DATEBEG], [DATEEND],[N003ID])
		SELECT '<xsl:value-of select="ID_T" />'
		, LEFT('<xsl:value-of select="DS_T" />', 255)
		, LEFT('<xsl:value-of select="KOD_T" />', 255)
		, LEFT('<xsl:value-of select="T_NAME" />', 255)
		, 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>
		, NewID()
		else
		update onk
		set [DS_T] = LEFT('<xsl:value-of select="DS_T" />', 255)
		, onk.[KOD_T] = LEFT('<xsl:value-of select="KOD_T" />', 255)
		, onk.[T_NAME] = LEFT('<xsl:value-of select="T_NAME" />', 255)
		, onk.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, onk.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N003_OnkT] onk
		where onk.[ID_T] = '<xsl:value-of select="ID_T" />'
		GO
	</xsl:template>

	<!-- N004_OnkN: N004.xml -->
	<xsl:template match="packet[(zglv[type='OnkN'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkN" />
		</xsl:for-each>
	</xsl:template>
	<!-- N004 "<zap>" template -->
	<xsl:template name="OnkN" >
		if not exists(select * from [ISZL].[T_N004_OnkN] onkn where onkn.[ID_N] = <xsl:value-of select="ID_N" />)
		INSERT INTO [ISZL].[T_N004_OnkN] ([ID_N], [DS_N],[KOD_N],[N_NAME],[DATEBEG], [DATEEND],[N004ID])
		SELECT <xsl:value-of select="ID_N" />
		, LEFT('<xsl:value-of select="DS_N" />', 5)
		, LEFT('<xsl:value-of select="KOD_N" />', 5)
		, LEFT('<xsl:value-of select="N_NAME" />', 255)
		, 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>
		, NewID()
		else
		update onkn
		set [DS_N] = LEFT('<xsl:value-of select="DS_N" />', 5)
		, onkn.[KOD_N] = LEFT('<xsl:value-of select="KOD_N" />', 5)
		, onkn.[N_NAME] = LEFT('<xsl:value-of select="N_NAME" />', 255)
		, onkn.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, onkn.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N004_OnkN] onkn
		where onkn.[ID_N] = <xsl:value-of select="ID_N" />
		GO
	</xsl:template>


	<!-- N005_OnkM: N005.xml -->
	<xsl:template match="packet[(zglv[type='OnkM'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkM" />
		</xsl:for-each>
	</xsl:template>
	<!-- N005 "<zap>" template -->
	<xsl:template name="OnkM" >
		if not exists(select * from [ISZL].[T_N005_OnkM] onkm where onkm.[ID_M] = <xsl:value-of select="ID_M" />)
		INSERT INTO [ISZL].[T_N005_OnkM] ([ID_M], [DS_M],[KOD_M],[M_NAME],[DATEBEG], [DATEEND],[N005ID])
		SELECT <xsl:value-of select="ID_M" />
		, LEFT('<xsl:value-of select="DS_M" />', 5)
		, LEFT('<xsl:value-of select="KOD_M" />', 5)
		, LEFT('<xsl:value-of select="M_NAME" />', 255)
		, 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>
		, NewID()
		else
		update onkm
		set [DS_M] = LEFT('<xsl:value-of select="DS_M" />', 5)
		, onkm.[KOD_M] = LEFT('<xsl:value-of select="KOD_M" />', 5)
		, onkm.[M_NAME] = LEFT('<xsl:value-of select="M_NAME" />', 255)
		, onkm.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, onkm.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N005_OnkM] onkm
		where onkm.[ID_M] = <xsl:value-of select="ID_M" />
		GO
	</xsl:template>

	<!-- N006_OnkTNM: N006.xml -->
	<xsl:template match="packet[(zglv[type='OnkTNM'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkTNM" />
		</xsl:for-each>
	</xsl:template>
	<!-- N006 "<zap>" template -->
	<xsl:template name="OnkTNM" >
		if not exists(select * from [ISZL].[T_N006_OnkTNM] onkt where onkt.[ID_gr] = <xsl:value-of select="ID_gr" />)
		INSERT INTO [ISZL].[T_N006_OnkTNM] ([ID_gr], [DS_gr],[ID_St],[ID_T],[ID_N],[ID_M],[DATEBEG],[DATEEND],[N006ID])
		SELECT <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:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update onkt
		set [DS_gr] = LEFT('<xsl:value-of select="DS_gr" />', 5)
		, onkt.[ID_St] = <xsl:value-of select="ID_St" />
		, onkt.[ID_T] = <xsl:value-of select="ID_T" />
		, onkt.[ID_N] = <xsl:value-of select="ID_N" />
		, onkt.[ID_M] = <xsl:value-of select="ID_M" />
		, onkt.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, onkt.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N006_OnkTNM] onkt
		where onkt.[ID_gr] = <xsl:value-of select="ID_gr" />
		GO
	</xsl:template>

	<!-- N007_OnkMrf: N007.xml -->
	<xsl:template match="packet[(zglv[type='OnkMrf'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkMrf" />
		</xsl:for-each>
	</xsl:template>
	<!-- N007 "<zap>" template -->
	<xsl:template name="OnkMrf" >
		if not exists(select * from [ISZL].[T_N007_OnkMrf] omr where omr.[ID_Mrf] = <xsl:value-of select="ID_Mrf" />)
		INSERT INTO [ISZL].[T_N007_OnkMrf] ([ID_Mrf], [Mrf_NAME],[DATEBEG], [DATEEND],[N007ID])
		SELECT <xsl:value-of select="ID_Mrf" />
		, LEFT('<xsl:value-of select="Mrf_NAME" />', 255)
		, 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>
		, NewID()
		else
		update omr
		set [Mrf_NAME] = LEFT('<xsl:value-of select="Mrf_NAME" />', 255)
		, omr.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, omr.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N007_OnkMrf] omr
		where omr.[ID_Mrf] = <xsl:value-of select="ID_Mrf" />
		GO
	</xsl:template>

	<!-- N010_OnkIgh: N010.xml -->
	<xsl:template match="packet[(zglv[type='OnkIgh'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkIgh" />
		</xsl:for-each>
	</xsl:template>
	<!-- N010 "<zap>" template -->
	<xsl:template name="OnkIgh" >
		if not exists(select * from [ISZL].[T_N010_OnkIgh] oi where oi.[ID_Igh] = <xsl:value-of select="ID_Igh" />)
		INSERT INTO [ISZL].[T_N010_OnkIgh] ([ID_Igh], [KOD_Igh],[Igh_NAME],[DATEBEG], [DATEEND],[N010ID])
		SELECT <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:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update oi
		set [KOD_Igh] = LEFT('<xsl:value-of select="KOD_Igh" />', 255)
		, oi.[Igh_NAME] = LEFT('<xsl:value-of select="Igh_NAME" />', 255)
		, oi.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, oi.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N010_OnkIgh] oi
		where oi.[ID_Igh] = <xsl:value-of select="ID_Igh" />
		GO
	</xsl:template>

	<!-- N011_OnkIghRt: N011.xml -->
	<xsl:template match="packet[(zglv[type='OnkIghRt'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkIghRt" />
		</xsl:for-each>
	</xsl:template>
	<!-- N011 "<zap>" template -->
	<xsl:template name="OnkIghRt" >
		if not exists(select * from [ISZL].[T_N011_OnkIghRt] oir where oir.[ID_R_I] = <xsl:value-of select="ID_R_I" />)
		INSERT INTO [ISZL].[T_N011_OnkIghRt] ([ID_R_I], [ID_Igh],[KOD_R_I],[R_I_NAME],[DATEBEG], [DATEEND],[N011ID])
		SELECT <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:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update oir
		set [ID_Igh] = <xsl:value-of select="ID_Igh" />
		, oir.[KOD_R_I] = LEFT('<xsl:value-of select="KOD_R_I" />', 255)
		, oir.[R_I_NAME] = LEFT('<xsl:value-of select="R_I_NAME" />', 255)
		, oir.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, oir.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N011_OnkIghRt] oir
		where oir.[ID_R_I] = <xsl:value-of select="ID_R_I" />
		GO
	</xsl:template>

	<!-- N013_OnkLech: N013.xml -->
	<xsl:template match="packet[(zglv[type='OnkLech'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkLech" />
		</xsl:for-each>
	</xsl:template>
	<!-- N013 "<zap>" template -->
	<xsl:template name="OnkLech" >
		if not exists(select * from [ISZL].[T_N013_OnkLech] okl where okl.[ID_TLech] = <xsl:value-of select="ID_TLech" />)
		INSERT INTO [ISZL].[T_N013_OnkLech] ([ID_TLech], [TLech_NAME],[DATEBEG], [DATEEND],[N013ID])
		SELECT <xsl:value-of select="ID_TLech" />
		, LEFT('<xsl:value-of select="TLech_NAME" />', 255)
		, 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>
		, NewID()
		else
		update okl
		set [TLech_NAME] = LEFT('<xsl:value-of select="TLech_NAME" />', 255)
		, okl.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, okl.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N013_OnkLech] okl
		where okl.[ID_TLech] = <xsl:value-of select="ID_TLech" />
		GO
	</xsl:template>

	<!-- N014_OnkHir: N014.xml -->
	<xsl:template match="packet[(zglv[type='OnkHir'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkHir" />
		</xsl:for-each>
	</xsl:template>
	<!-- N014 "<zap>" template -->
	<xsl:template name="OnkHir" >
		if not exists(select * from [ISZL].[T_N014_OnkHir] oh where oh.[ID_THir] = <xsl:value-of select="ID_THir" />)
		INSERT INTO [ISZL].[T_N014_OnkHir] ([ID_THir], [THir_NAME],[DATEBEG], [DATEEND],[N014ID])
		SELECT <xsl:value-of select="ID_THir" />
		, LEFT('<xsl:value-of select="THir_NAME" />', 255)
		, 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>
		, NewID()
		else
		update oh
		set [THir_NAME] = LEFT('<xsl:value-of select="THir_NAME" />', 255)
		, oh.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, oh.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N014_OnkHir] oh
		where oh.[ID_THir] = <xsl:value-of select="ID_THir" />
		GO
	</xsl:template>

	<!-- N016_OnkLek_V: N016.xml -->
	<xsl:template match="packet[(zglv[type='OnkLek_V'])]">
		<xsl:for-each select="zap">
			<!-- insert or update MedicalService -->
			<xsl:call-template name="OnkLek_V" />
		</xsl:for-each>
	</xsl:template>
	<!-- N016 "<zap>" template -->
	<xsl:template name="OnkLek_V" >
		if not exists(select * from [ISZL].[T_N016_OnkLek_V] olv where olv.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" />)
		INSERT INTO [ISZL].[T_N016_OnkLek_V] ([ID_TLek_V], [TLek_NAME_V],[DATEBEG], [DATEEND],[N016ID])
		SELECT <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:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		, NewID()
		else
		update olv
		set [TLek_NAME_V] = LEFT('<xsl:value-of select="TLek_NAME_V" />', 255)
		, olv.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, olv.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_N016_OnkLek_V] olv
		where olv.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" />
		GO
	</xsl:template>

	<!-- V016_DISPT: V016.xml -->
	<xsl:template match="packet[(zglv[type='DispT'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="DispT" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="DispT" >
		if not exists(select * from ISZL.T_V016_DISPT d where d.IDDT = LEFT('<xsl:value-of select="IDDT" />', 3))
		INSERT INTO  ISZL.T_V016_DISPT (IDDT ,DTNAME, DATEBEG, DATEEND, V016ID, Filter)
		SELECT
		LEFT('<xsl:value-of select="IDDT" />', 3)
		,LEFT('<xsl:value-of select="DTNAME" />', 254)
		,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>
		,NewID()
		,''
		else
		update d
		set d.DTNAME = LEFT('<xsl:value-of select="DTNAME" />', 254)
		,d.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,d.DATEEND = <xsl:choose>
			<xsl:when test="DATEEND!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>
		,d.Filter = '<xsl:value-of select="Filter" />'
		from ISZL.T_V016_DISPT d
		where d.IDDT = LEFT('<xsl:value-of select="IDDT" />', 3)
		GO
	</xsl:template>

	<!-- F006: F006.xml -->
	<xsl:template match="packet[(zglv[type='VidExp'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="VidExp" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="VidExp" >
		if not exists(select * from ISZL.T_F006_CONTROL_TYPE ct where ct.IDVID = '<xsl:value-of select="IDVID" />')
		INSERT INTO ISZL.T_F006_CONTROL_TYPE(IDVID, VIDNAME, DATEBEG, DATEEND, f006controltypeid)
		SELECT
		'<xsl:value-of select="IDVID" />'
		,LEFT('<xsl:value-of select="VIDNAME" />', 350)
		,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>
		,NewID()
		else
		update ct
		set ct.VIDNAME = LEFT('<xsl:value-of select="VIDNAME" />', 350)
		,ct.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,ct.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 ISZL.T_F006_CONTROL_TYPE ct
		where ct.IDVID = '<xsl:value-of select="IDVID" />'
		GO
	</xsl:template>

	<!-- N017: N017.xml -->
	<xsl:template match="packet[(zglv[type='OnkLuch'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="OnkLuch" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="OnkLuch" >
		if not exists(select * from ISZL.T_N017_OnkLuch t where t.ID_TLuch = '<xsl:value-of select="ID_TLuch" />')
		INSERT INTO ISZL.T_N017_OnkLuch(ID_TLuch, TLuch_NAME, DATEBEG, DATEEND, N017ID)
		SELECT
		'<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!=''">
				CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
			</xsl:when>
			<xsl:otherwise>null</xsl:otherwise>
		</xsl:choose>
		,NewID()
		else
		update t
		set t.TLuch_NAME = LEFT('<xsl:value-of select="TLuch_NAME" />', 255)
		,t.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,t.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 ISZL.T_N017_OnkLuch t
		where t.ID_TLuch = '<xsl:value-of select="ID_TLuch" />'
		GO
	</xsl:template>


	<!-- V018_VID_VMP: V018.xml -->
	<xsl:template match="packet[(zglv[type='HVid'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="HVid" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="HVid" >
		if not exists(select * from [ISZL].[T_V018_VID_VMP] vv where vv.[IDHVID] = '<xsl:value-of select="IDHVID" />')
		INSERT INTO  [ISZL].[T_V018_VID_VMP] ([V018VidVmpID], [IDHVID],[HVIDNAME], [DATEBEG], [DATEEND])
		SELECT  NewID()
		,LEFT('<xsl:value-of select="IDHVID" />', 16)
		,LEFT('<xsl:value-of select="HVIDNAME" />', 1024)
		,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 vv
		set
		vv.[HVIDNAME] = LEFT('<xsl:value-of select="HVIDNAME" />', 1024)
		,vv.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,vv.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
		from [ISZL].[T_V018_VID_VMP] vv
		where vv.[IDHVID] = '<xsl:value-of select="IDHVID" />'
		GO
	</xsl:template>

	<!-- V019_METOD_VMP: V019.xml -->
	<xsl:template match="packet[(zglv[type='HMet'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="HMet" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="HMet" >
		if not exists(select * from [ISZL].[T_V019_METOD_VMP] mv where mv.IDHM = '<xsl:value-of select="IDHM" />')
		INSERT INTO  [ISZL].[T_V019_METOD_VMP] ([V019MetodVmpID], [IDHM], [HMNAME], [DIAG], [HVID], [HGR], [HMODP], [IDMODP], [DATEBEG], [DATEEND])
		SELECT  NewID()
		,'<xsl:value-of select="IDHM" />'
		,LEFT('<xsl:value-of select="HMNAME" />', 1024)
		,LEFT('<xsl:value-of select="DIAG" />', 256)
		,LEFT('<xsl:value-of select="HVID" />', 16)
		,<xsl:value-of select="HGR" /><xsl:if test="HGR = ''">NULL</xsl:if>
		,LEFT('<xsl:value-of select="HMODP" />',256)
		,<xsl:value-of select="IDMODP" /><xsl:if test="IDMODP = ''">NULL</xsl:if>
		,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 mv
		set
		mv.[HMNAME] = LEFT('<xsl:value-of select="HMNAME" />', 1024)
		,mv.[DIAG] = LEFT('<xsl:value-of select="DIAG" />', 256)
		,mv.[HVID] = LEFT('<xsl:value-of select="HVID" />', 16)
		,mv.[HGR] = <xsl:value-of select="HGR" /><xsl:if test="HGR = ''">NULL</xsl:if>
		,mv.[HMODP] = LEFT('<xsl:value-of select="HMODP" />',256)
		,mv.[IDMODP] = <xsl:value-of select="IDMODP" /><xsl:if test="IDMODP = ''">NULL</xsl:if>
		,mv.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,mv.[DATEEND] = <xsl:if test="DATEEND != ''">
			CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		</xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>

		from [ISZL].[T_V019_METOD_VMP] mv
		where mv.[IDHM] = '<xsl:value-of select="IDHM" />'
		GO
	</xsl:template>

	<!--Violation Settings-->
	<xsl:template match="violations/groups/group">
		<xsl:call-template name="violationsGroup" />
		<xsl:for-each select="settings/setting">
			<xsl:call-template name="violation" />
			<xsl:call-template name="violationSetting" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="violationsGroup" >
		update vgs
		set vgs.Entities = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@entity"/>'),
		vgs.ViolationsEntities = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@violationEntity"/>'),
		vgs.ViolationCaseEntities = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@violationCaseEntity"/>'),
		vgs.ChildAttributes = '<xsl:value-of select="@childs"/>'
		from ISZL.T_VIOLATIONS_GROUP_SETTING vgs
		where vgs.Name = '<xsl:value-of select="@name"/>'

		INSERT INTO [ISZL].[T_VIOLATIONS_GROUP_SETTING]
		([ViolationsGroupSettingID] ,[Name] ,[Entities] ,[ViolationsEntities] ,[ChildAttributes] ,[ViolationCaseEntities])
		SELECT
		NEWID()--ViolationsGroupSettingID, uniqueidentifier,
		,'<xsl:value-of select="@name"/>'--Name, varchar(255),
		,(select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@entity"/>')--Entities, int,
		,(select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@violationEntity"/>')--ViolationsEntities, int,
		,'<xsl:value-of select="@childs"/>'--ChildAttributes, varchar(max),
		,(select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@violationCaseEntity"/>')--ViolationCaseEntities, int,
		where not exists
		(
		select *
		from [ISZL].[T_VIOLATIONS_GROUP_SETTING] vgs
		where vgs.Name = '<xsl:value-of select="@name"/>'
		)

		delete vs
		from [ISZL].[T_VIOLATION_SETTING] vs
		join [ISZL].[T_VIOLATIONS_GROUP_SETTING] vgs on vgs.ViolationsGroupSettingID = vs.ViolationsGroupSetting
		where vgs.Name = '<xsl:value-of select="@name"/>'
		GO
	</xsl:template>
	<xsl:template name="violation" >
		update v
		set v.Name = '<xsl:value-of select="violation/@name"/>'
		from ISZL.T_VIOLATION v
		where v.Code = '<xsl:value-of select="violation/@code"/>' and v.type_ = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="violation/@type"/>')

		INSERT INTO [ISZL].[T_VIOLATION]
		([ViolationID] ,[Code] ,[Name] ,[type_])
		SELECT
		NEWID()--ViolationID, uniqueidentifier
		,'<xsl:value-of select="violation/@code"/>'--Code, varchar(11)
		,'<xsl:value-of select="violation/@name"/>'--Name, varchar(255)
		,(select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="violation/@type"/>')--type_, int)
		where not exists
		(
		select *
		from [ISZL].[T_VIOLATION] v
		where v.Code = '<xsl:value-of select="violation/@code"/>' and v.type_ = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="violation/@type"/>')
		)
		GO
	</xsl:template>
	<xsl:template name="violationSetting" >
		INSERT INTO [ISZL].[T_VIOLATION_SETTING]
		([ViolationSettingID] ,[ViolationsGroupSetting] ,[Entities] ,[Filter] ,[Violation] ,[Alias] ,[OrderNum])
		SELECT
		NEWID()--ViolationSettingID, uniqueidentifier
		,vgs.ViolationsGroupSettingID--ViolationsGroupSetting, uniqueidentifier
		,(select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="@entity"/>')--Entities, int
		,'<xsl:call-template name="serializeNodeToString">
			<xsl:with-param name="node" select="filter/*"/>
		</xsl:call-template>'--Filter, varchar(max)
		,(select v.ViolationID from ISZL.T_VIOLATION v where v.Code = '<xsl:value-of select="violation/@code"/>' and v.type_ = (select EntityID from VCLib.T_ENTITIES where [Schema] + '.' + EntName = '<xsl:value-of select="violation/@type"/>'))--Violation, uniqueidentifier
		,'<xsl:value-of select="@alias"/>'--Alias, varchar(255)
		,<xsl:value-of select="@order"/>--OrderNum, int)
		from ISZL.T_VIOLATIONS_GROUP_SETTING vgs
		where vgs.Name = '<xsl:value-of select="../../@name"/>'
		GO
	</xsl:template>

	<xsl:template name="serializeNodeToString">
		<xsl:param name="node" />
		<xsl:variable name="name" select="name($node)" />
		<xsl:text>&lt;</xsl:text>
		<xsl:value-of select="$name" />
		<xsl:for-each select="$node/@*">
			<xsl:text> </xsl:text>
			<xsl:value-of select="name()" />
			<xsl:text>=&quot;</xsl:text>
			<xsl:call-template name ="replaceSpecCharacters">
				<xsl:with-param name="text" select="."/>
			</xsl:call-template>
			<xsl:text>&quot;</xsl:text>
		</xsl:for-each>
		<xsl:if test="namespace-uri($node)">
			<xsl:text> xmlns:</xsl:text>
			<xsl:value-of select="substring-before(name($node),':')"/>
			<xsl:text>="</xsl:text>
			<xsl:value-of select="namespace-uri($node)"/>
			<xsl:text>"</xsl:text>
		</xsl:if>
		<xsl:text>&gt;</xsl:text>
		<xsl:if test="./text()">
			<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
			<xsl:value-of select="./text()" />
			<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
		</xsl:if>
		<xsl:for-each select="$node/*">
			<xsl:call-template name="serializeNodeToString">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
		</xsl:for-each>
		<xsl:text>&lt;/</xsl:text>
		<xsl:value-of select="$name" />
		<xsl:text>&gt;</xsl:text>
	</xsl:template>

	<xsl:template name="replaceSpecCharacters">
		<xsl:param name="text" />
		<xsl:call-template name ="replace">
			<xsl:with-param name="text">
				<xsl:call-template name ="replace">
					<xsl:with-param name="text">
						<xsl:call-template name ="replace">
							<xsl:with-param name="text">
								<xsl:call-template name ="replace">
									<xsl:with-param name="text">
										<xsl:call-template name ="replace">
											<xsl:with-param name="text" select="$text"/>
											<xsl:with-param name="replace">&amp;</xsl:with-param>
											<xsl:with-param name="by">&amp;amp;</xsl:with-param>
										</xsl:call-template>
									</xsl:with-param>
									<xsl:with-param name="replace">&lt;</xsl:with-param>
									<xsl:with-param name="by">&amp;lt;</xsl:with-param>
								</xsl:call-template>
							</xsl:with-param>
							<xsl:with-param name="replace">&gt;</xsl:with-param>
							<xsl:with-param name="by">&amp;gt;</xsl:with-param>
						</xsl:call-template>
					</xsl:with-param>
					<xsl:with-param name="replace">&apos;</xsl:with-param>
					<xsl:with-param name="by">&amp;apos;</xsl:with-param>
				</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="replace">&quot;</xsl:with-param>
			<xsl:with-param name="by">&amp;quot;</xsl:with-param>
		</xsl:call-template>
	</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>

	<!-- V025_DISPT: V025.xml -->
	<xsl:template match="packet[(zglv[type='KPC'])]">
		<xsl:for-each select="zap">
			<!-- insert or update -->
			<xsl:call-template name="KPC" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="KPC" >
		if not exists(select * from ISZL.T_V025_KPC kpc where kpc.IDPC = LEFT('<xsl:value-of select="IDPC" />', 3))
		INSERT INTO  ISZL.T_V025_KPC (IDPC ,N_PC, DATEBEG, DATEEND, V025KpcID)
		SELECT
		LEFT('<xsl:value-of select="IDPC" />', 3)
		,LEFT('<xsl:value-of select="N_PC" />', 254)
		,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>
		,NewID()
		else
		update kpc
		set kpc.N_PC = LEFT('<xsl:value-of select="N_PC" />', 254)
		,kpc.DATEBEG = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		,kpc.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 ISZL.T_V025_KPC kpc
		where kpc.IDPC = LEFT('<xsl:value-of select="IDPC" />', 3)
		GO
	</xsl:template>

	<xsl:template match="root/MkbNamedRanges">
		<xsl:for-each select="MkbNamedRange">
			DECLARE @Name varchar(100)
			DECLARE @MkbFrom varchar(10)
			DECLARE @MkbTo varchar(10)

			select @Name =	<xsl:choose>
				<xsl:when test="Name">
					'<xsl:value-of select="Name" />'
				</xsl:when>
				<xsl:otherwise>NULL</xsl:otherwise>
			</xsl:choose>

			select @MkbFrom =	<xsl:choose>
				<xsl:when test="MkbFrom">
					'<xsl:value-of select="MkbFrom" />'
				</xsl:when>
				<xsl:otherwise>NULL</xsl:otherwise>
			</xsl:choose>

			select @MkbTo =	<xsl:choose>
				<xsl:when test="MkbTo">
					'<xsl:value-of select="MkbTo" />'
				</xsl:when>
				<xsl:otherwise>NULL</xsl:otherwise>
			</xsl:choose>

			IF NOT EXISTS (SELECT 1 FROM ISZL.T_MKB_NAMED_RANGE WHERE [Name] = @Name and [MkbFrom] = @MkbFrom and [MkbTo] = @MkbTo)
			BEGIN
			INSERT INTO ISZL.T_MKB_NAMED_RANGE(MkbNamedRangeID, [Name], [MkbFrom], [MkbTo])
			VALUES(newid()
			,@Name
			,@MkbFrom
			,@MkbTo
			)
			END

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

	<!-- F031_ERMO: F031.xml -->
	<xsl:template match="packet[(zglv[type='ERMO'])]">
		<xsl:for-each select="zap">
			<!-- insert or update F031_ERMO <zap> -->
			<xsl:call-template name="ERMO" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="ERMO">
		IF NOT EXISTS(SELECT * FROM [ISZL].[T_F031_ERMO] s WHERE s.[IDMO] = LEFT('<xsl:value-of select="IDMO" />', 17))
		INSERT INTO [ISZL].[T_F031_ERMO] (F031ErmoID, IDMO, NAM_MOP, NAM_MOK, INN, KPP, OGRN, OID_MO, OKOPF, OKFS, ADDR_J, ADDR_J_GAR, OKTMO, EMAIL, PHONE, FAX, DATEBEG, DATEEND)
		SELECT
		NEWID()
		, LEFT('<xsl:value-of select="IDMO" />', 17)
		, LEFT('<xsl:value-of select="NAM_MOP" />', 2500)
		, LEFT('<xsl:value-of select="NAM_MOK" />', 2500)
		, LEFT('<xsl:value-of select="INN" />', 12)
		, LEFT('<xsl:value-of select="KPP" />', 9)
		, LEFT('<xsl:value-of select="OGRN" />', 15)
		, LEFT('<xsl:value-of select="OID_MO" />', 35)
		, <xsl:choose>
			<xsl:when test="OKOPF=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKOPF" />', 5)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OKFS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKFS" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="ADDR_J=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="ADDR_J" />', 300)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="ADDR_J_GAR=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="ADDR_J_GAR" />', 36)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OKTMO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKTMO" />', 11)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="EMAIL=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="EMAIL" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="PHONE=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="PHONE" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="FAX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="FAX" />', 250)
			</xsl:otherwise>
		</xsl:choose>
		, CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		ELSE
		UPDATE s
		SET s.[NAM_MOP] = LEFT('<xsl:value-of select="NAM_MOP" />', 2500)
		, s.[NAM_MOK] = LEFT('<xsl:value-of select="NAM_MOK" />', 2500)
		, s.[INN] = LEFT('<xsl:value-of select="INN" />', 12)
		, s.[KPP] = LEFT('<xsl:value-of select="KPP" />', 9)
		, s.[OGRN] = LEFT('<xsl:value-of select="OGRN" />', 15)
		, s.[OID_MO] = LEFT('<xsl:value-of select="OID_MO" />', 35)
		, s.[OKOPF] = <xsl:choose>
			<xsl:when test="OKOPF=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKOPF" />', 5)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OKFS] = <xsl:choose>
			<xsl:when test="OKFS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKFS" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, s.[ADDR_J] = <xsl:choose>
			<xsl:when test="ADDR_J=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="ADDR_J" />', 300)
			</xsl:otherwise>
		</xsl:choose>
		, s.[ADDR_J_GAR] = <xsl:choose>
			<xsl:when test="ADDR_J_GAR=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="ADDR_J_GAR" />', 36)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OKTMO] = <xsl:choose>
			<xsl:when test="OKTMO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKTMO" />', 11)
			</xsl:otherwise>
		</xsl:choose>
		, s.[EMAIL] = <xsl:choose>
			<xsl:when test="EMAIL=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="EMAIL" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, s.[PHONE] = <xsl:choose>
			<xsl:when test="PHONE=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="PHONE" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, s.[FAX] = <xsl:choose>
			<xsl:when test="FAX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="FAX" />', 250)
			</xsl:otherwise>
		</xsl:choose>
		, s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, s.[DATEEND] = CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		FROM [ISZL].[T_F031_ERMO] s
		WHERE s.[IDMO] = LEFT('<xsl:value-of select="IDMO" />', 17)
		GO
	</xsl:template>

	<!-- F032_TRMO: F032.xml -->
	<xsl:template match="packet[(zglv[type='TRMO'])]">
		<xsl:for-each select="zap">
			<!-- insert or update F032_TRMO <zap> -->
			<xsl:call-template name="TRMO" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="TRMO">
		IF NOT EXISTS(SELECT * FROM [ISZL].[T_F032_TRMO] s WHERE s.[UIDMO] = LEFT('<xsl:value-of select="UIDMO" />', 17))
		INSERT INTO [ISZL].[T_F032_TRMO] (F032TrmoID, UIDMO, IDMO, MCOD, OKTMO_P, SUBJ, D_BEGIN, D_BEGIN_OMS, D_END,
		NAME_E, OSP, 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)
		SELECT
		NEWID()
		, LEFT('<xsl:value-of select="UIDMO" />', 17)
		, LEFT('<xsl:value-of select="IDMO" />', 17)
		, LEFT('<xsl:value-of select="MCOD" />', 6)
		, LEFT('<xsl:value-of select="OKTMO_P" />', 11)
		, LEFT('<xsl:value-of select="SUBJ" />', 150)
		, CONVERT(datetime, '<xsl:value-of select="D_BEGIN" />', 104)
		, CONVERT(datetime, '<xsl:value-of select="D_BEGIN_OMS" />', 104)
		, <xsl:choose>
			<xsl:when test="D_END=''">NULL</xsl:when>
			<xsl:otherwise>
				CONVERT(datetime, '<xsl:value-of select="D_END" />', 104)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="NAME_E=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="NAME_E" />', 1)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OSP='true'">1</xsl:when>
			<xsl:when test="OSP='false'">0</xsl:when>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="VID_MO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="VID_MO" />', 1000)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OID_MO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OID_MO" />', 50)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OID_SPMO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OID_SPMO" />', 50)
			</xsl:otherwise>
		</xsl:choose>
		, LEFT('<xsl:value-of select="NAM_MOP" />', 2500)
		, LEFT('<xsl:value-of select="NAM_MOK" />', 2500)
		, LEFT('<xsl:value-of select="INN" />', 12)
		, LEFT('<xsl:value-of select="KPP" />', 9)
		, LEFT('<xsl:value-of select="OGRN" />', 15)
		, <xsl:choose>
			<xsl:when test="JURADDRESS_INDEX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="JURADDRESS_INDEX" />', 6)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="JURADDRESS_ADDRESS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="JURADDRESS_ADDRESS" />', 300)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="GAR_ADDRESS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="GAR_ADDRESS" />', 36)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="OKFS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKFS" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="VEDPRI=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="VEDPRI" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="PHONE=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="PHONE" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="FAX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="FAX" />', 250)
			</xsl:otherwise>
		</xsl:choose>
		, <xsl:choose>
			<xsl:when test="EMAIL=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="EMAIL" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		ELSE
		UPDATE s
		SET s.[IDMO] = LEFT('<xsl:value-of select="IDMO" />', 17)
		, s.[MCOD] = LEFT('<xsl:value-of select="MCOD" />', 6)
		, s.[OKTMO_P] = LEFT('<xsl:value-of select="OKTMO_P" />', 11)
		, s.[SUBJ] = LEFT('<xsl:value-of select="SUBJ" />', 150)
		, s.[D_BEGIN] = CONVERT(datetime, '<xsl:value-of select="D_BEGIN" />', 104)
		, s.[D_BEGIN_OMS] = CONVERT(datetime, '<xsl:value-of select="D_BEGIN_OMS" />', 104)
		, s.[D_END] = <xsl:choose>
			<xsl:when test="D_END=''">NULL</xsl:when>
			<xsl:otherwise>
				CONVERT(datetime, '<xsl:value-of select="D_END" />', 104)
			</xsl:otherwise>
		</xsl:choose>
		, s.[NAME_E] = <xsl:choose>
			<xsl:when test="NAME_E=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="NAME_E" />', 1)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OSP] = <xsl:choose>
			<xsl:when test="OSP='true'">1</xsl:when>
			<xsl:when test="OSP='false'">0</xsl:when>
		</xsl:choose>
		, s.[VID_MO] =  <xsl:choose>
			<xsl:when test="VID_MO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="VID_MO" />', 1000)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OID_MO] = <xsl:choose>
			<xsl:when test="OID_MO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OID_MO" />', 50)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OID_SPMO] = <xsl:choose>
			<xsl:when test="OID_SPMO=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OID_SPMO" />', 50)
			</xsl:otherwise>
		</xsl:choose>
		, s.[NAM_MOP] = LEFT('<xsl:value-of select="NAM_MOP" />', 2500)
		, s.[NAM_MOK] = LEFT('<xsl:value-of select="NAM_MOK" />', 2500)
		, s.[INN] = LEFT('<xsl:value-of select="INN" />', 12)
		, s.[KPP] = LEFT('<xsl:value-of select="KPP" />', 9)
		, s.[OGRN] = LEFT('<xsl:value-of select="OGRN" />', 15)
		, s.[JURADDRESS_INDEX] = <xsl:choose>
			<xsl:when test="JURADDRESS_INDEX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="JURADDRESS_INDEX" />', 6)
			</xsl:otherwise>
		</xsl:choose>
		, s.[JURADDRESS_ADDRESS] = <xsl:choose>
			<xsl:when test="JURADDRESS_ADDRESS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="JURADDRESS_ADDRESS" />', 300)
			</xsl:otherwise>
		</xsl:choose>
		, s.[GAR_ADDRESS] = <xsl:choose>
			<xsl:when test="GAR_ADDRESS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="GAR_ADDRESS" />', 36)
			</xsl:otherwise>
		</xsl:choose>
		, s.[OKFS] = <xsl:choose>
			<xsl:when test="OKFS=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="OKFS" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, s.[VEDPRI] = <xsl:choose>
			<xsl:when test="VEDPRI=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="VEDPRI" />', 2)
			</xsl:otherwise>
		</xsl:choose>
		, s.[PHONE] = <xsl:choose>
			<xsl:when test="PHONE=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="PHONE" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, s.[FAX] = <xsl:choose>
			<xsl:when test="FAX=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="FAX" />', 250)
			</xsl:otherwise>
		</xsl:choose>
		, s.[EMAIL] = <xsl:choose>
			<xsl:when test="EMAIL=''">NULL</xsl:when>
			<xsl:otherwise>
				LEFT('<xsl:value-of select="EMAIL" />', 255)
			</xsl:otherwise>
		</xsl:choose>
		, s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
		, s.[DATEEND] = CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
		FROM [ISZL].[T_F032_TRMO] s
		WHERE s.[UIDMO] = LEFT('<xsl:value-of select="UIDMO" />', 17)
		GO
	</xsl:template>

</xsl:stylesheet>
