Neha Raje
Neha Raje

Reputation: 151

Websphere Message Broker: Accessing XML elements in ESQL

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?

Upvotes: 2

Views: 18081

Answers (3)

Sebastian
Sebastian

Reputation: 1

Try using the graphical debugger when resolving these types of issues, it allows you to step through the ESQL code and watch the tree being built. You'd easily be able to see where the code was not doing what you expected it to.

https://www-01.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ag11050_.htm

Upvotes: 0

Dave
Dave

Reputation: 633

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.

Upvotes: 6

user3714601
user3714601

Reputation: 1271

I think your code should be like this:

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

Useless to make assignment like "OutputRoot = InputRoot" several times, you need it only once.

Upvotes: 0

Related Questions