How to extract XML values with XPath in JMeter

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.

Steps to extract XML values with JMeter XPath2 Extractor:

  1. Inspect the XML response that contains the value to capture.
    <?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>
  2. Select the sampler that returns the XML response in the JMeter GUI.
  3. Add XPath2 Extractor from AddPost ProcessorsXPath2 Extractor.
  4. Name the extractor for the value it captures.
    Name: Extract order id
  5. Set the extractor scope to the sampler response.
    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.

  6. Set the target variable, match number, and default value.
    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.

  7. Enter the XPath query that returns the text value.
    XPath Query: //ord:order/ord:id/text()
  8. Add the namespace alias used by the XPath query.
    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

  9. Leave fragment return cleared when the variable should contain only text.
    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.

  10. Reference the extracted variable in the later sampler that needs the captured value.
    HTTP Request Path: /orders/${order_id}
  11. Add a temporary JSR223 Sampler after the extractor for the smoke test.
    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

  12. Save a validation copy of the test plan.
    xpath-extractor-use.jmx
  13. Run the saved plan in non-GUI mode and add the extracted variable to the result file.
    $ 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

  14. Check the process exit status.
    $ echo $?
    0
  15. Check the downstream smoke output.
    $ cat extracted-order.txt
    order_id=ORD-2042
    lookup_path=/orders/ORD-2042
  16. Inspect the result file for the extracted variable value.
    $ 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.

  17. Remove the temporary smoke sampler and validation-only result column before the load run.

    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.