Reputation: 879
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
Upvotes: 0
Views: 804
Reputation: 14604
Dils' answer is correct to access the attribute. Let me add more details, the syntax used for data extraction from XML payloads is not something invented by WSO2 or not something specific to WSO2. WSO2 EI simply supports Xpath 1.0
and Xpath 2.0
expressions. Simply when you say $body/data/type/capability
WSO2 will run the XPATH expression /data/type/capability
on the value assigned to $body
variable(In your case request payload). So you can use any Xpath expression to extract data from your payload. You can learn more about Xpath expressions from here. You can test your Xpath expressions on an online evaluator like this.
The Xpath 1.0 language specification is here. Xpath 2.0 language specification is here. Here are a few different Xpath expressions to extract the value yellow.
/data/type/capability/@name
//capability/@name
//type/capability/@name
//type[@name = 'HP Color LaserJet MFP M477fdw']/capability/@name
//capability/@*[2]
Upvotes: 2
Reputation: 417
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
Upvotes: 4