source: ccr/trunk/p/C0CMXML.m@ 604

Last change on this file since 604 was 601, checked in by George Lilly, 15 years ago

support for incoming CCDs

File size: 8.9 KB
Line 
1C0CMXML ; ERX/GPL - MXML based XPath utilities;10/13/09 17:05
2 ;;0.1;C0P;nopatch;noreleasedate
3 ;Copyright 2009 George Lilly. Licensed under the terms of the GNU
4 ;General Public License See attached copy of the License.
5 ;
6 ;This program is free software; you can redistribute it and/or modify
7 ;it under the terms of the GNU General Public License as published by
8 ;the Free Software Foundation; either version 2 of the License, or
9 ;(at your option) any later version.
10 ;
11 ;This program is distributed in the hope that it will be useful,
12 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;GNU General Public License for more details.
15 ;
16 ;You should have received a copy of the GNU General Public License along
17 ;with this program; if not, write to the Free Software Foundation, Inc.,
18 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;
20 Q
21 ;
22TEST ;
23 S C0CXMLIN=$NA(^TMP("C0CMXML",$J))
24 K GARY
25 W $$FTG^%ZISH("/home/vademo2/EHR/p/","mxml-test.xml",$NA(@C0CXMLIN@(1)),3)
26 S C0CDOCID=$$PARSE(C0CXMLIN) W !,"DocID: ",C0CDOCID
27 S REDUX="//ContinuityOfCareRecord/Body"
28 D XPATH(1,"/","GIDX","GARY",,REDUX)
29 Q
30 ;
31TEST2 ;
32 S REDUX="//soap:Envelope/soap:Body/GetPatientFullMedicationHistory5Response/GetPatientFullMedicationHistory5Result/patientDrugDetail"
33 D XPATH(1,"/","GIDX","GARY","",REDUX)
34 Q
35 ;
36TEST3
37 S C0CXMLIN=$NA(^TMP("C0CMXML",$J))
38 K GARY,GTMP,GIDX
39 K @C0CXMLIN
40 W $$FTG^%ZISH("/home/vademo2/CCR/","SampleCCDDocument.xml",$NA(@C0CXMLIN@(1)),3)
41 D CLEANARY("GTMP",C0CXMLIN) ; REMOVE CONTROL CHARACTERS
42 K @C0CXMLIN
43 M @C0CXMLIN=GTMP
44 K GTMP
45 D STRIPTXT("GTMP",C0CXMLIN)
46 K @C0CXMLIN
47 M @C0CXMLIN=GTMP
48 K GTMP
49 S C0CDOCID=$$PARSCCD(C0CXMLIN,"W") W !,"DocID: ",C0CDOCID
50 S REDUX="//ClinicalDocument/component/structuredBody"
51 D XPATH(1,"/","GIDX","GARY",,REDUX)
52 S C0CDOCID=$$PARSE(C0CXMLIN) W !,"DOCID: ",C0CDOCID ;CALL REGULAR PARSER
53 D XPATH(1,"/","GIDX2","GARY2",,REDUX)
54 Q
55 ;
56XPATH(ZOID,ZPATH,ZXIDX,ZXPARY,ZNUM,ZREDUX) ; RECURSIVE ROUTINE TO POPULATE
57 ; THE XPATH INDEX ZXIDX, PASSED BY NAME
58 ; THE XPATH ARRAY XPARY, PASSED BY NAME
59 ; ZOID IS THE STARTING OID
60 ; ZPATH IS THE STARTING XPATH, USUALLY "/"
61 ; ZNUM IS THE MULTIPLE NUMBER [x], USUALLY NULL WHEN ON THE TOP NODE
62 ; ZREDUX IS THE XPATH REDUCTION STRING, TAKEN OUT OF EACH XPATH IF PRESENT
63 I $G(ZREDUX)="" S ZREDUX=""
64 N NEWPATH
65 N NEWNUM S NEWNUM=""
66 I $G(ZNUM)>0 S NEWNUM="["_ZNUM_"]"
67 S NEWPATH=ZPATH_"/"_$$TAG(ZOID)_NEWNUM ; CREATE THE XPATH FOR THIS NODE
68 I $G(ZREDUX)'="" D ; REDUX PROVIDED?
69 . N GT S GT=$P(NEWPATH,ZREDUX,2)
70 . I GT'="" S NEWPATH=GT
71 S @ZXIDX@(NEWPATH)=ZOID ; ADD THE XPATH FOR THIS NODE TO THE XPATH INDEX
72 N GD D DATA("GD",ZOID) ; SEE IF THERE IS DATA FOR THIS NODE
73 I $D(GD(2)) M @ZXPARY@(NEWPATH)=GD ; IF MULITPLE DATA MERGE TO THE ARRAY
74 E I $D(GD(1)) S @ZXPARY@(NEWPATH)=GD(1) ; IF SINGLE VALUE, ADD TO ARRAY
75 N ZFRST S ZFRST=$$FIRST(ZOID) ; SET FIRST CHILD
76 I ZFRST'=0 D ; THERE IS A CHILD
77 . N ZMULT S ZMULT=$$ISMULT(ZFRST) ; IS FIRST CHILD A MULTIPLE
78 . D XPATH(ZFRST,NEWPATH,ZXIDX,ZXPARY,$S(ZMULT:1,1:""),ZREDUX) ; DO THE CHILD
79 N GNXT S GNXT=$$NXTSIB(ZOID)
80 I GNXT'=0 D ;
81 . N ZMULT S ZMULT=$$ISMULT(GNXT) ; IS THE SIBLING A MULTIPLE?
82 . I (ZNUM="")&(ZMULT) D ; SIBLING IS FIRST OF MULTIPLES
83 . . S ZNUM=1 ;
84 . . D XPATH(GNXT,ZPATH,ZXIDX,ZXPARY,ZNUM,ZREDUX) ; DO NEXT SIB
85 . E D XPATH(GNXT,ZPATH,ZXIDX,ZXPARY,$S(ZNUM>0:ZNUM+1,1:""),ZREDUX) ; DO NEXT SIB
86 Q
87 ;
88PARSE(INXML,INDOC) ;CALL THE MXML PARSER ON INXML, PASSED BY NAME
89 ; INDOC IS PASSED AS THE DOCUMENT NAME - DON'T KNOW WHERE TO STORE THIS NOW
90 ; EXTRINSIC WHICH RETURNS THE DOCID ASSIGNED BY MXML
91 ;Q $$EN^MXMLDOM(INXML)
92 Q $$EN^MXMLDOM(INXML,"W")
93 ;
94ISMULT(ZOID) ; RETURN TRUE IF ZOID IS ONE OF A MULTIPLE
95 N ZN
96 ;I $$TAG(ZOID)["entry" B
97 S ZN=$$NXTSIB(ZOID)
98 I ZN'="" Q $$TAG(ZOID)=$$TAG(ZN) ; IF TAG IS THE SAME AS NEXT SIB TAG
99 Q 0
100 ;
101FIRST(ZOID) ;RETURNS THE OID OF THE FIRST CHILD OF ZOID
102 Q $$CHILD^MXMLDOM(C0CDOCID,ZOID)
103 ;
104PARENT(ZOID) ;RETURNS THE OID OF THE PARENT OF ZOID
105 Q $$PARENT^MXMLDOM(C0CDOCID,ZOID)
106 ;
107TAG(ZOID) ; RETURNS THE XML TAG FOR THE NODE
108 Q $$NAME^MXMLDOM(C0CDOCID,ZOID)
109 ;
110NXTSIB(ZOID) ; RETURNS THE NEXT SIBLING
111 Q $$SIBLING^MXMLDOM(C0CDOCID,ZOID)
112 ;
113DATA(ZT,ZOID) ; RETURNS DATA FOR THE NODE
114 ;N ZT,ZN S ZT=""
115 ;S C0CDOM=$NA(^TMP("MXMLDOM",$J,C0CDOCID))
116 ;Q $G(@C0CDOM@(ZOID,"T",1))
117 S ZN=$$TEXT^MXMLDOM(C0CDOCID,ZOID,ZT)
118 Q
119 ;
120PARSCCD(DOC,OPTION) ; THIS WAS COPIED FROM EN^MXMLDOM TO CUSTIMIZE FOR
121 ; PROCESSING CCDS
122 N CBK,SUCCESS,LEVEL,NODE,HANDLE
123 K ^TMP("MXMLERR",$J)
124 L +^TMP("MXMLDOM",$J):5
125 E Q 0
126 S HANDLE=$O(^TMP("MXMLDOM",$J,""),-1)+1,^(HANDLE)=""
127 L -^TMP("MXMLDOM",$J)
128 S CBK("STARTELEMENT")="STARTELE^C0CMXML" ; ONLY THIS ONE IS CHANGED ;GPL
129 S CBK("ENDELEMENT")="ENDELE^MXMLDOM"
130 S CBK("COMMENT")="COMMENT^MXMLDOM"
131 S CBK("CHARACTERS")="CHAR^MXMLDOM"
132 S CBK("ENDDOCUMENT")="ENDDOC^MXMLDOM"
133 S CBK("ERROR")="ERROR^MXMLDOM"
134 S (SUCCESS,LEVEL,LEVEL(0),NODE)=0,OPTION=$G(OPTION,"V1")
135 D EN^MXMLPRSE(DOC,.CBK,OPTION)
136 D:'SUCCESS DELETE^MXMLDOM(HANDLE)
137 Q $S(SUCCESS:HANDLE,1:0)
138 ; Start element
139 ; Create new child node and push info on stack
140STARTELE(ELE,ATTR) ; COPIED FROM STARTELE^MXMLDOM AND MODIFIED TO TREAT
141 ; ATTRIBUTES AS SUBELEMENTS TO MAKE CCD XPATH PROCESSING EASIER
142 N PARENT
143 S PARENT=LEVEL(LEVEL),NODE=NODE+1
144 S:PARENT ^TMP("MXMLDOM",$J,HANDLE,PARENT,"C",NODE)=ELE
145 S LEVEL=LEVEL+1,LEVEL(LEVEL)=NODE,LEVEL(LEVEL,0)=ELE
146 S ^TMP("MXMLDOM",$J,HANDLE,NODE)=ELE,^(NODE,"P")=PARENT
147 ;M ^("A")=ATTR
148 N ZI S ZI="" ; INDEX FOR ATTR
149 F S ZI=$O(ATTR(ZI)) Q:ZI="" D ; FOR EACH ATTRIBUTE
150 . N ELE,TXT ; ABOUT TO RECURSE
151 . S ELE=ZI ; TAG
152 . S TXT=ATTR(ZI) ; DATA
153 . D STARTELE(ELE,"") ; CREATE A NEW SUBNODE
154 . D TXT^MXMLDOM("T") ; INSERT DATA TO TAG
155 . D ENDELE^MXMLDOM(ELE) ; POP BACK UP A LEVEL
156 Q
157 ;
158CLEANARY(OUTARY,INARY) ; GOES THROUGH AN ARRAY AND CALLS CLEAN ON EACH NODE
159 ; INARY AND OUTARY PASSED BY NAME
160 N ZI S ZI=""
161 F S ZI=$O(@INARY@(ZI)) Q:ZI="" D ; FOR EACH NODE
162 . S @OUTARY@(ZI)=$$CLEAN(@INARY@(ZI)) ; CLEAN THE NODE
163 Q
164 ;
165CLEAN(STR) ; extrinsic function; returns string
166 ;; Removes all non printable characters from a string.
167 ;; STR by Value
168 N TR,I
169 F I=0:1:31 S TR=$G(TR)_$C(I)
170 S TR=TR_$C(127)
171 QUIT $TR(STR,TR)
172 ;
173STRIPTXT(OUTARY,ZARY) ; STRIPS THE "TEXT" PORTION OUT OF AN XML FILE
174 ; THIS IS USED TO DELETE THE NARATIVE HTML OUT OF THE CCD XML FILES BECAUSE
175 ; THEY DO NOT WORK RIGHT WITH THE PARSER
176 ;N ZWRK,ZBLD,ZI ; WORK ARRAY,BUILD ARRAY, AND COUNTER
177 S ZI=$O(@ZARY@("")) ; GET FIRST LINE NUMBER
178 D C0CBEGIN("ZWRK",ZI) ; INSERT FIRST LINE IN WORK ARRAY
179 F S ZI=$O(@ZARY@(ZI)) Q:ZI="" D ; FOR EACH LINE OF THE ARRAY
180 . I $O(@ZARY@(ZI))="" D Q ; AT THE END
181 . . D C0CEND("ZWRK",ZI) ; INCLUDE LAST LINE IN WORK ARRAY
182 . I ZI=1 D C0CBEGIN("ZWRK",ZI) ; START WITH FIRST LINE
183 . I @ZARY@(ZI)["<text" D C0CEND("ZWRK",ZI-1) ;PREV LINE IS AN END
184 . I @ZARY@(ZI)["</text>" D C0CBEGIN("ZWRK",ZI+1) ;NEXT LINE IS A BEGIN
185 S ZI=""
186 F S ZI=$O(ZWRK(ZI)) Q:ZI="" D ; MAKE A BUILD LIST FROM THE WORK ARRAY
187 . D QUEUE^C0CXPATH("ZBLD",ZARY,$P(ZWRK(ZI),"^",1),$P(ZWRK(ZI),"^",2))
188 D BUILD^C0CXPATH("ZBLD",OUTARY) ; BUILD NEW ARRAY WITHOUT TEXT SECTIONS
189 K @OUTARY@(0) ; GET RID OF THE LINE COUNT
190 Q
191 ;
192C0CBEGIN(ZA,LN) ; INSERTS A BEGIN LINE LN INTO ARRAY ZWRK, PASSED BY NAME
193 N ZI
194 S ZI=$O(@ZA@(""),-1)
195 I ZI="" S ZI=1
196 E S ZI=ZI+1 ; INCREMENT COUNT IN WORK ARRAY
197 S $P(@ZA@(ZI),"^",1)=LN
198 Q
199 ;
200C0CEND(ZB,LN) ; INSERTS AN END LINE LN INTO ARRAY ZWRK, PASSED BY NAME
201 N ZI
202 S ZI=$O(@ZB@(""),-1)
203 I ZI="" S ZI=1
204 S $P(@ZB@(ZI),"^",2)=LN
205 Q
206 ;
207FINDTID ; FIND TEMPLATE IDS IN DOM 1
208 S C0CDOCID=1
209 S ZD=$NA(^TMP("MXMLDOM",$J,C0CDOCID))
210 S ZN=""
211 S CURSEC=""
212 S TID=""
213 F S ZN=$O(@ZD@(ZN)) Q:ZN="" D ;
214 . I $$TAG(ZN)="root" D ;
215 . . I $$TAG($$PARENT(ZN))="templateId" D ; ONLY LOOKING FOR TEMPLATES
216 . . . S ZG=$$PARENT($$PARENT(ZN))
217 . . . S CMT=$G(@ZD@(ZG,"X",1))
218 . . . I $$TAG(ZG)="section" D ;START OF A SECTION
219 . . . . S CURSEC=ZG
220 . . . . S SECCMT=$G(@ZD@(CURSEC,"X",1))
221 . . . S TID=$G(@ZD@(ZN,"T",1)) ;TEMPLATE ID
222 . . . I CURSEC'="" D ; IF WE ARE IN A SECTION
223 . . . . S CCDDIR(CURSEC,SECCMT,ZG,$$TAG(ZG),CMT)=TID
224 . . . W !,$$TAG(ZG)," ",$G(@ZD@(ZG,"X",1))
225 . . . W " root ",ZN," ",@ZD@(ZN,"T",1)
226 Q
227 ;
228GENXDS(ZD) ; GENERATE THE XDS PROTOTYPE RECORDS FROM A CCDDIR ARRAY
229 ; ARRAY ELEMENTS LOOK LIKE:
230 ; CCDDIR(1634," Vital signs section template ",1659,"observation"," Result observaion template ")="2.16.840.1.113883.10.20.1.31"
231 ;or CCDDIR(section node,sec name, cur node,cur tag,cur name)=templateId
232 S ZF=178.101 ; FILE NUMBER FOR THE C0C XDS PROTOTYPE FILE
233 S ZI=$Q(@ZD@("")) ;FIRST ARRAY ELEMENT
234 S DONE=0
235 F Q:DONE D ;
236 . W @ZI,!
237 . S ZJ=$QS(ZI,5)
238 . S ZJ=$E(ZJ,2,$L(ZJ)) ;STRIP THE LEADING SPACE
239 . S C0CFDA(ZF,"?+1,",.01)=ZJ
240 . S C0CFDA(ZF,"?+1,",.02)=$QS(ZI,4) ; TAG FOR THIS TEMPLATE
241 . S C0CFDA(ZF,"?+1,",1)=@ZI
242 . D UPDIE
243 . S ZI=$Q(@ZI)
244 . I ZI="" S DONE=1
245 Q
246 ;
247UPDIE ; INTERNAL ROUTINE TO CALL UPDATE^DIE AND CHECK FOR ERRORS
248 K ZERR
249 D CLEAN^DILF
250 D UPDATE^DIE("","C0CFDA","","ZERR")
251 I $D(ZERR) D ;
252 . W "ERROR",!
253 . ZWR ZERR
254 . B
255 K C0CFDA
256 Q
257 ;
Note: See TracBrowser for help on using the repository browser.