[resolved] XPath with context variables not working

One Star

[resolved] XPath with context variables not working

I have defined a context variable that should be used in my xpath.
I have created a route with the following xpath entry for testing purpose.
"concat('XPath ','is ',)"+context.emotion,String.class
where emotion is a context variable with value AWFUL!
This is the error that I get.
org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: concat('XPath ','is ',)AWFUL!. Reason: javax.xml.xpath.XPathExpressionException
Any idea on how to resolve this?

Accepted Solutions
Employee

Re: [resolved] XPath with context variables not working

Hi sijujohn
you need to construct a valid xpath statement. The xpath statement currently being generated is not valid.
concat('XPath ','is ',)AWFUL!
First, AWFUL! needs to be inside the parentheses and it needs to be inside quotes. Don't confused by the context variable. You are just building a string that happens to be an xpath expression. If you construct a string that is not valid, then you are going to get an error. I believe the xpath expression you want is:
concat('XPath ','is ', 'AWFUL!')
So your expression in Studio should read:
"concat('XPath ','is ', '"+context.emotion,String.class + "')"
Note that I have added an additional set of single quotes around the AWFUL that is being retrieved from the context.

All Replies
Employee

Re: [resolved] XPath with context variables not working

Can you show a little more of your route? It looks like the concat part is used literally in the xpath.
One Star

Re: [resolved] XPath with context variables not working

Attached is the image of the route.
The xpath is being applied at the setBody.
Employee

Re: [resolved] XPath with context variables not working

Hi sijujohn
you need to construct a valid xpath statement. The xpath statement currently being generated is not valid.
concat('XPath ','is ',)AWFUL!
First, AWFUL! needs to be inside the parentheses and it needs to be inside quotes. Don't confused by the context variable. You are just building a string that happens to be an xpath expression. If you construct a string that is not valid, then you are going to get an error. I believe the xpath expression you want is:
concat('XPath ','is ', 'AWFUL!')
So your expression in Studio should read:
"concat('XPath ','is ', '"+context.emotion,String.class + "')"
Note that I have added an additional set of single quotes around the AWFUL that is being retrieved from the context.