Not able to match XML request body using XPathMatcher pattern #161

Closed
opened 2025-12-29 14:23:58 +01:00 by adam · 7 comments
Owner

Originally created by @arpymastro on GitHub (Dec 20, 2018).

I want to create a mock server for SOAP service which accepts XML request body. I'm using XPathMatcher for request matching but it is always showing "No matching found".

My request body -

<s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Header>
    <ns1:RequestHeader
         s:actor="http://schemas.xmlsoap.org/soap/actor/next"
         s:mustUnderstand="0"
         xmlns:ns1="https://www.google.com/apis/ads/publisher/v201811">
      <ns1:networkCode>123456</ns1:networkCode>
      <ns1:applicationName>Api_test</ns1:applicationName>
    </ns1:RequestHeader>
  </s:Header>
  <s:Body>
    <getMyData xmlns="https://www.google.com/apis/ads/publisher/v201811">
      <ReqUID>myID-12345</ReqUID>
      <filterStatement>
        <query>WHERE parentId IS NULL LIMIT 500</query>
      </filterStatement>
    </getMyData>
  </s:Body>
</s:Envelope>

I am using below json for request matching -

{
	"Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/xpath",
                    "IgnoreCase": false
                }
            ]
        },
        "Methods": [
            "post"
        ],
        "Body": {
            "Matcher": {
                "Name": "XPathMatcher",
                "Pattern": "/getMyData"
            }
        }
    },
	"Response": {
        	"StatusCode": 200,
        	"BodyAsFile": "__admin//mappings//getMyDataResponse.xml",
        	"UseTransformer": false
    	}

}
  1. Is my request body matcher pattern incorrect?
  2. Is there a way to read a request body from files? If yes, which matcher can be used?
Originally created by @arpymastro on GitHub (Dec 20, 2018). I want to create a mock server for SOAP service which accepts XML request body. I'm using XPathMatcher for request matching but it is always showing `"No matching found"`. My request body - ``` xml <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s:Header> <ns1:RequestHeader s:actor="http://schemas.xmlsoap.org/soap/actor/next" s:mustUnderstand="0" xmlns:ns1="https://www.google.com/apis/ads/publisher/v201811"> <ns1:networkCode>123456</ns1:networkCode> <ns1:applicationName>Api_test</ns1:applicationName> </ns1:RequestHeader> </s:Header> <s:Body> <getMyData xmlns="https://www.google.com/apis/ads/publisher/v201811"> <ReqUID>myID-12345</ReqUID> <filterStatement> <query>WHERE parentId IS NULL LIMIT 500</query> </filterStatement> </getMyData> </s:Body> </s:Envelope> ``` I am using below json for request matching - ``` js { "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/xpath", "IgnoreCase": false } ] }, "Methods": [ "post" ], "Body": { "Matcher": { "Name": "XPathMatcher", "Pattern": "/getMyData" } } }, "Response": { "StatusCode": 200, "BodyAsFile": "__admin//mappings//getMyDataResponse.xml", "UseTransformer": false } } ``` 1. Is my request body matcher pattern incorrect? 2. Is there a way to read a request body from files? If yes, which matcher can be used?
adam added the question label 2025-12-29 14:23:58 +01:00
adam closed this issue 2025-12-29 14:23:58 +01:00
Author
Owner

@StefH commented on GitHub (Dec 20, 2018):

  1. I think it should be "Pattern": "//getMyData"

  2. Not possible.

@StefH commented on GitHub (Dec 20, 2018): 1. I think it should be `"Pattern": "//getMyData"` 2. Not possible.
Author
Owner

@arpymastro commented on GitHub (Dec 20, 2018):

