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);