node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

How to add namespace to root item in builder

Open quoininc-huudatran opened this issue 6 years ago • 1 comments

Hi,

How do you add the namespace when creating the builder?

I have the following code that add the namespace, but doesn't have a good output:

        const xmlObj = {
            urlset: {
                $: { xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' },
                _: urls
            }
        };
        const builder = new xml2js.Builder();
        return builder.buildObject(xmlObj);

It generates this weird thing at the bottom:

sitemap_1_xml

With the following code, I don't get the namespace:

        const builder = new xml2js.Builder({
            rootName: 'urlset',
            $: { xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' }
        });
        return builder.buildObject(urls);
sitemap_2_xml

Any way to fix this?

quoininc-huudatran avatar Oct 31 '19 22:10 quoininc-huudatran

Also interested in this. What's the best workaround?

rdupz avatar Apr 19 '23 12:04 rdupz