<?xml version="1.0"?>
<xsl:transform version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:n1="urn:hl7-org:v3"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:n2="urn:hl7-org:v3/meta/voc"
    xmlns:voc="urn:hl7-org:v3/voc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:fias="urn:hl7-ru:fias"
	xmlns:medService="urn:hl7-ru:medService"
    xmlns:identity="urn:hl7-ru:identity"
    xmlns:address="urn:hl7-ru:address">
    <xsl:output method="html" indent="yes" encoding="utf-8"/>
    <xsl:template match="/">
        <xsl:apply-templates select="n1:ClinicalDocument"/>
    </xsl:template>
    <xsl:template match="n1:ClinicalDocument">
	    <html>
		<!-- Файл для трансформации XML->HTML->PDF при помощи библиотек iText Core 5.5.13.2, iText XMLWorker 5.5.13.2
             Данный файл должен использоваться только для трансформации документа СЭМД (119) Протокол консультации (CDA) Редакция 4, v1.0
	         Разработан по заказу Минздрава РФ компанией ООО "Цифромед" 11.07.2023
	         Замечания по данному файлу можно направить на почту egisz@stp-egisz.ru -->
            <head>
                <style>
                    @page {
                        size: A4 portrait;
					    margin-top: 0;
					    margin-bottom: 50px;
					    margin-left: 10px;
					    margin-right: 20px;
                    }

                    table {
                        table-layout: fixed;
                        width: 98%;
                        border-collapse: collapse;

                        font-family: Arial, sans-serif;
                        word-wrap: break-word;
					    margin-top: 0;
                        margin-bottom: 10px;
					    margin-left: 20px;
                    }

                    td {
					    vertical-align: top;
					    padding: 2;
                    }

					.table-break {
					    margin-left: 0;
						border-collapse: collapse;
					}

					.param {
					    font-size: 15px;
					    border: 1px solid gray;

                        page-break-inside: avoid;
						word-wrap: none;
					}

					.created {
					    vertical-align: middle;
					}

                    .medorg {
                        text-align: left;
                        font-size: 15px;
                        font-weight: 400;
                        color: #000000;
                        margin-top: 5px;
                        margin-bottom: 5px;
                        white-space: normal;
                    }

                    .address {
                        text-align: left;
                        font-size: 15px;
                        font-weight: 100;
                        color: #000000;
                        margin-top: 5px;
                        margin-bottom: 5px;
                        white-space: normal;
                    }

                    .emd {
                        text-align: center;
                        font-size: 18px;
                        font-weight: 400;
                        color: #000000;
                        margin-top: 25px;
                        margin-bottom: 30px;
                        white-space: normal;
                    }

                    .date {
                        text-align: center;
                        font-size: 15px;
                        font-weight: 400;
                        color: #000000;
                        margin-top: 5px;
                        margin-bottom: 5px;
                        white-space: normal;
                    }

					.tablegray {
                        text-align: left;
                        font-size: 15px;
                        font-weight: 100;
                        color: #aaaaaa;
                        margin-top: 5px;
                        margin-bottom: 5px;
                    }

					.tableblack {
                        text-align: left;
                        font-size: 15px;
                        font-weight: 100;
                        color: #000000;
                        margin-top: 0;
                        margin-bottom: 10px;
                        white-space: normal;
                    }

					.createdate {
                        text-align: right;
                        font-size: 15px;
                        font-weight: 100;
                        color: #aaaaaa;
					    margin-right: 10px;
                    }

					.pagenumber {
                        text-align: center;
					    valign: center;
                        font-size: 15px;
                        font-weight: 100;
                        color: #aaaaaa;
					    margin-right: 10px;
                    }

					h2 + p {
                        page-break-inside: avoid;
                    }

					th {
					    text-align: left;
					    font-size: 15px;
						padding: 5px;
					}

					.first-block-form {
					}

					.block-form {
					    border-bottom: 1px solid #e0e0e0;
						page-break-before: avoid;
                        page-break-inside: avoid;
					}

					.end-block-form {
					}
                </style>
            </head>
			<body>
			        <table class="first-block-form">
			            <tr>
                            <td colspan="8">
                                <p class="medorg">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:custodian/n1:assignedCustodian/n1:representedCustodianOrganization[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:custodian/n1:assignedCustodian/n1:representedCustodianOrganization/n1:name"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text> </xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
						    	<p class="medorg">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:custodian/n1:assignedCustodian/n1:representedCustodianOrganization/n1:addr[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:custodian/n1:assignedCustodian/n1:representedCustodianOrganization/n1:addr/n1:streetAddressLine"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text> </xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
								<xsl:choose>
                                    <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:providerOrganization/identity:Props/identity:Ogrn[not(@nullFlavor)]">
                                        <p class="medorg">
										    <xsl:text>ОГРН: </xsl:text>
											<xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:providerOrganization/identity:Props/identity:Ogrn"/>
										</p>
                                    </xsl:when>
                                </xsl:choose>
								<xsl:choose>
                                    <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:providerOrganization/n1:telecom[not(@nullFlavor)]">
                                        <p class="medorg">
										 <xsl:text>Контакты: </xsl:text>
						    	        	<xsl:call-template name="formatContact">
                                                <xsl:with-param name="contact" select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:providerOrganization/n1:telecom[1]/@value"/>
                                            </xsl:call-template>
                                        </p>
                                    </xsl:when>
                                </xsl:choose>
						    </td>
                        </tr>
					</table>
					<table class="block-form">
				        <tr>
                            <td colspan="8">
                                <h1 class="emd">
								    <b>
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:code[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:code/@displayName"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>[Название вида медицинского документа]</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
									</b>
                                </h1>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Пациент</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:name[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:name"/>
                                        </xsl:when>
                                    </xsl:choose>
                                </p>
                            </td>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Полис ОМС</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:InsurancePolicy[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:InsurancePolicy/identity:Series"/>
								    		<xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:InsurancePolicy/identity:Number"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Дата рождения (возраст)</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:birthTime[not(@nullFlavor)]">
                                            <xsl:call-template name="formatDate">
                                                <xsl:with-param name="newdate" select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:birthTime/@value"/>
                                            </xsl:call-template>&#160;
                                            <xsl:text> (</xsl:text>
								    		<xsl:call-template name="getAge">
                                                <xsl:with-param name="birthdate" select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:birthTime/@value"/>
								    			<xsl:with-param name="deathdate" select="/n1:ClinicalDocument/n1:effectiveTime/@value"/>
								    			<xsl:with-param name="withtext" select="1"/>
                                            </xsl:call-template>
								    		<xsl:text>)</xsl:text>
								    	</xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>СНИЛС</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:id[2]/@extension[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:id[2]/@extension"/>
                                        </xsl:when>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Пол</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:administrativeGenderCode[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:patient/n1:administrativeGenderCode/@displayName"/>
                                        </xsl:when>
                                    </xsl:choose>
                                </p>
                            </td>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Номер карты</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:componentOf/n1:encompassingEncounter/n1:id[2]/@extension[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:componentOf/n1:encompassingEncounter/n1:id[2]/@extension"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
					        <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Адрес постоянной регистрации</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:addr[address:Type/@code='1']/n1:streetAddressLine[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:addr[address:Type/@code='1']/n1:streetAddressLine"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Адрес фактического проживания</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:addr[address:Type/@code='3']/n1:streetAddressLine[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:addr[address:Type/@code='3']/n1:streetAddressLine"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Документ, удостоверяющий личность</xsl:text>
                                </h2>
								<p class="tableblack">
					    		    <xsl:choose>
					    			    <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc[not(@nullFlavor)]">
					    				    <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IdentityCardType/@displayName"/>&#160;
					    				    <xsl:choose>
					    			            <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:Series[not(@nullFlavor)]">
                                                    <xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:Series"/>&#160;
					    				        </xsl:when>
					    				    </xsl:choose>
					    					<xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:Number"/>&#160;
					    				    <xsl:choose>
					    			            <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IssueOrgName[not(@nullFlavor)]">
					    				        	<xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IssueOrgName"/>&#160;
					    				        </xsl:when>
					    				    </xsl:choose>
					    				    <xsl:choose>
					    			            <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IssueOrgCode[not(@nullFlavor)]">
					    				        	<xsl:value-of select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IssueOrgCode"/>&#160;
					    				        </xsl:when>
					    				    </xsl:choose>
					    					<xsl:text>Дата выдачи: </xsl:text>
					    					<xsl:call-template name="formatDate">
                                                <xsl:with-param name="newdate" select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/identity:IdentityDoc/identity:IssueDate/@value"/>
                                            </xsl:call-template>
                                        </xsl:when>
					    				<xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
					    </tr>
					    <xsl:choose>
					        <xsl:when test="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:telecom[not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Контактные данные</xsl:text>
                                        </h2>
										<xsl:for-each select="/n1:ClinicalDocument/n1:recordTarget/n1:patientRole/n1:telecom">
										<p class="tableblack">
					            			<xsl:call-template name="formatContact">
                                                <xsl:with-param name="contact" select="./@value"/>
                                            </xsl:call-template>
                                        </p>
										</xsl:for-each>
                                    </td>
                                </tr>
			                </xsl:when>
			            </xsl:choose>
					</table>
					<table class="block-form">
					    <tr>
					        <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Шифр по МКБ-10</xsl:text>
                                </h2>
								<xsl:choose>
								    <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='809'][not(@nullFlavor)]">
								        <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='809']">
								        <p class="tableblack">
                                                <xsl:value-of select="./n1:value/@code"/>&#160;
					    		        		<xsl:value-of select="./n1:value/@displayName"/>
                                            <br/>
                                        </p>
								        </xsl:for-each>
									</xsl:when>
									<xsl:otherwise>
                                        <xsl:text>-</xsl:text>
                                    </xsl:otherwise>
								</xsl:choose>
                            </td>
                        </tr>
						<xsl:choose>
                            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='800'][not(@nullFlavor)]">
						        <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Вид обращения</xsl:text>
                                        </h2>
						        		<p class="tableblack">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='800']/n1:value/@displayName"/>
                                        </p>
                                    </td>
                                </tr>
							</xsl:when>
                        </xsl:choose>
					    <xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7003'][not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Заболевание выявлено при профилактическом осмотре или обращении за лечением</xsl:text>
                                        </h2>
										<p class="tableblack">
					            		    <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7003']/n1:value/@displayName"/>
                                        </p>
                                    </td>
                                </tr>
					        </xsl:when>
					    </xsl:choose>
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Место проведения</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='801'][not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='801']/n1:value/@displayName"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <xsl:choose>
                            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002'][not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Начало диспансерного наблюдения</xsl:text>
                                        </h2>
										<p class="tableblack">
                                            <xsl:choose>
                                                <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002'][not(@nullFlavor)]">
                                                   <xsl:call-template name="formatDate">
                                                        <xsl:with-param name="newdate" select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002']/n1:effectiveTime/@value"/>
                                                    </xsl:call-template>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                    <xsl:text>-</xsl:text>
                                                </xsl:otherwise>
                                            </xsl:choose>
                                        </p>
                                    </td>
									<td colspan="4">
									    <xsl:choose>
                                            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002']/n1:entryRelationship/n1:observation[n1:code/@code='7004'][not(@nullFlavor)]">
												<h2 class="tablegray">
                                                    <xsl:text>Дата прекращения диспансерного наблюдения</xsl:text>
                                                </h2>
										        <p class="tableblack">
                                                    <xsl:choose>
                                                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002']/n1:entryRelationship/n1:observation[n1:code/@code='7004'][not(@nullFlavor)]">
                                                           <xsl:call-template name="formatDate">
                                                                <xsl:with-param name="newdate" select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation[n1:code/@code='7002']/n1:entryRelationship/n1:observation[n1:code/@code='7004']/n1:effectiveTime/@value"/>
                                                            </xsl:call-template>
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                            <xsl:text>-</xsl:text>
                                                        </xsl:otherwise>
                                                    </xsl:choose>
                                                </p>
					                        </xsl:when>
					                    </xsl:choose>
									</td>
                                </tr>
					        </xsl:when>
					    </xsl:choose>
						<xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation/n1:entryRelationship/n1:observation[n1:code/@code='7004']/n1:value[@codeSystem='1.2.643.5.1.13.13.11.1045'][not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Причина прекращения диспансерного наблюдения</xsl:text>
                                        </h2>
										<p class="tableblack">
					            		    <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation/n1:entryRelationship/n1:observation[n1:code/@code='7004']/n1:value[@codeSystem='1.2.643.5.1.13.13.11.1045']/@displayName"/>
                                        </p>
                                    </td>
                                </tr>
					        </xsl:when>
					    </xsl:choose>
					    <xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation/n1:entryRelationship/n1:observation[n1:code/@code='7005'][not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Следующая явка</xsl:text>
                                        </h2>
										<p class="tableblack">
					            		    <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='DOCINFO']/n1:entry/n1:observation/n1:entryRelationship/n1:observation[n1:code/@code='7005']/n1:value"/>
                                        </p>
                                    </td>
                                </tr>
					        </xsl:when>
					    </xsl:choose>
					</table>
					<xsl:choose>
				        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='BENEFITS']">
					        <table class="block-form">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Льготы</xsl:text>
                                        </h2>
					            		<xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='BENEFITS']/n1:entry/n1:observation">
                                        <p class="tableblack">
                                                <xsl:value-of select="./n1:code/@displayName"/>
					            				<xsl:text>: </xsl:text>
					            				<xsl:value-of select="./n1:value/@displayName"/>
                                            <br/>
                                        </p>
					                    </xsl:for-each>
                                    </td>
                                </tr>
					        </table>
					    </xsl:when>
					</xsl:choose>
					<table class="block-form">
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Анамнез заболевания</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='ANAM']/n1:entry/n1:observation[n1:code/@code='7006']/n1:value[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='ANAM']/n1:entry/n1:observation[n1:code/@code='7006']/n1:value"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
						<tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Анамнез жизни</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@code='7006']/n1:value[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@code='7006']/n1:value"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
							    <p>
						        <xsl:choose>
				                    <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1053'][not(@nullFlavor)]">
                                        <tr>
                                            <td colspan="8">
						                	    <h3 class="tableblack">
						                		    <u><xsl:text>Инвалидность:</xsl:text></u>
						                		</h3>
						                	    <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1053']">
                                                <p class="tableblack">
                                                    <xsl:value-of select="./n1:code/@displayName"/>.
					                    			<xsl:value-of select="./n1:code/n1:qualifier/n1:value/@displayName"/>,
						                			<xsl:value-of select="./n1:entryRelationship/n1:observation[n1:code/@code='4115']/n1:value/@displayName"/>
						                			<xsl:text> (</xsl:text>
						                			<xsl:call-template name="formatDate">
                                                        <xsl:with-param name="newdate" select="./n1:effectiveTime/n1:low/@value"/>
                                                    </xsl:call-template>
						                			<xsl:choose>
				                                        <xsl:when test="./n1:effectiveTime/n1:high[not(@nullFlavor)]">
						                				    <xsl:text> &#8212; </xsl:text>
						                				    <xsl:call-template name="formatDate">
                                                                <xsl:with-param name="newdate" select="./n1:effectiveTime/n1:high/@value"/>
                                                            </xsl:call-template>
						                			    </xsl:when>
					                                </xsl:choose>
						                			<xsl:text>)</xsl:text>
                                                </p>
						                		</xsl:for-each>
                                            </td>
						                </tr>
					                </xsl:when>
					            </xsl:choose>
						        <xsl:choose>
				                    <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1059'][not(@nullFlavor)]">
                                        <tr>
                                            <td colspan="8">
						                	    <h3 class="tableblack">
						                		    <u><xsl:text>Потенциально-опасные для здоровья социальные факторы:</xsl:text></u>
						                		</h3>
						                	    <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1059']">
                                                <p class="tableblack">
                                                    <xsl:value-of select="./n1:code/@displayName"/>
                                                </p>
						                		</xsl:for-each>
                                            </td>
						                </tr>
					                </xsl:when>
					            </xsl:choose>
						        <xsl:choose>
				                    <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1060'][not(@nullFlavor)]">
                                        <tr>
                                            <td colspan="8">
						                	    <h3 class="tableblack">
						                		    <u><xsl:text>Вредные производственные факторы:</xsl:text></u>
						                		</h3>
						                	    <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1060']">
                                                <p class="tableblack">
                                                    <xsl:value-of select="./n1:code/@displayName"/>
                                                </p>
						                		</xsl:for-each>
                                            </td>
						                </tr>
					                </xsl:when>
					            </xsl:choose>
						        <xsl:choose>
				                    <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1058'][not(@nullFlavor)]">
                                        <tr>
                                            <td colspan="8">
						                	    <h3 class="tableblack">
						                		    <u><xsl:text>Вредные привычки и зависимости:</xsl:text></u>
						                		</h3>
						                	    <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LANAM']/n1:entry/n1:observation[n1:code/@codeSystem='1.2.643.5.1.13.13.11.1058']">
                                                <p class="tableblack">
                                                    <xsl:value-of select="./n1:code/@displayName"/>
                                                </p>
						                		</xsl:for-each>
                                            </td>
						                </tr>
					                </xsl:when>
					            </xsl:choose>
								</p>
							</td>
                        </tr>
					</table>
					<table class="block-form">
					    <xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='VITALPARAM']/n1:entry">
                                <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
 					            		    <xsl:text>Витальные параметры</xsl:text>
                                        </h2>
										<p class="tableblack">
					                        <tr>
					                            <th class="param" colspan="4">Параметр</th>
					            	            <th class="param">Значение</th>
					            	            <th class="param">Ед. изм.</th>
					            	        </tr>
					            	        <xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='VITALPARAM']/n1:entry/n1:organizer[n1:statusCode/@code='completed']/n1:component/n1:observation">
					            	        <tr>
                                                <td class="param" colspan="4">
                                                    <p class="tableblack">
					            	            			<xsl:value-of select="./n1:code/@displayName"/>
					            	            	</p>
                                                </td>
					            	        	<td class="param">
                                                    <p class="tableblack">
					            	            			<xsl:value-of select="./n1:value/n1:translation/@value"/>
					            	            	</p>
                                                </td>
					            	        	<td class="param">
                                                    <p class="tableblack">
					            	            		<xsl:value-of select="./n1:value/n1:translation/@displayName"/>
					            	            	</p>
                                                </td>
					            	        </tr>
					            	        </xsl:for-each>
										</p>
									</td>
					            </tr>
				            </xsl:when>
			            </xsl:choose>
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='804'][not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='804']/n1:code/@displayName"/>
                                        </xsl:when>
					                </xsl:choose>
					    		</h2>
								<p class="tableblack">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='804'][not(@nullFlavor)]">
					    		        	<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='804']/n1:value/@displayName"/>
					    		        </xsl:when>
					                </xsl:choose>
                                </p>
                            </td>
                        </tr>
					</table>
					<table class="block-form">
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='805'][not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='805']/n1:code/@displayName"/>
                                        </xsl:when>
					                </xsl:choose>
					    		</h2>
								<p class="tableblack">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='805'][not(@nullFlavor)]">
					    		        	<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='805']/n1:value"/>
					    		        </xsl:when>
					                </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='806'][not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='806']/n1:code/@displayName"/>
                                        </xsl:when>
					                </xsl:choose>
					    		</h2>
								<p class="tableblack">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='806'][not(@nullFlavor)]">
					    		        	<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='806']/n1:value"/>
					    		        </xsl:when>
					                </xsl:choose>
                                </p>
                            </td>
                        </tr>
						<xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='808'][not(@nullFlavor)]">
					            <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Выявленные патологии</xsl:text>
                                        </h2>
										<xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='808']">
										    <p class="tableblack">
					            		        <xsl:value-of select="./n1:value/@code"/>&#160;
										    	<xsl:value-of select="./n1:value/@displayName"/>
                                            </p>
										</xsl:for-each>
                                    </td>
                                </tr>
					        </xsl:when>
					    </xsl:choose>
						<xsl:choose>
				            <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='810'][not(@nullFlavor)]">
						        <tr>
                                    <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Результат консультации</xsl:text>
						        		</h2>
										<p class="tableblack">
					            		    <xsl:text>Направление: </xsl:text>
											<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='RESCONS']/n1:entry/n1:observation[n1:code/@code='810']/n1:value/@displayName"/>
                                        </p>
                                    </td>
                                </tr>
						    </xsl:when>
					    </xsl:choose>
					</table>
					<table class="block-form">
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Диагнозы</xsl:text>
                                </h2>
								<p class="tableblack">
							        <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section/n1:component/n1:section[n1:code/@code='DGN']/n1:entry/n1:observation[n1:code/@code='806'][not(@nullFlavor)]">
							            	<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section/n1:component/n1:section[n1:code/@code='DGN']/n1:entry/n1:observation[n1:code/@code='806']/n1:value[1]"/>
							            </xsl:when>
										<xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
					                </xsl:choose>
                                </p>
                            </td>
                        </tr>
					    <tr>
                            <td colspan="8">
                                <h2 class="tablegray">
                                    <xsl:text>Режим и рекомендации</xsl:text>
                                </h2>
								<p class="tableblack">
					    		    <xsl:choose>
				                        <xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section/n1:component/n1:section[n1:code/@code='REGIME']/n1:entry/n1:observation[n1:code/@code='807'][not(@nullFlavor)]">
					    		        	<xsl:value-of select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section/n1:component/n1:section[n1:code/@code='REGIME']/n1:entry/n1:observation[n1:code/@code='807']/n1:value"/>
					    		        </xsl:when>
					    		        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
					                </xsl:choose>
                                </p>
                            </td>
                        </tr>
					</table>
					<xsl:choose>
						<xsl:when test="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LINKDOCS']/n1:entry">
					        <table class="block-form">
						        <tr>
					                <td colspan="8">
                                        <h2 class="tablegray">
                                            <xsl:text>Связанные документы</xsl:text>
                                        </h2>
						        		<xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:code/@code='LINKDOCS']/n1:entry/n1:act">
                                            <p class="tableblack">
                                                <xsl:value-of select="./n1:text"/><br/>
												<xsl:choose>
						                            <xsl:when test="./n1:entryRelationship/n1:observation[n1:code/@code='11002']/n1:value">
												        <xsl:text>Серия: </xsl:text>
														<xsl:value-of select="./n1:entryRelationship/n1:observation[n1:code/@code='11002']/n1:value"/>&#160;
													</xsl:when>
						                        </xsl:choose>
												<xsl:choose>
						                            <xsl:when test="./n1:entryRelationship/n1:observation[n1:code/@code='11003']/n1:value">
												        <xsl:text>Номер: </xsl:text>
														<xsl:value-of select="./n1:entryRelationship/n1:observation[n1:code/@code='11003']/n1:value"/>&#160;
													</xsl:when>
						                        </xsl:choose>
												<xsl:text>Дата: </xsl:text>
												<xsl:call-template name="formatDate">
                                                    <xsl:with-param name="newdate" select="./n1:effectiveTime/@value"/>
                                                </xsl:call-template>
                                            </p>
						        		</xsl:for-each>
                                    </td>
                                </tr>
					        </table>
					    </xsl:when>
					</xsl:choose>
					<table class="end-block-form">
					    <tr>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Консультацию провел</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:documentationOf/n1:serviceEvent/n1:performer/n1:assignedEntity/n1:code[not(@nullFlavor)]">
                                            <xsl:value-of select="/n1:ClinicalDocument/n1:documentationOf/n1:serviceEvent/n1:performer/n1:assignedEntity/n1:assignedPerson/n1:name"/>
								    		<br/>
								    		<xsl:value-of select="/n1:ClinicalDocument/n1:documentationOf/n1:serviceEvent/n1:performer/n1:assignedEntity/n1:code/@displayName"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                            <td colspan="4">
                                <h2 class="tablegray">
                                    <xsl:text>Дата составления</xsl:text>
                                </h2>
								<p class="tableblack">
                                    <xsl:choose>
                                        <xsl:when test="/n1:ClinicalDocument/n1:effectiveTime[not(@nullFlavor)]">
                                            <xsl:call-template name="formatDate">
                                                <xsl:with-param name="newdate" select="/n1:ClinicalDocument/n1:effectiveTime/@value"/>
                                            </xsl:call-template>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text>-</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </p>
                            </td>
                        </tr>
                    </table>
            </body>
		</html>
	</xsl:template>
	<xsl:template name="getR">
        <xsl:param name="templateId"/>
        <xsl:variable name="templateIdLength" select="string-length($templateId)"/>
        <xsl:value-of select="substring ($templateId, $templateIdLength, 1)"/>
    </xsl:template>
    <xsl:template name="getYear">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:choose>
            <xsl:when test="$dateLength=0">
                <xsl:text>----</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring ($date, 1, 4)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getMonth">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:variable name="month" select="substring ($date, 5, 2)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=6">
            <xsl:value-of select="substring ($date, 5, 2)"/>
			</xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
	<xsl:template name="getTextMonth">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:variable name="month" select="substring ($date, 5, 2)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=6">
                <xsl:choose>
                    <xsl:when test="$month='01'">
                        <xsl:text>января</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='02'">
                        <xsl:text>Февраля</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='03'">
                        <xsl:text>марта</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='04'">
                        <xsl:text>апреля</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='05'">
                        <xsl:text>мая</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='06'">
                        <xsl:text>июня</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='07'">
                        <xsl:text>июля</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='08'">
                        <xsl:text>августа</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='09'">
                        <xsl:text>сентября</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='10'">
                        <xsl:text>октября</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='11'">
                        <xsl:text>ноября</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month='12'">
                        <xsl:text>декабря</xsl:text>
                    </xsl:when>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getDay">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=8">
                <xsl:value-of select="substring ($date, 7, 2)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getName">
        <xsl:param name="name"/>
        <xsl:choose>
            <xsl:when test="$name/n1:family">
                <xsl:if test="$name/n1:prefix">
                    <xsl:value-of select="$name/n1:prefix"/>
                    <xsl:text></xsl:text>
                </xsl:if>
                <xsl:value-of select="$name/n1:family"/>
                <xsl:text></xsl:text>
                <xsl:value-of select="$name/n1:given[1]"/>
                <xsl:text></xsl:text>
                <xsl:value-of select="$name/n1:given[2]"/>
                <xsl:text></xsl:text>
                <xsl:if test="$name/n1:suffix">
                    <xsl:value-of select="$name/n1:suffix"/>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$name"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getHour">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=10">
                <xsl:choose>
                    <xsl:when test="substring ($date, 9, 1)='0'">
                        <xsl:value-of select="substring ($date, 10, 1)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="substring ($date, 9, 2)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getMinute">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=12">
                <xsl:choose>
                    <xsl:when test="substring ($date, 9, 1)='0'">
                        <xsl:value-of select="substring ($date, 11, 2)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="substring ($date, 11, 2)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getSecond">
        <xsl:param name="date"/>
        <xsl:variable name="dateLength" select="string-length($date)"/>
        <xsl:choose>
            <xsl:when test="$dateLength>=14">
                <xsl:value-of select="substring ($date, 13, 5)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>--</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getMKB">
        <xsl:param name="code"/>
        <xsl:param name="pos"/>
        <xsl:value-of select="substring ($code, $pos, 1)"/>
    </xsl:template>
	<xsl:template name="getAges">
	    <xsl:param name="age"/>
        <xsl:variable name="agebefore100" select="substring($age, 2, 1)"/>
		<xsl:variable name="ageafter100" select="substring($age, 3, 1)"/>
		<xsl:variable name="age1234" select="substring($age, 1, 1)"/>
            <xsl:if test="string-length($age)=2">
                <xsl:choose>
                    <xsl:when test="($agebefore100='1') and (number($age)&gt;20)">
                        <xsl:text> год</xsl:text>
                    </xsl:when>
					<xsl:when test="(($agebefore100='2') or ($agebefore100='3') or ($agebefore100='4')) and (number($age)&gt;20)">
                        <xsl:text> года</xsl:text>
                    </xsl:when>
			        <xsl:otherwise>
                        <xsl:text> лет</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
			</xsl:if>
			<xsl:if test="string-length($age)=1">
                <xsl:choose>
                    <xsl:when test="$age1234='1'">
                        <xsl:text> год</xsl:text>
                    </xsl:when>
					<xsl:when test="($age1234='2') or ($age1234='3') or ($age1234='4')">
                        <xsl:text> года</xsl:text>
                    </xsl:when>
			        <xsl:otherwise>
                        <xsl:text> лет</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
			</xsl:if>
			<xsl:if test="(number($age)&gt;100) and (number($age)&lt;110)">
                 <xsl:choose>
                     <xsl:when test="($ageafter100='1')">
                         <xsl:text> год</xsl:text>
                     </xsl:when>
					 <xsl:when test="($ageafter100='2') or ($ageafter100='3') or ($ageafter100='4')">
                         <xsl:text> года</xsl:text>
                     </xsl:when>
			        <xsl:otherwise>
                        <xsl:text> лет</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
			<xsl:if test="(number($age)&gt;120)">
                 <xsl:choose>
				 <xsl:when test="($ageafter100='1')">
                         <xsl:text> год</xsl:text>
                     </xsl:when>
					 <xsl:when test="($ageafter100='2') or ($ageafter100='3') or ($ageafter100='4')">
                        <xsl:text> года</xsl:text>
                     </xsl:when>
			        <xsl:otherwise>
                        <xsl:text> лет</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
	</xsl:template>
    <xsl:template name="getAge">
        <xsl:param name="birthdate"/>
        <xsl:param name="deathdate"/>
        <xsl:param name="withtext"/>
        <xsl:variable name="bDay" select="substring ($birthdate, 7, 2)"/>
        <xsl:variable name="dDay" select="substring ($deathdate, 7, 2)"/>
        <xsl:variable name="bMonth" select="substring ($birthdate, 5, 2)"/>
        <xsl:variable name="dMonth" select="substring ($deathdate, 5, 2)"/>
        <xsl:variable name="bYear" select="substring ($birthdate, 1, 4)"/>
        <xsl:variable name="dYear" select="substring ($deathdate, 1, 4)"/>
		<xsl:variable name="age1" select="number($dYear)-number($bYear)"/>
		<xsl:variable name="age2" select="number($dYear)-number($bYear)-1"/>
        <xsl:choose>
            <xsl:when test="number($dDay)-number($bDay)&gt;=0">
                <xsl:choose>
                    <xsl:when test="number($dMonth)-number($bMonth)&gt;=0">
                        <xsl:value-of select="$age1"/>
						<xsl:if test="$withtext=1">
						    <xsl:call-template name="getAges">
                                <xsl:with-param name="age" select="$age1"/>
                            </xsl:call-template>
						</xsl:if>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$age2"/>
						<xsl:if test="$withtext=1">
							<xsl:call-template name="getAges">
                                <xsl:with-param name="age" select="$age2"/>
                            </xsl:call-template>
						</xsl:if>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="number($dMonth)-number($bMonth)-1&gt;=0">
                        <xsl:value-of select="$age1"/>
						<xsl:if test="$withtext=1">
							<xsl:call-template name="getAges">
                                <xsl:with-param name="age" select="$age1"/>
                            </xsl:call-template>
						</xsl:if>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$age2"/>
						<xsl:if test="$withtext=1">
							<xsl:call-template name="getAges">
                                <xsl:with-param name="age" select="$age2"/>
                            </xsl:call-template>
						</xsl:if>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getMonths">
        <xsl:param name="birthdate"/>
        <xsl:param name="deathdate"/>
        <xsl:variable name="bDay" select="substring ($birthdate, 7, 2)"/>
        <xsl:variable name="dDay" select="substring ($deathdate, 7, 2)"/>
        <xsl:variable name="bMonth" select="substring ($birthdate, 5, 2)"/>
        <xsl:variable name="dMonth" select="substring ($deathdate, 5, 2)"/>
        <xsl:variable name="bYear" select="substring ($birthdate, 1, 4)"/>
        <xsl:variable name="dYear" select="substring ($deathdate, 1, 4)"/>
        <xsl:choose>
            <xsl:when test="number($dYear)=number($bYear)">
                <xsl:choose>
                    <xsl:when test="(string-length($birthdate)>=8) and (string-length($deathdate)>=8)">
                        <xsl:choose>
                            <xsl:when test="number($dDay)&gt;=number($bDay)">
                                <xsl:value-of select="number($dMonth)-number($bMonth)"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="number($dMonth)-number($bMonth)-1"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="(string-length($birthdate)&lt;6) or (string-length($deathdate)&lt;6)">
                                <xsl:value-of select="0"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="number($dMonth)-number($bMonth)"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="(string-length($birthdate)>=8) and (string-length($deathdate)>=8)">
                        <xsl:choose>
                            <xsl:when test="number($dDay)&gt;=number($bDay)">
                                <xsl:value-of select="number($dMonth)+12-number($bMonth)"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="number($dMonth)+12-number($bMonth)-1"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="(string-length($birthdate)&lt;6) or (string-length($deathdate)&lt;6)">
                                <xsl:value-of select="12"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="number($dMonth)+12-number($bMonth)"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getDays">
        <xsl:param name="birthdate"/>
        <xsl:param name="deathdate"/>
        <xsl:variable name="bDay" select="substring ($birthdate, 7, 2)"/>
        <xsl:variable name="dDay" select="substring ($deathdate, 7, 2)"/>
        <xsl:variable name="bMonth" select="substring ($birthdate, 5, 2)"/>
        <xsl:variable name="dMonth" select="substring ($deathdate, 5, 2)"/>
        <xsl:variable name="dYear" select="substring ($deathdate, 1, 4)"/>
        <xsl:choose>
            <xsl:when test="(string-length($birthdate)>=8) and (string-length($deathdate)>=8)">
                <xsl:choose>
                    <xsl:when test="number($dDay)&gt;=number($bDay)">
                        <xsl:value-of select="number($dDay)-number($bDay)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="number($dMonth)=1">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=2">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=3">
                                <xsl:choose>
                                    <xsl:when test="number($dYear) mod 4=0">
                                        <xsl:choose>
                                            <xsl:when test="(number($dYear) mod 100)=0">
                                                <xsl:choose>
                                                    <xsl:when test="(number($dYear) mod 400)=0">
                                                        <xsl:value-of select="number($dDay)+28-number($bDay)"/>
                                                    </xsl:when>
                                                    <xsl:otherwise>
                                                        <xsl:value-of select="number($dDay)+29-number($bDay)"/>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of select="number($dDay)+29-number($bDay)"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="number($dDay)+28-number($bDay)"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=4">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=5">
                                <xsl:value-of select="number($dDay)+30-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=6">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=7">
                                <xsl:value-of select="number($dDay)+30-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=8">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=9">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=10">
                                <xsl:value-of select="number($dDay)+30-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=11">
                                <xsl:value-of select="number($dDay)+31-number($bDay)"/>
                            </xsl:when>
                            <xsl:when test="number($dMonth)=12">
                                <xsl:value-of select="number($dDay)+30-number($bDay)"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="0"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="formatDate">
        <xsl:param name="newdate"/>
			    <xsl:call-template name="getDay">
                    <xsl:with-param name="date" select="$newdate"/>
                </xsl:call-template>
				<xsl:text>&#46;</xsl:text>
				<xsl:call-template name="getMonth">
                    <xsl:with-param name="date" select="$newdate"/>
                </xsl:call-template>
				<xsl:text>&#46;</xsl:text>
				<xsl:call-template name="getYear">
                    <xsl:with-param name="date" select="$newdate"/>
                </xsl:call-template>
	</xsl:template>
	<xsl:template name="formatContact">
        <xsl:param name="contact"/>
                    <xsl:value-of select="substring-after($contact, ':')"/>
	</xsl:template>
</xsl:transform>
