---- Function: public.spx_orphxmlvalidation()

---- DROP FUNCTION public.spx_orphxmlvalidation();

--CREATE OR REPLACE FUNCTION public.spx_orphxmlvalidation()
--  RETURNS xml AS
--$BODY$
--from lxml import etree
--from test_Validation1 import validate
--from datetime import datetime

--xsd_doc = etree.parse('E:\\SQL_Server_Management_Studio\\ORPHValidation\\OrphSchema.xml')
--root = etree.parse('E:\\SQL_Server_Management_Studio\\ORPHValidation\\children.xml')

--_root = etree.Element("root")

--for child_zap in root.findall('./child'):
--    for card_zap in child_zap.findall('.//card'):	
--        child = child_zap.find('name/last').text + ' ' + child_zap.find('name/first').text + ' ' + child_zap.find('name/middle').text
--        dob = datetime.strptime(child_zap.find("dateOfBirth").text, '%Y-%m-%d').strftime("%d.%m.%Y")
--        eventDate = datetime.strptime(child_zap.find("cards/card/dateOfObsled").text, '%Y-%m-%d').strftime("%d.%m.%Y")
--        doc = child_zap.find('documentSer').text + ' ' + child_zap.find('documentNum').text
--        cards = child_zap.find('cards')
--        if cards is not None:
--            child_zap.remove(cards)
	
--        _children = etree.Element("children")
--        _cards = etree.Element("cards")
--        _cards.append(card_zap)
--        child_zap.append(_cards)
--        _children.append(child_zap)        
--        err = validate(_children, xsd_doc)
--        zap = etree.Element("zap")
--        if err != '':
--            fio = etree.SubElement(zap, "FIO")
--            fio.text = child            
--            _root.append(zap)
--            dr = etree.SubElement(zap, "DR")
--            dr.text = dob
--            document = etree.SubElement(zap, "Document")
--            document.text = doc
--            eDate = etree.SubElement(zap, "EventDate")
--            eDate.text = eventDate
--            _typeerror = etree.SubElement(zap, "TypeError")
--            _typeerror.text = 'Error'
--            _xerror = etree.SubElement(zap, "Xerror")
--            _xerror.text = err
--            _error = etree.SubElement(zap, "error")
--            _error.text = err
--plpy.notice(etree.tostring(_root, encoding='utf8'))
--return etree.tostring(_root, encoding='utf8').decode('utf8')
--$BODY$
--  LANGUAGE plpythonu VOLATILE
--  COST 100;
--ALTER FUNCTION public.spx_orphxmlvalidation()
--  OWNER TO postgres;
