I'm trying to virtualize an XML-based API using WireMock and generate a JSON response using wiremock standalone jar. The requirement is to copy a tag value from the XML request payload to the JSON response using XPath. However, I'm facing issues with the XPath expressions I've tried so far, and I'm not able to get it to work correctly.
Here's a dummy example to illustrate my scenario: Sample Request Payload (XML):
`<Envelope
xmlns="http://www.example.com/Envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<SourceID>SOURCE_ID</SourceID>
<Timestamp>2024-07-01T16:27:26Z</Timestamp>
</Header>
<Body>
<Record>
<Metadata>
<Key>
<Name>RequestNumber</Name>
<Value>REQ12345</Value>
</Key>
</Metadata>
</Record>
</Body>
</Envelope>`
Sample Response Payload (JSON):
`{
"event": {
"source": "example",
"type": "example_event",
"timestamp": "2024-07-01T16:27:26Z",
"payload": {
"requestNumber": "REQ12345"
}
}
}`
Problem:
I'm getting an UncheckedExecutionException
in WireMock, indicating an issue with the XPath expression. Despite trying various XPath expressions, the correct value is not being extracted and placed into the JSON response.
Request for Help: Can anyone guide me on the correct way to extract the value from the XML request payload using XPath in WireMock? Any insights or examples would be greatly appreciated.
XPath Expressions I've Tried:
{{{xpath request.body '//Key[Name="RequestNumber"]/Value/text()'}}}
{{{xpath request.body '//*[local-name()="Key"]//*[local-name()="Name" and text()="RequestNumber"]/*[local-name()="Value"]/text()'}}}
{{{xpath request.body '/*[local-name()="Envelope"]/*[local-name()="Body"]/*[local-name()="Record"]/*[local-name()="Metadata"]/*[local-name()="Key"]/*[local-name()="Name"][text()="RequestNumber"]/following-sibling::*[local-name()="Value"]/text()'}}}