@StefH
"Pattern": "//getMyData" not working for me :(

@arpymastro commented on GitHub (Dec 20, 2018): @StefH `"Pattern": "//getMyData"` not working for me :(
Author
Owner

@StefH commented on GitHub (Dec 20, 2018):

Try
/s:Envelope/s:Body/*[local-name()='getMyData']

See also https://stackoverflow.com/questions/31167803/what-is-the-xpath-of-an-element-in-a-different-default-namespace

@StefH commented on GitHub (Dec 20, 2018): Try `/s:Envelope/s:Body/*[local-name()='getMyData']` See also https://stackoverflow.com/questions/31167803/what-is-the-xpath-of-an-element-in-a-different-default-namespace
Author
Owner

@arpymastro commented on GitHub (Dec 21, 2018):

@StefH
Tried all below combinations but still no luck -
/s:Envelope/s:Body/*[local-name()='getMyData']

/s:Envelope[@xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"]/s:Body/getMyData[@xmlns=\"https://www.google.com/apis/ads/publisher/v201811\"]

//s:Envelope/s:Body/*[local-name()='getMyData']

//s:Envelope[@xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"]/s:Body/getMyData[@xmlns=\"https://www.google.com/apis/ads/publisher/v201811\"]

@arpymastro commented on GitHub (Dec 21, 2018): @StefH Tried all below combinations but still no luck - `/s:Envelope/s:Body/*[local-name()='getMyData']` `/s:Envelope[@xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"]/s:Body/getMyData[@xmlns=\"https://www.google.com/apis/ads/publisher/v201811\"]` `//s:Envelope/s:Body/*[local-name()='getMyData']` `//s:Envelope[@xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"]/s:Body/getMyData[@xmlns=\"https://www.google.com/apis/ads/publisher/v201811\"]`
Author
Owner

@StefH commented on GitHub (Dec 21, 2018):

I did test this:

{
        "Guid": "5c0b6f3c-a5b7-4d81-90a3-fd18097d023f",
        "Priority": 0,
        "Request": {
            "Path": {
                "Matchers": [
                    {
                        "Name": "WildcardMatcher",
                        "Pattern": "/xpathsoap",
                        "IgnoreCase": false
                    }
                ]
            },
            "Methods": [
                "POST"
            ],
            "Body": {
                "Matcher": {
                    "Name": "XPathMatcher",
                    "Pattern": "//*[local-name() = 'getMyData']"
                }
            }
        },
        "Response": {
            "Body": "<xml>ok</xml>",
            "UseTransformer": false,
            "Headers": {
                "Content-Type": "application/xml"
            }
        }
    }

The console logging displays:

"RequestMatchResult": {
    "TotalScore": 3.0,
    "TotalNumber": 3,
    "IsPerfectMatch": true,
    "AverageTotalScore": 1.0,
    "MatchDetails": [
      {
        "Name": "MethodMatcher",
        "Score": 1.0
      },
      {
        "Name": "PathMatcher",
        "Score": 1.0
      },
      {
        "Name": "BodyMatcher",
        "Score": 1.0
      }
    ]
  }
@StefH commented on GitHub (Dec 21, 2018): I did test this: ``` js { "Guid": "5c0b6f3c-a5b7-4d81-90a3-fd18097d023f", "Priority": 0, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/xpathsoap", "IgnoreCase": false } ] }, "Methods": [ "POST" ], "Body": { "Matcher": { "Name": "XPathMatcher", "Pattern": "//*[local-name() = 'getMyData']" } } }, "Response": { "Body": "<xml>ok</xml>", "UseTransformer": false, "Headers": { "Content-Type": "application/xml" } } } ``` The console logging displays: ``` js "RequestMatchResult": { "TotalScore": 3.0, "TotalNumber": 3, "IsPerfectMatch": true, "AverageTotalScore": 1.0, "MatchDetails": [ { "Name": "MethodMatcher", "Score": 1.0 }, { "Name": "PathMatcher", "Score": 1.0 }, { "Name": "BodyMatcher", "Score": 1.0 } ] } ```
Author
Owner

@arpymastro commented on GitHub (Dec 21, 2018):

@StefH
Thank you!

Can you point me to a article/tutorial/utility where I can learn to extract XPath from XML? I might need to create complex pattern for my requirement

@arpymastro commented on GitHub (Dec 21, 2018): @StefH Thank you! Can you point me to a article/tutorial/utility where I can learn to extract XPath from XML? I might need to create complex pattern for my requirement
Author
Owner

@StefH commented on GitHub (Dec 21, 2018):

Sorry I don't know any tutorials. Mostly I search the internet and stackoverflow.

But there are some online xpath validators which you can use to verify your request xml.

@StefH commented on GitHub (Dec 21, 2018): Sorry I don't know any tutorials. Mostly I search the internet and stackoverflow. But there are some online xpath validators which you can use to verify your request xml.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#161