Satheesh Kumar
Satheesh Kumar

Reputation: 797

Not able to form xpath using mule expression for given xml

Am trying to read data from xml using an expression in mule but am getting the following exception:

java.lang.RuntimeException: org.mule.api.MuleRuntimeException: Failed to evaluate XPath expression: "//*[xpath:local-name()="oa:ID"]/text()"

The flow is given below

<flow name="testauditFlow1" doc:name="testauditFlow1">
        <http:inbound-endpoint exchange-pattern="one-way"
            host="localhost" port="8086" path="test" doc:name="HTTP"/>
            doc:name="DOM to XML" />
        <expression-component doc:name="Expression"><![CDATA[#[xpath('//*[xpath:local-name()="ID"]/text()').text]]]></expression-component>
        <logger level="INFO" doc:name="Logger" />
    </flow>

The xml i used is given below

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <_ord:ProcessOrder releaseID="9.0" versionID="7.0.0.0" xmlns:_ord="http://www.test.com/xmlns/test" xmlns:_wcf="http://www.test.com/xmlns/test/9" xmlns:oa="http://www.test.com/xmlns/test/foundation">
         <oa:ApplicationArea xsi:type="_wcf:ApplicationAreaType">
            <oa:CreationDateTime>2012-08-07T13:25:01.337Z</oa:CreationDateTime>
            <oa:ID>1234566</oa:ID>
            <_wcf:BusinessContext/>
         </oa:ApplicationArea>
      </_ord:ProcessOrder>
   </soapenv:Body>
</soapenv:Envelope>

Upvotes: 0

Views: 328

Answers (2)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Well I am able to extract the value using XPATH ... You can do it 2 ways :-

1) First way is using local-name and without namespace :-

<logger message="Value of id : #[xpath://.[xpath:local-name()='ID']]" level="INFO" doc:name="Logger"/> 

It works perfect and it is simple, but the XPATH is an old way which may be depreciated in Mule

2) The second way and most effective way is using mulexml:namespace-manager as following :- First you use a mulexml:namespace-manager in the Mule config before the flow:-

<mulexml:namespace-manager includeConfigNamespaces="false">
    <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
    <mulexml:namespace prefix="xsi" uri="http://www.w3.org/2001/XMLSchema-instance"/>
    <mulexml:namespace prefix="soapenc" uri="http://schemas.xmlsoap.org/soap/encoding/"/>
    <mulexml:namespace prefix="xsd" uri="http://www.w3.org/2001/XMLSchema"/>
    <mulexml:namespace prefix="_ord" uri="http://www.test.com/xmlns/test"/>
    <mulexml:namespace prefix="_wcf" uri="http://www.test.com/xmlns/test/9"/>
    <mulexml:namespace prefix="oa" uri="http://www.test.com/xmlns/test/foundation"/>
</mulexml:namespace-manager> 

Then use the following XPATH to retrieve the value as I have done it in a logger:-

<logger message="Value of id : #[xpath('//_ord:ProcessOrder/oa:ApplicationArea/oa:ID/text()').text]" level="INFO" doc:name="Logger"/>  

So, Both the way you can exact the value and both are working perfect

Upvotes: 1

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

Any reason you need xpath: inside this expression?

<expression-component doc:name="Expression">#[xpath('//*[local-name()="ID"]/text()')]</expression-component>

According to this page, there is no need to prefix the local-name() function. Also, I'm not sure why you put the whole into a CDATA section.

Upvotes: 0

Related Questions