Assume that we have an XML payload where there are several namespaces defined and we need to retrieve values of some elements from the payload into Properties using XPath. There are several ways to do this. We can either define the namespace in the property mediator and then refer to XML element via XPath with adding the namespace itself into the Xpath operation. let's get started with this.
Below is our payload.
<abc xmlns="http://abc.xyz.com" >
<A>YES</A>
<B>{"abc":"Y","d":"N","e":"N"}</B>
</abc>
For e.g i need to read the value 'A' into a property. Following are the two ways for this However the 2nd option is more cleaner approach since we can get rid of adding namespace references every where. In addition to Property mediator, this can be used with other mediators in WSO2 ESB whether XPath operations are supported.
1. Use name space entries with property mediator
<property xmlns:ns="http://abc.xyz.com" name="aValue" expression="//ns:abc/ns:A" scope="default" type="STRING"/>
2. Use local-name() option in XPath in order to avoid name spaces and get the same element value with property mediator.
<property name="aValueWithoutNS" expression="//*[local-name()='A']" scope="default" type="STRING"/>
Below is our payload.
<abc xmlns="http://abc.xyz.com" >
<A>YES</A>
<B>{"abc":"Y","d":"N","e":"N"}</B>
</abc>
For e.g i need to read the value 'A' into a property. Following are the two ways for this However the 2nd option is more cleaner approach since we can get rid of adding namespace references every where. In addition to Property mediator, this can be used with other mediators in WSO2 ESB whether XPath operations are supported.
1. Use name space entries with property mediator
<property xmlns:ns="http://abc.xyz.com" name="aValue" expression="//ns:abc/ns:A" scope="default" type="STRING"/>
2. Use local-name() option in XPath in order to avoid name spaces and get the same element value with property mediator.
<property name="aValueWithoutNS" expression="//*[local-name()='A']" scope="default" type="STRING"/>
Hi,
ReplyDeleteSuppose there is another child element 'A' for element 'C' as below XML. Then the property mediator 'aValueWithoutNS' has the value as "YESNO". So, the above expression fails in this scenario. Can you please suggest correct expression so that the property mediator 'aValueWithoutNS' has only the "YES". Thanks in advance.
Scenario XML:
---------------------
YES
{"abc":"Y","d":"N","e":"N"}
NO