Luigi D'Amico
Luigi D'Amico

Reputation: 701

Using a mule variable value inside an xpath expression to query a repeating XML node

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

Answers (2)

Luigi D&#39;Amico
Luigi D&#39;Amico

Reputation: 701

concatenate it like a string...

#[xpath('//person['+flowVars.forEachCounterVariable+']/name/text()')]

Upvotes: 0

Anirban Sen Chowdhary
Anirban Sen Chowdhary

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

Related Questions