Do you think you know XML?

Vibhor Mahajan
Trantor
Published in
2 min readNov 27, 2021

--

Photo by Pankaj Patel on Unsplash

One of the products which we develop and maintain has several third-party API integrations, many of which use SOAP for communication. We decided to create simulators for these in order to mock different kinds of edge-cases which we encounter during our testing. There’s an excellent tool available for the purpose called Hoverfly and we have been using it with great success to create such mock APIs.

We wanted to make the responses change dynamically based on the input provided. Hoverfly supports a templating feature using which you can parse the input and use the values in a templated output.

The following XML code is a representation of how our input looks like:

Sample Input

I wanted to parse the XML and get the value of the element called “FirstName” and naively believed that the Xpath to get the value should be as simple as the following:

/soapenv:Envelope/soapenv:Body/api:Command/api:Body/api:Item/IDScoreRequest/Identity/FirstName

When I tried using it in the output template it did not work. Notice the xmlns attributes on the elements. With namespaces, it isn’t as simple as tracing the path of the element from the root in the tree. You have to give the parser information about the namespaces before it can understand where to look for your element.

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms754539(v=vs.85)

You can have both named namespaces and default namespaces. In our example, we have both of these.

So, the issue with Hoverfly was that the Go parser used by the library did not support namespaces. In case you are interested in going into details, the following is the link to the issue — https://github.com/SpectoLabs/hoverfly/issues/682

I was able to parse the XML after the hoverfly started using a Go lib called Xsel as the XML parser. Following is the XPath query which I ended up using to get the value of FirstName:

//*[local-name()="FirstName" and namespace-uri()="http://someservice.com/request"]'

The following query should also be able to get the value:

/soapenv:Envelope/soapenv:Body/api:Command/api:Body/api:Item/IDScoreRequest:IDScoreRequest/IDScoreRequest:Identity/IDScoreRequest:FirstName

Read more about writing XPath queries having namespaces here — https://docs.microsoft.com/en-us/dotnet/standard/data/xml/xpath-queries-and-namespaces

So, the next time you are looking for XML parsers for your next project, look out for namespace support and be aware of such scenarios.

Vibhor Mahajan is the VP — Software Product Development and Innovation at Trantor. His job at Trantor is to establish trust with new challenging accounts, develop novel practices, and scale software engineering teams. He provides consulting and thought leadership to help establish consistent and sustainable software engineering delivery.

https://www.linkedin.com/in/vibhormahajan

--

--

Vibhor Mahajan
Trantor
Editor for

A Software Craftsman, he loves building Software Products, and high performance Software Engineering Teams http://bit.ly/2YaU6zY