Execution of xpath failed in Mulesoft - xpath

My Mulesoft process is making a call to SuccessFactors API. The /LOGIN call results in a response like this.
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<loginResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">
<result>
<sessionId>9A95*******A2631B8E820894CA.ps8bsfapi52t</sessionId>
<msUntilPwdExpiration>9223372036854775807</msUntilPwdExpiration>
</result>
</loginResponse>
</S:Body>
</S:Envelope>
I've the following name spaces declared in my namespace manager
<mulexml:namespace-manager>
<mulexml:namespace prefix="S" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
<mulexml:namespace prefix="ns2" uri="urn:fault.sfapi.successfactors.com"/>
<mulexml:namespace prefix="" uri="urn:sfobject.sfapi.successfactors.com"/>
</mulexml:namespace-manager>
I want to read the sessionId into a mule session variable.
<set-session-variable variableName="SESSION" value="#[xpath('//S:Envelope/S:Body/loginResponse/result/sessionId').text]" doc:name="Get Session from Login"/>
But, upon execution I end up in this
<faultstring>Execution of the expression "xpath('//S:Envelope/S:Body/loginResponse/result/sessionId').text" failed. (org.mule.api.expression.ExpressionRuntimeException).</faultstring>
The XPath checks out well on any other tool but for Mulesoft.

Use XPATH with * as namespace, so you dont need to bother about namespace.
#[xpath('//*:Envelope/*:Body/*:loginResponse/*:result/*:sessionId').text]
xpath is deprecated new version of mule.
Update:
#[xpath3('//*:Envelope/*:Body/*:loginResponse/*:result/*:sessionId')]
Hope this helps.

Related

How to get a child value with XPath

I have this xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<loginResponse xmlns="http://www.tedial.com/3rdparty/"
xmlns:ns2="http://www.tedial.com/apiextension/">
<session>1C7AE89A-73BF-01E9-9D3F-0010007FFF00</session>
</loginResponse>
</S:Body>
</S:Envelope>
I was trying so many combinations but I am unable to get the session value. Can you help me?
I tried //S:Envelope//S:Body//ns2:loginResponse//ns2:session with no luck
You used a wrong namespace on the session element. The default namespace of loginResponse - xmlns="http://www.tedial.com/3rdparty/" - is inherited to the session element. You have to use the same namespace as
with loginResponse which you - erroneously - assigned the ns2 namespace. So define a third namespace prefix for http://www.tedial.com/3rdparty/ - here I used third - and use that for loginResponse and session:
/S:Envelope/S:Body/third:loginResponse/third:session
Try this:
//S:Envelope//S:Body/loginResponse/session/text()
ns2 prefix not needed.
I got it working now. The problem was happening in SoapUI. For some reason Soap UI automatically uses namespaces for the default namespaces starting with ns1. In my case this Xpath expression worked fine:
//S:Envelope//S:Body//ns1:loginResponse//ns1:session

Xpath not working in camel route

I'm sending soap xml through exchange object.When i try to route the request using xpath in apache camel,i'm not able to execute it properly.Please suggest
My Exchange body xml is
<Envelope><Header>
</Header>
<Body>
<Choice>
<Selector>1</selector>
</Choice>
</Body>
</Envelope>
My Camel Route
from(direct:XX)
.to(when(xpath("body()/Choice/Selector/.",String.class)=='1')
.to("direct:X")
.otherwise()
.to("direct:Y")
your Envelope cannot look like that. it must be something like:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
...
body()/Choice/Selector/. is not an Xpath. there is no such thing like body() in XPath.
Assuming that you have a SOAP Body content in the Exchange.body processed by some JAX-WS endpoint XPath will be
/Choice/Selector or /Choice/Selector/text() if it is an element with mixed content
BTW: if your Envelope is real example you try to test do not forget to fix your XML - you have wrong <Selector>1</selector> Tag names are case sensitive. It must be <Selector>1</Selector>

Trying to parse xml with nokogiri and ruby

I am trying to parse the xml below to get the email address out. I can get the messageid but I think having the a: in front is enabling me to use xpath. Not sure how to pull out the email address. I am trying
xml.xpath("//s:Body/Discover/request/EmailAddress").children.text.to_s
and
xml.xpath("//s:Body/Discover/EmailAddress").children.text.to_s
if i do xml.xpath("//s:Body").children.text.to_s i get the email and the version with all the newlines and tabs but i do not want to parse the email out if i do not have to.
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">test url</a:Action>
<a:MessageID>mid</a:MessageID>
<a:ReplyTo>
<a:Address>test url</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">test url</a:To>
</s:Header>
<s:Body>
<Discover xmlns="test url">
<request xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<EmailAddress>bob#xml.com</EmailAddress>
<RequestVersion>1.0</RequestVersion>
</request>
</Discover>
</s:Body>
</s:Envelope>
The test url is preventing Nokogiri's Xpath from catching on to your namespacing within s:Body. Try simply
email = xml.xpath("//s:Body").first.to_xml.scan(/<EmailAddress>([^<]+)/)[0][0]
The Discover element (and its children) are in a different namespace, and you need to specify this in your query. The second argument to the xpath method is a hash where you can associate prefixes used in the query with namespace urls. Have a look at the section on namespaces in the Nokogiri tutorial.
With Nokogiri, if you don’t specify a namespace hash it will automatically register any namespaces defined on the root node for you. In this case that is the a prefix for http://www.w3.org/2005/08/addressing and the s prefix for http://www.w3.org/2003/05/soap-envelope. This is why your query for //s:Body works. The namespace declaration for Discover isn’t on the root, so you have to register it yourself.
When you provide your own namespace hash Nokogiri doesn’t add those defined on the root, so you will also need to include any of those used in your query.
In your case the following will find the EmailAddress node. The actual prefix you used doesn’t matter (here I’ve chosen t) as long as the URI matches).
xml.xpath('//s:Body/t:Discover/t:request/t:EmailAddress',
's' => "http://www.w3.org/2003/05/soap-envelope",
't' => "test url")

Xpath Post Processor : extract node content from a SOAP response

I have a soap response of this form
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:Responseto xmlns:ns2="http://xyz.company.com/">
<return>
<objectContent xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">/path/to/file.txt</objectContent>
<objectType>FILEPATH</objectType>
<rid>111</rid>
<sid>2</sid>
</return>
</ns2:Responseto>
</S:Body>
</S:Envelope>
I wish to extract the object content in jmeter in order to feed to an xpath assertion.
Can anyone suggest how to do this?
I tried //return/objectType but then the DebugSampler shows me that the value of my variable is blank.
Put you XPath Extractor as a child of the Request that has the response you mention.
Configure the extractor like this:
"Main Sample" Only
"Use Namespaces" checked
"Ignore Whitespaces" checked
"Return entire XPath fragment instead of text content" Unchecked
"XPath query" : //return/objectType
I tested it it works.

Xpath extractor

Can someone help me with this:
I am using xpath extractor of jmeter to retrieve sessionId value from the below response.
I need the value of the sessionId to be stored in some file, so that i can use that value in succeeding calls..My basic response is as below:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:createUserResponse xmlns:ns2="http://www.musicthp.com"
isNewUser="false"
profileId="32109"
sessionId="ryIlb+E5yj7FReA2w96uag=="
success="true">
<duration>316</duration>
</ns2:createUserResponse>`
In order to use the results in a future call, you simply need to use the "Reference Name" as a variable.
If you configure the Xpath Extractor reference name as sessionID, subsequent calls would use ${sessionID}

Resources