C0CMXP	  ; GPL - MXML based XPath utilities;12/04/09  17:05
 ;;0.1;C0C;nopatch;noreleasedate
 ;Copyright 2009 George Lilly.  Licensed under the terms of the GNU
 ;General Public License See attached copy of the License.
 ;
 ;This program is free software; you can redistribute it and/or modify
 ;it under the terms of the GNU General Public License as published by
 ;the Free Software Foundation; either version 2 of the License, or
 ;(at your option) any later version.
 ;
 ;This program is distributed in the hope that it will be useful,
 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;GNU General Public License for more details.
 ;
 ;You should have received a copy of the GNU General Public License along
 ;with this program; if not, write to the Free Software Foundation, Inc.,
 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 ;
 Q
 ;
INITXPF(ARY) ;INITIAL XML/XPATH FILE ARRAY
 ;
 S @ARY@("XML FILE NUMBER")=178.101
 S @ARY@("XML SOURCE FIELD")=2.1
 S @ARY@("XML TEMPLATE FIELD")=3
 S @ARY@("XPATH BINDING SUBFILE")=178.1014
 Q
 ;
SETXPF(ARY) ; SET FILE AND FIELD VARIABLES FROM XPF ARRAY
 ;
 S C0CXPF=@ARY@("XML FILE NUMBER")
 S C0CXFLD=@ARY@("XML SOURCE FIELD")
 S C0CXTFLD=@ARY@("XML TEMPLATE FIELD")
 S C0CXPBF=@ARY@("XPATH BINDING SUBFILE")
 Q
 ;
ADDXP(INARY,TID,FARY) ;ADD XPATH .01 FIELD TO BINDING SUBFILE OF TEMPLATE TID
 I '$D(FARY) D  ;
 . S FARY="FARY" ; FILE ARRAY
 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
 D SETXPF(FARY) ;SET FILE VARIABLES
 N C0CA,C0CB
 S C0CA="" S C0CB=0
 F  S C0CA=$O(@INARY@(C0CA)) Q:C0CA=""  D  ; FOR EACH XPATH
 . S C0CB=C0CB+1 ; COUNT OF XPATHS
 . S C0CFDA(C0CXPBF,"?+"_C0CB_","_TID_",",.01)=C0CA
 . D UPDIE ; CREATE THE BINDING SUBFILE FOR THIS XPATH
 Q
 ;
ADDXML(INXML,TEMPID,FARY) ;ADD XML TO A TEMPLATE ID TEMPID
 ; INXML IS PASSED BY NAME
 I '$D(FARY) D  ;
 . S FARY="FARY" ; FILE ARRAY
 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
 D SETXPF(FARY) ;SET FILE VARIABLES
 D WP^DIE(C0CXPF,TEMPID_",",C0CXFLD,,INXML)
 Q
 ;
ADDTEMP(INXML,TEMPID,FARY) ;ADD XML TEMPLATE TO TEMPLATE RECORD TEMPID FIELD 3
 ;
 I '$D(FARY) D  ;
 . S FARY="FARY" ; FILE ARRAY
 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
 D SETXPF(FARY) ;SET FILE VARIABLES
 D WP^DIE(C0CXPF,TEMPID_",",C0CXTFLD,,INXML)
 Q
 ;
GETXML(OUTXML,TEMPID,FARY) ;GET THE XML FROM TEMPLATE TEMPID
 ;
 I '$D(FARY) D  ;
 . S FARY="FARY" ; FILE ARRAY
 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
 D SETXPF(FARY) ;SET FILE VARIABLES
 I $$GET1^DIQ(C0CXPF,TEMPID_",",C0CXFLD,,OUTXML)'=OUTXML D  Q  ;
 . W "ERROR RETRIEVING TEMPLATE",!
 Q
 ;
MKTPLATE(INXML,OUTT) ;MAKE A TEMPLATE FROM INXML, RETURNED IN OUTT
 ; BOTH PASSED BY NAME
 ;
 S C0CDOCID=$$PARSE^C0CMXML(INXML,"C0CMKT")
 S C0CDOM=$NA(^TMP("MXMLDOM",$J,C0CDOCID)) 
 N ZI S ZI=""
 F  S ZI=$O(@C0CDOM@(ZI)) Q:ZI=""  D  ; FOR EACH NODE IN THE DOM
 . W !,ZI,$$TAG^C0CMXML(ZI)
 Q
 ;
DEMUX(OUTX,INX) ;PARSES XPATH PASSED BY VALUE IN INX TO REMOVE [x] MULTIPLES
 ; RETURNS OUTX: MULTIPLE^SUBMULTIPLE^XPATH 
 N ZX,ZY,ZZ,ZZ1,ZMULT,ZSUB
 S (ZMULT,ZSUB)=""
 S ZX=$P(INX,"[",2)
 I ZX'="" D  ; THERE IS A [x] MULTIPLE
 . S ZY=$P(INX,"[",1) ;FIRST PART OF XPATH
 . S ZMULT=$P(ZX,"]",1) ; NUMBER OF THE MULTIPLE
 . S ZX=ZY_$P(ZX,"]",2) ; REST OF THE XPATH
 . I $P(ZX,"[",2)'="" D  ; A SUB MULTIPLE EXISTS
 . . S ZZ=$P(ZX,"[",1) ; FIRST PART OF XPATH
 . . S ZX=$P(ZX,"[",2) ; DELETE THE [
 . . S ZSUB=$P(ZX,"]",1) ; NUMBER OF THE SUBMULTIPLE
 . . S ZX=ZZ_$P(ZX,"]",2) ; REST OF THE XPATH
 E  S ZX=INX ;NO MULTIPLE HERE
 S @OUTX=ZMULT_"^"_ZSUB_"^"_ZX ;RETURN MULTIPLE^SUBMULTIPLE^XPATH
 Q
 ;
UPDIE	; INTERNAL ROUTINE TO CALL UPDATE^DIE AND CHECK FOR ERRORS
 K ZERR
 D CLEAN^DILF
 D UPDATE^DIE("","C0CFDA","","ZERR")
 I $D(ZERR) D  ;
 . W "ERROR",!
 . ZWR ZERR
 . B
 K C0CFDA
 Q
 ;
