wso2wso2-esbwso2-enterprise-integratorei

Accessing XML attributes in WSO2 EI


I'm able to access the body of the payload and other Childs of the body but I'm not able to access the attributes defined inside the xml tag i.e.

<data version="2.0.0_461" timestamp="2022-09-02T15:56:37+00:00Z" instance="stg" host="37432d6e1ea8">
    <type id="1019275" name="HP Color LaserJet MFP M477fdw">
        <name firstName="1" lastName="Hewlett Packard"/>
        <capability id="2" name="Yellow"/>
    </type>
    <cons>
        <con name="Black" id="103">
            <dataSource>RM</dataSource>
            <colors>
                <color name="Black" id="3" order="1"/>
            </colors>
        </con>
    </cons>
</data>

In the above xml I can access the capability with $body//data//type//capability it give me <capability id="2" name="Yellow"/> but I want to access the name defined attribute of capability.

How can I do that. I'm using WSO2 EI 6.6.0


Solution

  • You can use @ symbol to access the attributes in Xpaths. So using $body/data/type/capability/@name will return the value of name attribute.

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="test"
           startOnLoad="true"
           statistics="disable"
           trace="disable"
           transports="http,https">
       <target>
          <inSequence>
             <log level="custom">
                <property expression="$body/data/type/capability/@name" name="Value"/>
             </log>
             <respond/>
          </inSequence>
       </target>
       <description/>
    </proxy>
    

    If you send the payload to the above proxy as a request, you will be able to see the log mediator output as below in the logs.

    [2022-09-02 20:53:02,976]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Value = Yellow