Daniel Fernánde... · May 25

Replace element value in %XML.XPATH.Document


I have created a method than evaluates an XML expression using the EvaluateExpression method. I need to replace an element value by other string but I couldn't find the way of doing it.

To get that, first I have created the "%XML.XPATH.Document" object using an stream and then I have evaluated the XML expression with the "EvaluateExpression" method. Once I found the element I would like to replace,  I got the value of the element using "do element.Read()" and "set cadena = element.Value" and then I realized some transformations like $ZCVT. Finally, I tried to insert the new string in the element.value but the "set element.Value = nuevaResp" is not working.


This is the XML structure:

            <return xsi:type="ns2:Map">
                    <key xsi:type="xsd:string">datos</key>
                    <value xsi:type="ns2:Map">
                            <key xsi:type="xsd:string">comentario_tramitador</key>
                            <value xsi:type="xsd:string">&lt;br&gt;&lt;p align="justify"&gt;some text:&lt;/p&gt;</value>

This is part of my code:

Set tSC=##class(%XML.XPATH.Document).CreateFromStream(auxStream,.tDoc, , , , , "SOAP-ENV, ns1")
If $$$ISERR(tSC) {Do $System.OBJ.DisplayError(tSC) Quit}

Set tSC=tDoc.EvaluateExpression("/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:get_requerimientoResponse/return","item",.tRes)
If $$$ISERR(tSC) Quit
set contador = tRes.Count()
set element = tRes.GetAt(1)

if (contador>0){
     for index=1:1:tRes.Count() {
     set element = tRes.GetAt(index)
     set aux = element.Read()

     set auxVar = 0


          if ((element.Name = "key") && (element.NodeType = "element")){

               do element.Read()
               set resultado=element.Value

               set auxVar = 1
    if (auxVar = 1){
          if ((element.Name = "value") && (element.NodeType = "element")){
               do element.Read()
               set cabeceraCDATA = "![CDATA["
               set cierreCDATA = "]]"

               set nuevaResp=cabeceraCDATA_element.Value_cierreCDATA
               set nuevaResp = $ZCVT(nuevaResp, "I", "UTF8")

               set element.Value = nuevaResp

               set auxVar = 0


The problem is in the line "set element.Value = nuevaResp" where I can't find the way of replace the old data in the element.Value, with the new "nuevaResp" data.

Thank you so much for your help.

Product version: Ensemble 2017.1
2 0 1 45
Log in or sign up to continue

I have been using %XML.Document methods to manipulate XML. As soon as you find the appropriate %XML.Node, you can use method ReplaceCharacter().