Question
· May 25, 2021

Replace element value in %XML.XPATH.Document

Hi,

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:

<SOAP-ENV:Envelope>
    <SOAP-ENV:Body>
        <ns1:get_requerimientoResponse>
            <return xsi:type="ns2:Map">
                <item>
                    <key xsi:type="xsd:string">datos</key>
                    <value xsi:type="ns2:Map">
                        <item>
                            <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>
                        </item>
                    </value>
                </item>
            </return>
        </ns1:get_requerimientoResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This is part of my code:

Set tSC=##class(%XML.XPATH.Document).CreateFromStream(auxStream,.tDoc, , , , , "SOAP-ENV http://schemas.xmlsoap.org/soap/envelope/, ns1 https://pre.es/ws/catalog")
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

     while(element.Read()){

          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
Discussion (1)1
Log in or sign up to continue