Reputation: 701
Lets set the example XML as:
<test>
<randomtag>abc</randomtag>
<person>
<name>john</name>
</person>
<person>
<name>peter</name>
</person>
</test>
Goal:
I want to use the Mule ForEach
component to call a logger to output as follows:
john
peter
The Xpath that is required is as follows:
#[xpath("//person[1]/name/text()")]
for john
#[xpath("//person[2]/name/text()")]
for peter
Question: How do I make the values of the indexes 1
& 2
dynamic?
I was trying something like:
#[xpath("//person[flowVars.forEachCounterVariable]/name/text()")]
but did not work.
PS: Similar but not the same: using mule variable value in xpath expression
Upvotes: 1
Views: 955
Reputation: 701
concatenate it like a string...
#[xpath('//person['+flowVars.forEachCounterVariable+']/name/text()')]
Upvotes: 0
Reputation: 8311
Try the following :-
<splitter evaluator="xpath" expression="/test" doc:name="Splitter_For_MultipleSameNodes"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<foreach collection="#[xpath('//test')]" doc:name="For Each">
<logger message="ID:- #[xpath('person/name/text()')] " level="INFO" doc:name="Logger"/>
</foreach>
Upvotes: 1