Search code examples
xpathmuledataweavemulesoftmule4

can't extract value from xpath-extract using mule variables


I have a set LocationProperties variable as its Location-465697646-800339871 value. Based on the value of a variable, I want to select a node in the payload.

Mule 3:

xpath3('//DTOLocation[@id=\'' + flowVars.LocationProperties.LocationRef + '\'][1]/DTOLocationDetail[@DetailTypeCd=\'CommercialPropertyLocationDetail\'][1]/@ProtectionClassCd',flowVars.domPayload)
xpath3('//DTOLocation[@id=' + flowVars.LocationProperties.LocationRef + '][1]/@LocationCounterNumber',flowVars.domPayload)

Mule 4 flow:

<flow name="var_xpath_testFlow" doc:id="406270fb-17e7-48e9-a33a-f7a0197f8e05" >
        <http:listener doc:name="Listener" doc:id="662f2277-859f-4516-974b-de7cceeb5b40" config-ref="HTTP_Listener_config" path="/vartest"/>
        <set-variable value="#[payload]" doc:name="Set domPayload" doc:id="2495ac98-b976-41e0-9dcf-398574e54ffa" variableName="domPayload"/>
        <set-variable value="Location-465697646-800339871" doc:name="Set Variable" doc:id="12370458-bb32-4709-b509-acc1e5f50b94" variableName="LocationProperties"/>
        <xml-module:xpath-extract xpath="#[&quot;/DTOApplication/DTOLocation[@id='&quot; ++ vars.LocationProperties ++ &quot;'][1]&quot;/DTOLocationDetail[@DetailTypeCd='CommercialPropertyLocationDetail'][1]/@ProtectionClassCd]" target="xvalue1" config-ref="XML_Config"/>
        <xml-module:xpath-extract xpath="#[&quot;/DTOApplication/DTOLocation[@id='&quot; ++ vars.LocationProperties ++ &quot;'][1]&quot;/@LocationCounterNumber]" config-ref="XML_Config" target="xvalue2"/>
        <logger level="INFO" doc:name="Logger" doc:id="f495b0bf-f8cb-43be-b4d9-d069758e4028" message="#[vars.xvalue]"/>
    </flow>

Input payload: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml

Expected output is the variable :

xvalue1 returns 3,

xvalue2 returns 1.

If I give values directly in xpath, it's work.

When I'm trying to use variables inside an xpath,it throws error like : https://github.com/Manikandan99/rate-dtostep/blob/master/xapth_error.txt

How to extract value using variables in XPath Mule 4?


Solution

  • The error is because you have not added double code correctly in the expression of XPath

    I have done some corrections as below

    
        <flow name="var_xpath_testFlow" doc:id="406270fb-17e7-48e9-a33a-f7a0197f8e05" >
            <http:listener doc:name="Listener" doc:id="662f2277-859f-4516-974b-de7cceeb5b40" config-ref="HTTP_Listener_config" path="/vartest"/>
            <set-variable value="#[payload]" doc:name="Set domPayload" doc:id="2495ac98-b976-41e0-9dcf-398574e54ffa" variableName="domPayload"/>
            <set-variable value="Location-465697646-800339871" doc:name="Set Variable" doc:id="12370458-bb32-4709-b509-acc1e5f50b94" variableName="LocationProperties"/>
            <xml-module:xpath-extract doc:name="Xpath extract" doc:id="4a8ce1ed-3c1c-48f7-95dd-664654c6a66b" xpath="#[&quot;/DTOApplication/DTOLocation[@id='&quot; ++ vars.LocationProperties ++ &quot;'][1]/DTOLocationDetail[@DetailTypeCd='CommercialPropertyLocationDetail'][1]/@ProtectionClassCd&quot;]"/>
            <xml-module:xpath-extract doc:name="Xpath extract" doc:id="6287299e-aae5-411b-b6d5-936e6c6be5fb" xpath="#[&quot;/DTOApplication/DTOLocation[@id='&quot; ++ vars.LocationProperties ++ &quot;'][1]/@LocationCounterNumber&quot;]"/>
            <logger level="INFO" doc:name="Logger" doc:id="f495b0bf-f8cb-43be-b4d9-d069758e4028" message="#[vars.xvalue]"/>
        </flow>
    

    With this, your error will get resolved, which is mentioned in your question. Also, I am not sure why are you setting the payload initially in a variable called domPayload, because you are not using it. Please look into that

    as per our discussion in comments, for your expected output use below dataweave code just after second Xpath extractor

    %dw 2.0 
    output application/json 
    --- 
    (vars.xvalue1 default [] ++ vars.xvalue2 default []) joinBy ","
    

    the o/p will be:- "3,1"