ibm-integration-busextended-sql

Fetch value from XML using dynamic tag in ESQL


I have an xml

<family>
   <child_one>ROY</child_one>
   <child_two>VIC</child_two>
</family>

I want to fetch the value from the XML based on the dynamic tag in ESQL. I have tried like this

SET dynamicTag = 'child_'||num;
SET value = InputRoot.XMLNSC.parent.(XML.Element)dynamicTag;

Here num is the value received from the input it can be one or two. The result should be value = ROY if num is one and value is VIC if num is two.


Solution

  • The chapter ESQL field reference overview describes this use case:

    Because the names of the fields appear in the ESQL program, they must be known when the program is written. This limitation can be avoided by using the alternative syntax that uses braces ( { ... } ).

    So can change your code like this:

    SET value = InputRoot.XMLNSC.parent.(XMLNSC.Element){dynamicTag};
    

    Notice the change of the element type as well, see comment of @kimbert.