XML response with only specific subelements

Very much at the beginning of the learning curve…

I send a succesful XML soap post to an open API. I get a response, but I only need specific subelements of the reponse.

<queryResult total="7015734" pageSize="3" pageNumber="1">
                <namespace:person uuid="72a350f0-eb0b-4607147">
                    <namespace:name id="979928772">
                        <namespace:firstName>First name</namespace:firstName>
                        <namespace:lastName>Last name</namespace:lastName>
                    </namespace:name>
                </namespace:person>
               <namespace:hidden>false</namespace:hidden>
               <namespace:dataProvider>provider 1</namespace:dataProvider>
               <namespace:dataProviderId>611e17002a88b2b17778</namespace:dataProviderId>
               <namespace:sources/>
</queryResult>

I only need the namespace:name and the namespace:dataProviderId to be returned. I tried to add in the headers a XPath with value “//namespace:person/namespace:name”, but I still get the full XML.

I’m very much scratching my head on how to ‘filter’ for the specific subelements I’m interested in.

Postman doesn’t have great support for XPath, so as far as I’m aware you are stuck to parsing to a JavaScript object and working with that object.

Here is some code to consider.

var xmlString = "<queryResult total='7015734' pageSize='3' pageNumber='1'><namespace:person uuid='72a350f0-eb0b-4607147'><namespace:name id='979928772'><namespace:firstName>First name</namespace:firstName><namespace:lastName>Last name</namespace:lastName></namespace:name></namespace:person><namespace:hidden>false</namespace:hidden><namespace:dataProvider>provider 1</namespace:dataProvider><namespace:dataProviderId>611e17002a88b2b17778</namespace:dataProviderId><namespace:sources/></queryResult>";

// parse the XML to a Javascript object.
var javascriptObject = xml2Json(xmlString); 

// just a pretty view of the XML (by converting the javaScript object back to an XML string)
var xml2js = require("xml2js");
var builder = new xml2js.Builder();
console.log(builder.buildObject(javascriptObject));

// accessing the elements directly.
console.log(javascriptObject.queryResult['namespace:person']['namespace:name']['namespace:firstName']);
console.log(javascriptObject.queryResult['namespace:person']['namespace:name']['namespace:lastName']);
console.log(javascriptObject.queryResult['namespace:dataProviderId']);

// create a new JavaScript object with just the info you want
let newObj = {
    firstName: `${javascriptObject.queryResult['namespace:person']['namespace:name']['namespace:firstName']}`,
    lastName: `${javascriptObject.queryResult['namespace:person']['namespace:name']['namespace:lastName']}`,
    dataProviderId: `${javascriptObject.queryResult['namespace:dataProviderId']}`    
    }
console.log(newObj);

With the associated console logs.

image