[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.

Calling Talend Open Studio Users

The first 100 community members completing the Open Studio survey win a $10 gift voucher.

Start the survey

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read