Websphere Message Broker: Accessing XML elements in ESQL

Neha Raje picture Neha Raje · Jul 11, 2014 · Viewed 17.3k times · Source

Websphere Message Broker: File in File out example. I have an XML file with repeating element structure. How can I access and modify value of a particular element in ESQL. I worte following code..

CREATE PROCEDURE CopyEntireMessage() BEGIN
     --SET OutputRoot = InputRoot;
      DECLARE I INTEGER 1;
      DECLARE J INTEGER;
      SET J = CARDINALITY(OutputRoot.*[]);
      WHILE I < J DO
         SET OutputRoot = InputRoot;
         SET OutputRoot.XMLNS.person.student[I].name = 'XYZ';
         SET I = I + 1;
      END WHILE;
 END;

But its not working. Picking up the file from input folder but i cannot see anything in Output folder. But if I comment

SET OutputRoot.XMLNS.student[I].name = 'XYZ';

then file is available in output folder as it is without any change.

My XML file is as below

<person>
 <student>
   <name>ABC</name>
   <age>20</age>
   <address>city1</address>
 </student>
 <student>
   <name>PQR</name>
   <age>20</age>
   <address>city2</address>
 </student>
</person>

can anybody help me on this?

Answer

Dave picture Dave · Jul 14, 2014

This compute module should do what you need, tested at 9001 on linux:

CREATE COMPUTE MODULE FileInputOutput_Compute
    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
        -- CALL CopyMessageHeaders();
        CALL CopyEntireMessage();

        FOR source AS OutputRoot.XMLNSC.person.student[] DO
            SET source.name = 'XYZ';
        END FOR;


        RETURN TRUE;
    END;

    CREATE PROCEDURE CopyMessageHeaders() BEGIN
        DECLARE I INTEGER 1;
        DECLARE J INTEGER;
        SET J = CARDINALITY(InputRoot.*[]);
        WHILE I < J DO
            SET OutputRoot.*[I] = InputRoot.*[I];
            SET I = I + 1;
        END WHILE;
    END;

    CREATE PROCEDURE CopyEntireMessage() BEGIN
        SET OutputRoot = InputRoot;
    END;
END MODULE;

A couple of notes, firstly it is not good practice to redefine the auto-generated procedures, if you need to reuse functionality which sets every field in a message then it would be wise to create a new procedure to do this.

XMLNS is also deprecated so use XMLNSC instead, it is higher performance and has all the same capabilities as XMLNS which is retained only to support legacy applications.