XML responses often carry the identifier, token, or status value that a later JMeter sampler must reuse. XPath2 Extractor reads the structured response after a sampler runs and stores the selected XML value in a named variable for the rest of the thread.
XPath2 Extractor is a post-processor, so its position in the test tree matters. Attach it to the sampler that returns the XML body, bind any XML namespaces the query needs, and choose whether the variable should receive one match, a random match, or every match as numbered variables.
A validation copy can extract a namespaced order id, store it as order_id, and pass it into a later sampler path such as /orders/${order_id}. Use the older XPath Extractor only for legacy plans; current JMeter plans should use XPath2 Extractor for namespace handling and XPath 2.0 support. For ordinary HTML pages, prefer CSS Selector Extractor unless the response is XML or XHTML.
<?xml version="1.0" encoding="UTF-8"?> <ord:orders xmlns:ord="https://schemas.example.net/order"> <ord:order> <ord:id>ORD-2042</ord:id> <ord:total currency="USD">154.30</ord:total> </ord:order> </ord:orders>
Name: Extract order id
Apply to: Main sample only
Use Sub-samples only or Main sample and sub-samples only when the parent sampler creates embedded or transaction child samples that should also be searched.
Name of created variable: order_id Match No.: 1 Default Value: NOT_FOUND
Match No. set to 1 stores the first match in order_id. Use 0 for a random match, or -1 to store all matches as order_id_1, order_id_2, and order_id_matchNr.
XPath Query: //ord:order/ord:id/text()
Namespaces aliases list: ord=https://schemas.example.net/order
XPath queries do not inherit a document's default namespace automatically. Bind a prefix in Namespaces aliases list and use that prefix in the query when the XML uses namespaces.
Tool: XPath Tester
Return entire XPath fragment instead of text content?: cleared
Select this option only when a downstream sampler needs the matched XML fragment rather than the element text.
HTTP Request Path: /orders/${order_id}
def extracted = vars.get('order_id') def smokeOutput = "order_id=${extracted}\nlookup_path=/orders/${extracted}\n" new File('extracted-order.txt').text = smokeOutput SampleResult.setResponseCode(extracted == 'ORD-2042' ? '200' : '500') SampleResult.setResponseMessage(extracted == 'ORD-2042' ? 'OK' : 'XPath value missing') SampleResult.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT) SampleResult.setResponseData(smokeOutput, 'UTF-8') SampleResult.setSuccessful(extracted == 'ORD-2042')
A Debug Sampler can replace this temporary sampler when GUI validation is enough. Keep a real downstream request in the production plan.
Related: How to add a Debug Sampler in JMeter
xpath-extractor-use.jmx
$ jmeter -n -t xpath-extractor-use.jmx -Jsample_variables=order_id -l xpath-extractor-results.jtl -j jmeter.log Creating summariser <summary> Created the tree successfully using xpath-extractor-use.jmx Starting standalone test @ 2026 Jun 30 22:11:08 GMT Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445 summary = 2 in 00:00:01 = 3.3/s Avg: 184 Min: 60 Max: 308 Err: 0 (0.00%) Tidying up ... ... end of run
-n runs JMeter without the GUI, -t selects the saved plan, -l writes sampler results, and -j writes the run log. sample_variables adds order_id as a .jtl column for this validation run.
Related: How to run a JMeter test from the command line
Related: How to configure JMeter JTL result columns
$ echo $? 0
$ cat extracted-order.txt order_id=ORD-2042 lookup_path=/orders/ORD-2042
$ cat xpath-extractor-results.jtl timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect,"order_id" 1782857468755,308,XML order response,200,OK,XPath smoke users 1-1,text,true,,219,0,1,1,null,0,0,0,ORD-2042 1782857469288,60,Use extracted order id,200,OK,XPath smoke users 1-1,text,true,,47,0,1,1,null,0,0,0,ORD-2042
The order_id column confirms that XPath2 Extractor saved ORD-2042 after the XML sampler, and the second row confirms that a later sampler used the same variable.
Keep the XPath2 Extractor and the real sampler reference to ${order_id}. Drop the temporary JSR223 Sampler and omit sample_variables unless the production result format intentionally records extracted variables.