PK.PATKEY,
"HDR",@1,
"ACCT",@2,
"PATS",@3,
"OPT.PATS",@4,
"TRLR",@10,
END;

[1]::HDR
"<?xml version="_D(34)_1.0_D(34)_" ?>"^!,
"<ReplyMessage>"^!,
"<TransactionId>0</TransactionId>"^!,
"<Status>Success</Status>"^!,
"<StatusDescription>Step returned a Success status.</StatusDescription>"^!,
"<Payload>"^!,
"<PatientManagerList>"^!;

[2]::ACCT
""^Q,
"<HEL>"^!,
DO{>(?(A)AARB[Q],QQ)^Q $(A)AA[QQ]|0^Q1,
$(A)AA[QQ]|3^Q3,
IF{Q1="NEW";Q3_.=.;
$(A)AA[QQ]B|0^Q2,
<$(A)AA[QQ]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
""^ggg,
DO{+$(A)AA[QQ]CDR[ggg]^ggg Q2_ggg^Q2},
"<Encounter>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Encounter>"^!}},
"</HEL>"^!;

[3]::PATS
""^Q,
"<HPL>"^!,
DO{>(?(A)AARB[Q],QQ)^Q $(A)AA[QQ]|0^Q1,
$(A)AA[QQ]|3^Q3,
IF{Q1="NEW";Q3_.=.;
$(A)AA[QQ]B|0^Q2,
<$(A)AA[QQ]EV[""]^/tmp,
Q2_(/tmp#0S)_(/tmp#1S)^Q2,
""^ggg,
DO{+$(A)AA[QQ]CDR[ggg]^ggg Q2_ggg^Q2},
"<Person>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Person>"^!}},
"</HPL>"^!;

[4]::OPT.PATS
1^nr,
DO{nr+1</ARGC 1,
   /ARGV[nr]^TYP,
   /ARGV[nr+1]^BEGIN.END.DT,
   @5,"OPT.BYTYPE",
   nr+2^nr};

[5]::OPT.BYTYPE
BEGIN.END.DT$L(BEG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,
""^ggg,
DO{+$(A)AA[aa]CDR[ggg]^ggg Q2_ggg^Q2},
"<Encounter>"^!,
"<VisitNumber>"_Q1_"</VisitNumber>"^!,
"<PatientNumber>"_Q3_"</PatientNumber>"^!,
"<Key>"_Q2_"</Key>"^!,
"</Encounter>"^!};

[10]::TRLR
"</PatientManagerList>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!;