select 
	/*han_EvnPS_pg.rptdesign*/
	concat(
		'Дата: ',
		coalesce(
			to_char(EvnXml.EvnXml_insDT, 'DD.MM.YYYY'), 
			'не указана') ,
		'   Автор: ',
		coalesce(
			trim(pmUserCache.pmUser_Name), 
			'не указан')
		) as "Xml_DateAuthor",
		
	rpt.han_evnps_survey_parse_pg( --в строку с текстом документа вставляются заголовки
		cardinality(
			regexp_split_to_array(
				cast(xtd.XmlTemplateData_Data as varchar),
				'<fieldLabel>')) - 1, --количество разделов документа
		xtd.XmlTemplateData_Data::text, --строка из которой берутся названия разделов документа
		EvnXml_Data::text --текст документа сохраняя теги
	) as "Xml_Text",

	EvnXml.XmlType_id as "XmlType_id",
	coalesce(EvnSection.EvnSection_IsPriem,1) as "EvnSection_IsPriem"	 
from v_EvnXml as EvnXml
	inner join EvnSection on EvnXml.Evn_id = EvnSection.Evn_id
	left join pmUserCache on pmUserCache.pmUser_id = EvnXml.pmUser_insID
	left join v_xmltemplate as template on template.XmlTemplate_id = evnxml.XmlTemplate_id
	left join XmlTemplateData as xtd on xtd.XmlTemplateData_id = template.XmlTemplateData_id
where EvnSection.Evn_rid = cast($1 as bigint)
	and EvnXml.XmlType_id in (2, 8, 9, 10)
	and EvnSection.Evn_deleted = 1
