Override OnResponse method in bps . You can handle reponses in OnResponse() method.
First of all your source message is missing one segment , i corrected it. below code should work for you .
set target = ##class(EnsLib.HL7.Message).%New()
set target.DocType = source.DocType
write $System.Status.GetErrorText(source.BuildMap(1))
set tSC = $$$OK
try {
set segCount = source.SegCount
for segNo=1:1:segCount {
set oSegment = source.GetSegmentAt(segNo)
set segPath = source.GetSegmentPath(segNo)
set fieldCount = oSegment.GetValueAt("*")
for fieldNo=1:1:fieldCount {
set fieldValue = oSegment.GetValueAt(fieldNo)
set compCount = oSegment.GetValueAt(fieldNo_".*")
for compNo=1:1:compCount { // considering there is no repeat - if repeat then one more loop is required
set subCompCount = oSegment.GetValueAt(fieldNo_"."_compNo_".*")
for subCompNo=1:1:subCompCount {
set path = segPath_":"_fieldNo_"."_compNo_"."_subCompNo
set subCompValue = source.GetValueAt(path)
if (subCompValue '= "") && (subCompValue '= ($C(34)_$C(34))) {
//if (oSegment.Name = "PID")&&(fieldNo=2) break
if path="PIDgrp.PID:13.4.1" break
write path_"--->"_subCompValue,!
do target.SetValueAt(subCompValue,path)
}
}
}
//set componentCount = oSegment.GetValueAt(fieldNo
}
}
do target.OutputToFile("C:\Work\Report\target.hl7",1)
}
catch oEx {
}






You can use EnsLib.ITK.HL7.FormatXMLv2 . It was primarily designed for HL7 ITK version. You can extend this class and modify as you need.Then simply convert that into stream that you need and transfer wherever you want.
Alternatively you can download the xsd from HL7 site then map from HL7 to XML schema. It would be pretty lengthy method .