问题
I have a following SOAP response :-
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<retrieveAllDataResponse xmlns="http://services.test.com/schema/MainData/V1">
<retrieveAllData>
<Response>The Data retrieved from the Database</Response>
<Id>1231</Id>
<Name>test1</Name>
<Age>560</Age>
<Designation>Software Engineer</Designation>
</retrieveAllData>
<retrieveAllData>
<Response>The Data retrieved from the Database</Response>
<Id>165</Id>
<Name>test2</Name>
<Age>561</Age>
<Designation>Senior Software Engineer</Designation>
</retrieveAllData>
<retrieveAllData>
<Response>The Data retrieved from the Database</Response>
<Id>134</Id>
<Name>test3</Name>
<Age>562</Age>
<Designation>HR</Designation>
</retrieveAllData>
</retrieveAllDataResponse>
</soap:Body>
</soap:Envelope>
Now I want to extract the values from the list <retrieveAllData>
and put into flow variables under foreach :-
<foreach collection="#[xpath('//xmlns:retrieveDataResponse/xmlns:retrieveAllData')]" doc:name="For Each">
<set-variable doc:name="Variable" value="#[xpath('//xmlns:Id/text()').text]" variableName="id"/>
<logger level="INFO" message="#[flowVars['id']]" doc:name="Logger"/>
<set-variable doc:name="Variable" value="#[xpath('//xmlns:Name/text()').text]" variableName="name"/>
<logger level="INFO" message="#[flowVars['name']]" doc:name="Logger"/>
<set-variable doc:name="Variable" value="#[xpath('//xmlns:Age/text()').text]" variableName="age"/>
<logger level="INFO" message="#[flowVars['age']]" doc:name="Logger"/>
<set-variable doc:name="Variable" value="#[xpath('//xmlns:Designation/text()').text]" variableName="designation"/>
But I am not getting the values into the variables.. I am getting the following :-
Splitter returned no results. If this is not expected, please check your split expression
Please note that I have already set XML namespace Manager for the namespace ..
Please let me know is there any better way to get all the values from the list and set it into the flow variables ?
回答1:
<flow name="xmlsplitter" doc:name="xmlsplitter">
<file:inbound-endpoint path="C:/var/lib/data" doc:name="File"/>
<file:file-to-string-transformer doc:name="File to String"/>
<logger level="INFO" message="#[payload:]" doc:name="Logger" />
<foreach collection="#[xpath('//out:retrieveAllData')]" doc:name="For Each">
<set-variable doc:name="Variable" value="#[xpath('out:Designation/text()').wholeText]" variableName="id"/>
<logger level="INFO" message="#[flowVars['id']]" doc:name="Logger"/>
</foreach>
</flow>
回答2:
I believe you have a problem with the namespaces, xmlns
is used to indicate the root namespace of an element and its children, however you are using an xpath using it as prefix, i.e: //xmlns:retrieveDataResponse/xmlns:retrieveAllData'
, which is wrong. Try defining namespace myNameSpace the URI http://services.test.com/schema/MainData/V1
and then use it in the xpath fuction: //myNameSpace:retrieveDataResponse/myNameSpace:retrieveAllData'
.
Also if you are using namespaces you will likely have problems with xpath functions as per MULE-6508, try also the wordaround of disabling the inclusions of the mule namespaces.
Finally if this is for an ongoing development, please give it a try to the very-soon-to-be-released Mule 3.6 that includes a much improved support for XML as per wiki and jira.
回答3:
So the final working solution is using the following XPATH and is working for me :- #[xpath('out:Designation/text()').wholeText]
来源:https://stackoverflow.com/questions/27725286/how-to-extract-values-from-an-xml-list-in-mule-foreach