I'm trying to perform a simple XPath expression on a string (for example, FHP2019/0156). My aim is to replace the / character with an _ character.
The XPath expression I've used is: fn:replace($ctx:ApplicationID , '/', '_')
But I'm getting the following error:
Evaluation of the XPath expression fn:replace($ctx:ApplicationID ,
'/', '_') resulted in an error
Since, replace is a XPATH 2.0 function, you have to enable xpath 2.0 in the esb. Uncomment the "synapse.xpath.dom.failover.enabled=true" line inside synapse.properties conf file and restart the server. You may have to change your expression from "fn:replace($ctx:ApplicationID , '/', '')" to "replace($ctx:ApplicationID , '/', '')"
Please note that, enabling xpath 2.0 functions might affect other xpath expressions already defined. In that case you may have to find an alternative for fn:replace.
Related
How to get the value of a data-amount JMeter
I tried to get the value using a xpath extractor. But I always got an empty string. What am I doing wrong?
//*[#class = 'panel-body']//*[#class='fade-in'][1]/#data-amount
There is a couple of problems with your XPath expression, for instance:
//*[#class = 'panel-body']//*[#class='fade-in'][1]/#data-amount
^ - extra slash ^ - not required
I also don't like * wildcard, however it might be a weird form of your test scenario so let it be.
Being converted to "normal" XPath expression it should look something like:
//*[#class='panel-body']/*/#data-amount
Demo:
It may be simplified to something like:
//div[#data-action='bid_online']/#data-amount
References:
XPath Language Specification
Using the XPath Extractor in JMeter
XPath Tutorial
I have a link in my Request -
example.com/people/3176972
and my regular expression extractor is-
Regular expression: example.com/people/(.+?)
Template: $1$
Match no: 1
but it is only extracting only 3. I want to extract 3176972 number.
What am I doing wrong?
If you want to retrieve anything after the last slash, then just remove question mark from your expression:
example.com/people/(.+)
(question mark tells it to be non-greedy, hence it's taking 1 character).
If the last portion is always numeric, use
example.com/people/([0-9]+)
You should try this regular expression:
[0-9]+
Amend your regular expression to look like example.com/people/(.*) or example.com/people/(\d+) as your regex stops after first match.
See Regular Expressions chapter of JMeter's User Manual for more information on JMeter Regular Expressions.
Convenient way of testing regular expressions is using "RegExp Tester" mode of the View Results Tree listener.
Check out How to debug your Apache JMeter script guide for more information on different debugging techniques for JMeter tests.
my web sampler request has a reponse message of
[Message 1]
{"event":"pusher:connection_established","data":"{\"socket_id\":\"177828.486549\",\"activity_timeout\":120}"}
How I can extract 177828.486549?
Many thanks.
Actually regular expression for this is very simple, you only need to remember that backslash - \ is a special "escape" character hence it needs to be escaped with another backslash
Add Regular Expression Extractor as a child of the element which returns above message
Configure it as follows:
Reference Name: anything making sense, i.e. socket_id
Regular Expression: "socket_id\\":\\"(.+?)\\"
Template: $1$
Refer extracted value as ${socket_id} where required
Demo (you can test your regular expressions right in View Results Tree listener:
References:
Regular Expressions
USING REGULAR EXPRESSION EXTRACTOR
If I have two XPath queries where the second one is meant to further drill down the result of the first, can I safely let my script combine them into a single query by...
placing parenthesis around the first query,
prefixing the second query with with a slash, and then
simply concatenating the two strings ?
Context
The concrete usecase that sparked this question involves extracting information from XML/XHTML documents according to externally supplied pairs of "CSS selector + attribute name" using XPath behind the scenes.
For example the script may get the following as input:
selector: a#home, a.chapter
attribute: href
It then compiles the selector to an XPath query using the HTML::Selector::XPath Perl module, and the attribute by simply prefixing a # ... which in this case would yield:
XPath query 1: //a[#id='home'] | //a[contains(concat(' ', #class, ' '), ' chapter ')]
XPath query 2: #href
And then it repeatedly passes those queries to libxml2's XPath engine to extract the requested information (in this example, a list of URLs) from the XML documents in question.
It works, but I would prefer to combine the two queries into a single one, which would simplify the code for invoking them and reduce the performance overhead:
XPath query: (//a[#id='home'] | //a[contains(concat(' ', #class, ' '), ' chapter ')])/#href
(note the added parenthesis and slash)
But is this safe to do programmatically, for arbitrary input queries?
In general, no, you can't concatenate two arbitrary XPath expressions in this way, especially not in XPath 1.0. It's easy to find counter-examples: in XPath 1.0 you can't even have a union expression on the RHS of '/', so concatenating "/a" and "(b|c)" would fail.
In XPath 2.0, the result will always be syntactically valid, but in may contain type errors, e.g. if the expressions are "count(a)" and "b". The LHS operand of "/" must evaluate to a sequence of nodes.
Sure, this should work. However, you will always have to respect the correct context. If the elements in your example in the first query have no href attribute, you will get an empty result set.
Also, you will have to take care of e.g. a leading slash in front of your second query, so that you don't end up with a descendant-or-self axis step, which might not be what you want. Apart from that, this should always work - The worst that can happen that it is not logical correct (i.e. you don't get the expected result), but it should always be valid XPath.
My problem using XPath is whenever i use the "substring" function I get only one match and I want to get them all.
another problem is whenever I use the combination of "substring" and operator | it just won't work (no matches).
For example: http://www.tripadvisor.com/Hotel_Review-g52024-d653910-Reviews-Ace_Hotel_Portland-Portland_Oregon.html
on this webpage I used the query
//SPAN[#class='ratingDate relativeDate']/#title | //*[#class='ratingDate']/text()
I got 10 matches but some of them start with "Reviewed ". so I added "substring-after"
and didn't get any matches
the original syntax:
//SPAN[#class='ratingDate relativeDate']/#title | substring-after(//*[#class='ratingDate']/text(), 'Reviewed ')
With pure XPath 1.0 you can't solve that, if you use XPath 2.0 or XQuery 1.0 you can put the substring-after call into the last step of the path e.g. //*[#class='ratingDate']/substring-after(., 'REVIEWED').
If you only have XPath 1.0 then you first need to select the elements with XPath and then iterate over the result in your host language to extract the substring for each element; how you do that depends on the host language and the XPath API.