Thilina
Thilina

Reputation: 1

WSO2 EI new xml tag using Enrich mediator and when xml tag takes dynamically

Is it possible to add new xml tag to xml payload using enrich mediator.

my sample payload -

<courses>
   <id>181</id>
   <formatted_name>Learning</formatted_name>
   <score>0</score>
   <status>in_progress</status>
   <issued_certificate />
   <from_timestamp>1626083705</from_timestamp>
   <to_timestamp />
</courses>

New tag would be

<link> www.google.com </link>

I cannot use inline as the source since link is taken dynamically. So I'm adding new tag to a payload and then property.

<payloadFactory media-type="xml">
    <format>
        <link xmlns="">$1</link>
    </format>
    <args>
        <arg evaluator="xml" expression="get-property('login_link')"/>
    </args>
</payloadFactory>
<property description="Get login link payload" expression="//root/*" name="login_link_xml" scope="default" type="STRING"/>
// get original payload back
<enrich description="Restore original payload">
    <source clone="false" property="course_payload" type="property"/>
    <target type="body"/>
</enrich>
// assign property as a new node inside the courses
<enrich>
    <source clone="true" property="login_link_xml" type="property"/>
    <target action="child" type ="custom" xpath="//courses"/>
</enrich>

This gives the same payload after enrich

Upvotes: 0

Views: 811

Answers (1)

tmoasz
tmoasz

Reputation: 1331

You can do it in a bit different way. Create your 'tag' as property type OM, using xpath expresion and function: concat, with coded char

<property name="my_link" value="devintegra.com" scope="default" type="STRING"/>
<property name="linkNode" 
          type="OM" 
          expression="concat('&lt;link&gt;',get-property('my_link'),'&lt;/link&gt;')" 
          scope="default" />
        

And with that property you can enrich your body:

<enrich>
  <source type="property" clone="true" property="linkNode"/>
  <target action="child" xpath="//courses"/>
</enrich>

That should work as you expect.

Upvotes: 1

Related Questions