Wiremock: xpath not working if xmlns is present

Viewed 1241

I am creating stub in wiremock. If I have xmlns in xml then it doesn't match however, without that It works.

Request

curl -d '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a xmlns="http://www.example.com/namespaces/ad"><b>1</b><c>2</c><d>9407339517</d></a>' -i -H "Content-Type: text/xml" -X POST "http://localhost:8080/test"

Stub Json

{
  "request": {
    "method": "POST",
    "url": "/test",
    "headers" : {
      "Content-Type" : {
          "equalTo" : "text/xml"
      }
    },
    "bodyPatterns" : [ {
      "matchesXPath" : "/stuff:a[b='1'][c='2']",
      "xPathNamespaces" : {
        "stuff" : "http://www.example.com/namespaces/ad"
      }
    } ]
   },
  "response": {
    "body": "Hello world!",

    "status": 200
  }

}

Along with above mentioned way, I have tried with local-name() too.

1 Answers

When there is a namespace present on a (grand) parent then on the (grand) children inherit the same namespace. So your /b and /c should be prefixed with /stuff:b or /stuff:c

{
  "request": {
    "method": "POST",
    "url": "/test",
    "headers" : {
      "Content-Type" : {
          "equalTo" : "text/xml"
      }
    },
    "bodyPatterns" : [ {
      "matchesXPath" : "/stuff:a[./stuff:b='1'][./stuff:c='2']",
      "xPathNamespaces" : {
        "stuff" : "http://www.example.com/namespaces/ad"
      }
    } ]
   },
  "response": {
    "body": "Hello world!",

    "status": 200
  }

}
Related