I am trying to add a namespace to my root element only, but doing so seems to add xmlns="" to all direct child elements. This is causing the Google AdWords API to return an error.
Here is my code:
var adwordsNameSpace = XmlService.getNamespace('https://adwords.google.com/api/adwords/cm/v201809');
var root = XmlService.createElement('reportDefinition')
.setNamespace(adwordsNameSpace)
var selector = XmlService.createElement('selector')
var fields = ['Clicks', 'Ctr', 'Cost'];
fields.forEach(function (field) {
var xml = XmlService.createElement('fields').setText(field);
selector.addContent(xml)
})
root.addContent(selector)
var options = [
['reportName', 'Custom Report'],
['reportType', 'ACCOUNT_PERFORMANCE_REPORT'],
['dateRangeType', 'LAST_7_DAYS'],
['downloadFormat', 'XML']
]
options.forEach(function(option) {
root.addContent(XmlService.createElement(option[0]).setText(option[1]))
})
var document = XmlService.createDocument(root);
var xml = XmlService.getPrettyFormat().format(document);
Logger.log(xml);
And this is the final result:
<?xml version="1.0" encoding="UTF-8"?>
<reportDefinition xmlns="https://adwords.google.com/api/adwords/cm/v201809">
<selector xmlns="">
<fields>Clicks</fields>
<fields>Ctr</fields>
<fields>Cost</fields>
</selector>
<reportName xmlns="">Custom Report</reportName>
<reportType xmlns="">ACCOUNT_PERFORMANCE_REPORT</reportType>
<dateRangeType xmlns="">LAST_7_DAYS</dateRangeType>
<downloadFormat xmlns="">XML</downloadFormat>
</reportDefinition>
How I can either prevent or easily remove those blank attributes?