%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tv_to_elem
%
% Transform the TV structure to the Element structure to create a XML file
%                   tv ---> elem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% When the total result is empty
tv_to_elem([], _, []):-!.
tv_to_elem(tv(0.0, []), _,[]):-!.

% While you advances, are propagated the values of Deep & Down
tv_to_elem(tv(TV0,[[], Sons, Siblings]), TV1, Result):-
	TV2 is TV0 * TV1,
	tv_to_elem(Sons,     TV2, ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result), !.

% is not the desired element, continues to advance
tv_to_elem(tv(TV0, [[tv(TV_C,[])], Sons, Siblings]), TV1, Result):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_elem(Sons, TV_C2, ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.

% Is an element but this was not created from a Condition
tv_to_elem(tv(TV0, [element(Tag,Attr,IamSons), Sons, Siblings]), TV1, [element(Tag,[rsv=AttrTV|Attr], IamSons)|Result]):-
	TV2 is TV0 * TV1,
	tv_to_elem(Sons, TV2,	ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	concat('',TV2,AttrTV),
	append(ResSons, ResSiblings, Result),!.

% Is an element and this was created from a Condition
tv_to_elem(tv(TV0, [[tv(TV_C,[]),element(Tag,Attr,IamSons)], Sons, Siblings]), TV1, [element(Tag,[rsv=AttrTV|Attr], IamSons)|Result]):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_elem(Sons, TV_C2, ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	concat('', TV_C2, AttrTV),
	append(ResSons, ResSiblings, Result),!.

%%%%%%%%%%%%%%%%%%%%
% For lists
%%%%%%%%%%%%%%%%%%%%
% Is a List but this was not created from a Condition
tv_to_elem(tv(TV0, [[List], Sons, Siblings]), TV1, [element(result,[rsv=AttrTV], [List])|Result]):-
	TV2 is TV0 * TV1,
	tv_to_elem(Sons, TV2,	ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	concat('',TV2,AttrTV),
%	concat('',List, List2),
	append(ResSons, ResSiblings, Result),!.

% Is a List and this was created from a Condition
tv_to_elem(tv(TV0, [[tv(TV_C,[]),[List]], Sons, Siblings]), TV1, [element(result,[rsv=AttrTV], [List])|Result]):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_elem(Sons, TV_C2, ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	concat('',TV_C2,AttrTV),
%	concat('',List, List2),
	append(ResSons, ResSiblings, Result),!.

% for all case, review
tv_to_elem(tv(TV0, [List, Sons, Siblings]), TV1, [element(result,[rsv=AttrTV], List)|Result]):-
	TV2 is TV0 * TV1,
	tv_to_elem(Sons, TV2,	ResSons),
	tv_to_elem(Siblings, TV2, ResSiblings),
	concat('',TV2,AttrTV),
	append(ResSons, ResSiblings, Result),!.




















%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tv_to_element for Tag's
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
Forma General de tv_to_tag...
     tv_to_tag(TV_I, TV_P, Result):-
	TV_I   --> Tv que se debe analizar
	TV_P   --> Valor que se pasa para la penalizacion.
        Result --> Arreglo con los Tags de salida.

	La forma de TV_I es:
	     tv(TV0, [TAG, Son, Sib])
	donde,
	     TV0 -> valor de penalizacion del elemento
	     TAG -> Tag que se debe tomar en cuenta, o condiciones
	     Son -> Hijos
	     Sib -> Hermanos, siblings
*/

/* Comenzamos la CONDICION*/
tv_to_tag(tv(_, [[condition,[]], Consult, Rest]), Result):- %
	tv_to_tag(Consult, RC),
	addCor(RC, ResC),                      % adicionamos Corchetes
	tv_to_tag(Rest,RR),
	combTag(ResC, RR, Result).

/* Para el Operador Logico*/
tv_to_tag(tv(_, [[op(Op),[]], Son, Sib]), Result):-
	tv_to_tag(Son, LI),
	tv_to_tag(Sib, LD),
	combLogOp(Op, LI, LD, ResultR),
	elimRep(ResultR, Result). %no creo que se necesite,

/* Ahora el operador relacional
Como hijo tiene el lado derecho de la relacion y en el derecho tenemos
un valor que pude ser numerico o cadena, con el que se compara el lado
isquierdo.*/
tv_to_tag(tv(_, [[opRel(Op),[]], Son, tv(_,[Valor])]), Result):- % podriamos cambiar por tag valor
	tv_to_tag(Son, ResSon),
	combRelOp(Op, ResSon, Valor, ResultR),
	elimRep(ResultR, Result), !.

/*
	recResp(List, Cambios, ResSon, ResultSon),
	    List --> es el TAG actual
	    []   --> si lo hubiera, es donde estan los cambios
	    ResSon --> el resultado de los hijos, una matriz con cadenas...
*/

/*-------------------------------------------------------------------------------------------*/

% CUANDO NO SE TIENE QUE EVALUAR
tv_to_tag([],[]):-!. % con  esto soluciono cuando no existe hermanos...

% CUANDO SE TERMINA RECORRIDO, SE EMPIEZA TAG
tv_to_tag(tv(TV0, [[tag(Tag),Change], [], Sibling]), Result):-
	newTag(TV0,Tag, Change, TagIam),
	tv_to_tag(Sibling, ResSib),
	append(TagIam, ResSib, ResultR),
	elimRep(ResultR, Result), !.

tv_to_tag(tv(TV0, [[Other,Change], [], Sibling]), Result):-
	tv_to_tag(Sibling, ResSibling),
	append([res(Other, Change, TV0)], ResSibling, ResultR),
	elimRep(ResultR, Result), !.


%  SE RECORRE LOS TAGs
% Condicion
tv_to_tag(tv(TV0, [[tag(Tag),Change], tv(TV,[[condition,[]],Cond,Son]), Sibling]), Result):-
	tv_to_tag(tv(TV,[[condition,[]],Cond,Son]), ResSon),
	newTag(TV0, other(Tag), Change, Tag0),
%	repTag([res(other(Tag),Change,TV0)], ResSon, TagIam),
	repTag(Tag0, ResSon, TagIam),
	tv_to_tag(Sibling, ResultSibling),
	append(TagIam, ResultSibling, ResultR),
	elimRep(ResultR, Result), !.

% Con hijos y Con hermanos
tv_to_tag(tv(TV0, [[tag(Tag),Change], Son, Sibling]), Result):-
	tv_to_tag(Son, ResSon),
	newTag(TV0, Tag, Change, Tag0),
	repTag(Tag0, ResSon, TagIam),
	tv_to_tag(Sibling, ResultSibling),
	append(TagIam, ResultSibling, ResultR),
	elimRep(ResultR, Result), !.

tv_to_tag(tv(TV0, [[Other,Change], Son, Sibling]), Result):-
	tv_to_tag(Son, ResSon),                  % Revisar para el arrastre de Tag
	repTag([res(other(Other), Change, TV0)], ResSon, TagIam),
	tv_to_tag(Sibling, ResultSibling),
	append(TagIam, ResultSibling, ResultR),
	elimRep(ResultR, Result), !.

% Para cuando los hijos no existen,
tv_to_tag(tv(_, [[], _, Sibling]), Result):-
	tv_to_tag(Sibling, Result),!.









/* ------------------------------------------------
	newTag(Tv, Tag, Change, Res)

   Entrada
        Tv  = Valor de verdad del cambio.
        Tag = final Actual
        Change = cambios que se aplican.
   Salida
        Res = Nuevo Resultado Generado

   ------------------------------------------------ */

newTag(_, noExist, _, []):- !.
newTag(Tv, other(Tag), [], [res(other(TagF), [], Tv)]):- !, concat('',Tag,TagF). % quite /
newTag(Tv, Tag, [], [res(TagF, [], Tv)]):- !, concat('',Tag,TagF). % quite /

newTag(Tv, other(Tag), Change, [res(other(TagF), Change, Tv)]):-!,
	concat('',Tv,TVS), concat('[SWAP=',TVS, A1), concat(A1,']',A2),concat(A2,Tag, TagF).	% quite / antes de [SWAP
newTag(Tv, Tag, Change, [res(TagF, Change, Tv)]):-!,
	concat('',Tv,TVS), concat('[SWAP=',TVS, A1), concat(A1,']',A2),concat(A2,Tag, TagF).	% quite / antes de [SWAP

/* ------------------------------------------------
 Concatena Tag actual a todos sus hijos

       repTag(Tv, Tag, Change, resSon, Res)

   Entrada
        Tv  = Valor de verdad del Tag
        Tag = Tag para adicionar
        Change = cambios que se aplican en Tag actual
	resSon = resultado obtenido en los hijos
   Salida
        Res = Nuevo Resultado Generado
  ------------------------------------------------ */
repTag( _, [], []):- !.
repTag([res(other(Other), Change,TV0)], [res(TagS, ChangeS, TV1)|B], [res(TagR, ChangeF, TV2)|B1]):-
	               TV2 is TV0 * TV1, concat(Other,TagS,TagR),
		       append(Change, ChangeS, ChangeF),
		       repTag([res(other(Other), Change, TV0)], B, B1), !.

repTag([res(Tag, Change,TV0)], [res(TagS, ChangeS, TV1)|B], [res(TagR, ChangeF, TV2)|B1]):-
	               TV2 is TV0 * TV1, concat(Tag,'/',Tag2), concat(Tag2,TagS,TagR), % cuando es un tag, se pone en medio /
		       append(Change, ChangeS, ChangeF),
		       repTag([res(Tag, Change, TV0)], B, B1), !.

/*
        combTag(LI,LD, Result)
    Combina todo lo del Lado izquierdo con el derecho, si el lado
    derecho es vacio, entonces se devuelve el mismo.
*/
%combTag(_,[],[]):-!.   % hacemos esto porque despues de la condicion, no puede haber nada valido
combTag(A,B,C):-combTag2(A,B,C).

combTag2(Result,[],Result):-!.
combTag2([], _, []):-!.
combTag2([Res1|Res2], LD, Result):-!, repTag([Res1],LD, Result1),
	                             combTag2(Res2, LD, Result2),
				     append(Result1, Result2, Result).

/*-------------------------------------------------------
 Combina los operandos N,M, y tenemos N*M resultados
            combLogOp(Operation, Op1, Op2, Result)
	    Op = Operacion
	    LI = Lado izquierdo del operado
	    LD = Lado Derecho del Operador
	    Res = Resultado
----------------------------------------------------------*/
combLogOp(_, [], _, []):- !.
combLogOp(Op, [res(Tag, Changes1, TV1)|B], LD, Res):-
	combLogOpP(Op, Tag, Changes1, TV1, LD, ResP),
	combLogOp(Op, B, LD, ResI),
	append(ResP, ResI, Res), !.

combLogOpP(_, _, _,_, [], []):- !.
combLogOpP(Operation, Tag, Changes1, TV1, [res(TagI,Changes2,TV2)|B], [res(TagP,Changes,TV0)|B1]):-
	concat('(', Tag, C1), concat(C1,') ', Co1),
	concat(Co1, Operation, C2),
	concat(C2, ' (', C3), concat(C3, TagI, C4), concat(C4,')', TagP),
	append(Changes1,Changes2, Changes),
	TV0 is TV1 * TV2,
	combLogOpP(Operation, Tag, Changes1, TV1, B, B1), !.




% Simplemente adiciona corchetes a los Tags
addCor([], []):- !.
addCor([res(A1, ChangeS, TV)|B], [res(A3, ChangeS, TV)|B1]):- concat('[',A1,A2), concat(A2,']',A3), addCor(B, B1), !.


/*-------------------------------------------------------
	Cambios...
----------------------------------------------------------*/
% 	combRelOp(Op, ResSon, Valor, ResultR),
% Operador Relacional.
% recRespOpRel(Operator, [Tag], Valor,[Result])
combRelOp(_, [], _, []):- !.

% Si (changes <> []) entonces anotamos penalizacion y pasamos cambios
combRelOp(Op, [res(Tag, Changes, TV)|B], Valor, [res(TagS,Changes,TV)|B1]):-
	concat(Tag, ' ', C4), concat(C4, Op, C5), concat(C5, ' ', C6), concat(C6, Valor, TagS),
	combRelOp(Op, B, Valor, B1), !.

% Eliminación de elementos repetidos.
elimRep([],[]).
elimRep([H|T],S):-member(H,T),!,elimRep(T,S).
elimRep([H|T],[H|S]):-elimRep(T,S).


































% Ademas de pasar de la estructura tag a la element, borra todo lo
% inservible, es decir, los noExist, y si se encontro el path..
% pues todos los demas resultados
tag_to_element([], [],0):-!.
% tag_to_element([res(Tag,_,1.0)|_], [element(query, [cd='1.0'],[Tag])],1):- !.

% Si rsv='0.0', entonces ignorar y continuar con el siguiente
tag_to_element([res(_,_,0.0)|Rest], RestE, R):- !, tag_to_element(Rest, RestE, R).

% Dando las demas respuestas
tag_to_element([res(Tag, Change, TV)|Rest], Result, S):- tag_to_element(Rest, RestE, R), !,
	                                         (R=1, S=1, Result=RestE ;
						       S=0, concat('',TV,TVF),append([cd=TVF], Change, Att),
						            Result=[element(query, Att, [Tag])|RestE]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TV to list TV
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tv_to_listTv(TV, tv(1.0, Result)):- tv_to_listTv(TV, 1.0, Result),!.

% When the total result is empty
tv_to_listTv([], _, []):-!.
tv_to_listTv(tv(0.0, []), _,[]):-!.

% While you advances, are propagated the values of Deep & Down
tv_to_listTv(tv(TV0,[[], Sons, Siblings]), TV1, Result):-
	TV2 is TV0 * TV1,
	tv_to_listTv(Sons,     TV2, ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result), !.

% is not the desired element, continues to advance
tv_to_listTv(tv(TV0, [[tv(TV_C,[])], Sons, Siblings]), TV1, Result):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_listTv(Sons, TV_C2, ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.

% Is an element but this was not created from a Condition
tv_to_listTv(tv(TV0, [element(Tag,Attr,IamSons), Sons, Siblings]), TV1, [tv(TV2, [element(Tag,Attr,IamSons)])|Result]):-
	TV2 is TV0 * TV1,
	tv_to_listTv(Sons, TV2,	ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.

% Is an element and this was created from a Condition
tv_to_listTv(tv(TV0, [[tv(TV_C,[]),element(Tag,Attr,IamSons)], Sons, Siblings]), TV1, [tv(TV_C2, [element(Tag,Attr,IamSons)])|Result]):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_listTv(Sons, TV_C2, ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.
%%%%%%%%%%%%%%%%%%%%
% For lists
%%%%%%%%%%%%%%%%%%%%
% Is a List but this was not created from a Condition
tv_to_listTv(tv(TV0, [[List], Sons, Siblings]), TV1, [tv(TV2,[List])|Result]):-
	TV2 is TV0 * TV1,
	tv_to_listTv(Sons, TV2,	ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.

% Is a List and this was created from a Condition
tv_to_listTv(tv(TV0, [[tv(TV_C,[]),[List]], Sons, Siblings]), TV1, [tv(TV_C2,[List])|Result]):-
	TV2 is TV0 * TV1,
	TV_C2 is TV2 *TV_C,
	tv_to_listTv(Sons, TV_C2, ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.

% List with several Elements
tv_to_listTv(tv(TV0, [[Element|Rest], Sons, Siblings]), TV1, [tv(TV2,[Element])|Result]):-!,
	TV2 is TV0 * TV1,
	tv_to_listTv(tv(TV0, [Rest, Sons, Siblings]), TV1,	Result).

% for all case, review
tv_to_listTv(tv(TV0, [List, Sons, Siblings]), TV1, [tv(TV2,List)|Result]):-
	TV2 is TV0 * TV1,
	tv_to_listTv(Sons, TV2,	ResSons),
	tv_to_listTv(Siblings, TV2, ResSiblings),
	append(ResSons, ResSiblings, Result),!.








%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PROCEDURE TO PRINT TV WITH FORMAT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tv_to_print(tv(TV0, [Iam, Sons, Siblings]), Tabular):-
	write('\n'), write(Tabular),write('tv('),write(TV0),write(',['), write(Iam), write(', '),
	concat(Tabular, '\t', Tabular2),
	tv_to_print(Sons, Tabular2), write(', '),
	tv_to_print(Siblings, Tabular), write('])'),!.

tv_to_print(tv(TV0, [Iam]), Tabular):-
	write('\n'), write(Tabular),write('tv('),write(TV0),write(',['), write(Iam), write('])'), !.


tv_to_print([], _):- write('[]'), !.
tv_to_print(tv(0.0,[]), _):- write('[]'), !.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ORDER element
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elemen_order([],[]).
elemen_order([element(_,[cd='0.0'|_], _)|T],S):-!,elemen_order(T,S).
elemen_order([H|T],S):- elemen_order(T,R),insertElement(H,R,S).

insertElement(X,[],[X]):-!.
%inserta(X,[H|T],[X,H|T]):-X>=H,!.
insertElement(element(Tag1,[cd=AttrTV1|Attr1], Son1),[element(Tag2,[cd=AttrTV2|Attr2], Son2)|T],
       [element(Tag1,[cd=AttrTV1|Attr1], Son1),element(Tag2,[cd=AttrTV2|Attr2], Son2)|T]):-
	                        atom_codes(AttrTV1,Y), number_codes(Num1,Y),atom_codes(AttrTV2,Z), number_codes(Num2,Z),
				Num1>=Num2,!.

insertElement(X,[H|T],[H|S]):-insertElement(X,T,S),!.


% lo mismo que el anterior pero para RSV
elemen_orderRSV([],[]).
elemen_orderRSV([element(_,[rsv='0.0'|_], _)|T],S):-!,elemen_orderRSV(T,S).
elemen_orderRSV([H|T],S):- elemen_orderRSV(T,R),insertElementRSV(H,R,S).

insertElementRSV(X,[],[X]):-!.
%inserta(X,[H|T],[X,H|T]):-X>=H,!.
insertElementRSV(element(Tag1,[rsv=AttrTV1|Attr1], Son1),[element(Tag2,[rsv=AttrTV2|Attr2], Son2)|T],
       [element(Tag1,[rsv=AttrTV1|Attr1], Son1),element(Tag2,[rsv=AttrTV2|Attr2], Son2)|T]):-
	                        atom_codes(AttrTV1,Y), number_codes(Num1,Y), atom_codes(AttrTV2,Z), number_codes(Num2,Z),
				Num1>=Num2,!.

insertElementRSV(X,[H|T],[H|S]):-insertElementRSV(X,T,S),!.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% antes de buscar la similaridad se ordena para poner la cadena menor
% primero.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
chainOrder(C1, C2, CO1, CO2):- name(C1,LA1), name(C2,LA2),
			       ((chainLess(LA1,LA2), !, CO1=C1, CO2=C2);
			        (CO1=C2, CO2=C1)).
% Te indica si una cadena es mayor que otra
chainLess([],_).
chainLess([H1|_],[H2|_]):- H1<H2,!.
chainLess([H1|T1],[H2|T2]):- H1=H2, chainLess(T1,T2).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Add/Remove list item.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* Adding at the end of a list */
addend(X, [], [X]):-!.
addend(X, [C|R], [C|R1]):- addend(X, R, R1), !.

/* Delete the last item in a list */
deleteend(L,L1):- addend(_,L1,L).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO
% JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS
% CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO
% DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE
% JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS
% CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO
% DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE
% JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS
% CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO
% DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE JESUS CODIGO DE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LOAD-XML
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load_xml(Doc,Clean):-load_structure(Doc,List,[dialect(sgml)]),
	             clean(List,Clean).

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WRITE-XML
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

write_xml(File,Term):-open(File,write,F),xml_write(F,Term,[]),close(F).

%%%%%%%%%%
%  CLEAN
%%%%%%%%%%

clean([],[]):-!.
clean([El|Relements],[AnEl|AnRelements]):-!,clean(El,AnEl),
			clean(Relements,AnRelements).
clean(element(Label,Att,Subelements),element(Label,Att,AnSubelements)):-!,
		clean(Subelements,AnSubelements).
clean(String,AnStringN):-atomic(String),remove_r(String,AnString1),
					remove_t(AnString1, AnString2),
					remove_b(AnString2,AnStringN).

%%%%%%%%%%%%%%%%
% REMOVE_R
%%%%%%%%%%%%%%%%
remove_r(String,String2):-concat_atom(List,'\n',String),
			     concat_atom(List,' ',String2).
remove_t(String,String2):-concat_atom(List,'\t',String),
			     concat_atom(List,' ',String2).
remove_b(String,String2):-concat_atom(List,' ',String),
			remove_l_b(List,List2),
			concat_atom(List2,' ',String2).

%%%%%%%%%%%%%%%%%
% REMOVE_L_B
%%%%%%%%%%%%%%%%%%
remove_l_b([],[]):-!.
remove_l_b([''|R1],R2):-!,remove_l_b(R1,R2).
remove_l_b([' '|R1],R2):-!,remove_l_b(R1,R2).
remove_l_b([X|R1],[X|R2]):-remove_l_b(R1,R2).







































