【问题标题】:Wiremock: xpath not working if xmlns is presentWiremock:如果存在 xmlns,则 xpath 不起作用
【发布时间】:2019-06-26 18:43:29
【问题描述】:

我正在wiremock 中创建存根。如果我在 xml 中有 xmlns,那么它不匹配但是,没有它它可以工作。

请求

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"

存根 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
  }

}

除了上面提到的方法,我也尝试过 local-name()。

【问题讨论】:

    标签: xpath wiremock


    【解决方案1】:

    当(大)父级上存在命名空间时,(大)子级上会继承相同的命名空间。所以你的/b/c 应该以/stuff:b/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
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      相关资源
      最近更新 更多