0
votes

I have this xml file

<Collections xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
    <Collection value="bob">
        <Environments>
            <Environment>amazon</Environment>
        </Environments>
        <ContentTypes>
            <Type>Standard:ShowVideo</Type>
            <Type>Standard:VideoPlaylist</Type>
            <Type>Blog</Type>
        </ContentTypes>
    </Collection>

    <Collection value="sandy">
    <Environments>
         <Environment>amazon</Environment>
    </Environments>
    <ContentTypes>
         <Type>Blog</Type>
    </ContentTypes>
</Collection>

and am executing this xpath query in my Mule flow. It uses 2 flowVars 'environment' and 'contentType' which come from a Redis message.

<set-variable variableName="collectionQuery" value="/Collections/Collection[Environments/Environment='#[environment]' and ContentTypes/Type='#[contentType]']/@value" />
<logger message="collectionQuery is #[collectionQuery]" level="INFO" /> 
<set-payload value="#[xpath(collectionQuery)]" />
<logger message="payload is #[payload]" level="INFO" />        

Here you can see the fully instantiated query and the result.

INFO] org.mule.api.processor.LoggerMessageProcessor: collectionQuery is /Collections/Collection[Environments/Environment='amazon' and ContentTypes/Type='Blog']/@value
INFO] org.mule.api.processor.LoggerMessageProcessor: payload is [org.dom4j.tree.DefaultAttribute@140207e0 [Attribute: name value value "bob"], org.dom4j.tree.DefaultAttribute@11530d63 [Attribute: name value value "sandy"]]

The result is what looks like an array of 2 org.dom4j.tree objects. I just need an ArrayList of attribute 'value' such as this

[bob,sandy]

I can do this in Groovy but not using Mule's xpath functionality.

2

2 Answers

1
votes

only this worked for me

<set-variable variableName="collectionName" value="#[payload.getText()]" doc:name="collectionName"/>
<logger message="now processing #[collectionName]" level="INFO" doc:name="Logger"/> 
0
votes

My xpath expression returned an array of DefaultAttribute. To get the actual value of the attribute, as a String, I needed to call the getValue() method with my foreach loop.

<foreach doc:name="For Each">
     <set-variable variableName="collectionName" value="#[payload.getValue()]" doc:name="collectionName"/>
     <logger message="now processing #[collectionName]" level="INFO" doc:name="Logger"/> 
</foreach>

output:

INFO] org.mule.api.processor.LoggerMessageProcessor: now processing bob
INFO] org.mule.api.processor.LoggerMessageProcessor: now processing sandy