0

I'm using node.js and xml2js to create a xml sitemap.xml.

It looks all fine, but when I want to define the attributes e.g.:

'$': {
    'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9'
},

... like this:

var obj = {
    'urlset': {
        '$': {
            'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9'
        },
        'url': [
            {
                'loc': URL_DOMAIN,
                'changefreq': 'monthly',
                'priority': 1
            },
            {
                'loc': URL_DOMAIN+'/data-privacy',
                'changefreq': 'monthly',
                'priority': 0.5
            }
        ]
    }
};

var builder = new xml2js.Builder({ xmldec: {'version': '1.0', 'encoding': 'UTF-8'} });
var xml = builder.buildObject(obj);

res.header('Content-Type','text/xml').send(xml) 

... the atributes for the urlset won't render:

<urlset>
    <url>
        <loc>http://127.0.0.1:2000</loc>
        <changefreq>monthly</changefreq>
        <priority>1</priority>
        </url>
    <url>
        <loc>http://127.0.0.1:2000/data-privacy</loc>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
</url>  

... I would expect:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    ...
</url>  

What am I missing?

... If I do a test:

    'urlset': {
        '$': {
            'test': 'test'
        },

... it works:

<urlset test="test">
    ...
</url>  
Philipp M
  • 3,306
  • 5
  • 36
  • 90
  • I don't see any problem with your code. It works absolutely fine for me :). [See here](https://dualhauntingtoolbox.nithinthampi.repl.co). [Code here](https://repl.it/@nithinthampi/DualHauntingToolbox). You probably need to check the server you using. I'm using express in this case. If you are using express, see the versions of packages used as well. Might help. – Nithin Thampi Oct 28 '19 at 15:23
  • ... but in your case I also only see: + as output ... instead of + . Is this normal behaviour if I don't see the attribute although its there and applied? – Philipp M Oct 28 '19 at 16:47
  • Most probably a browser issue then. Please check my answer (not really an answer as it doesn't solve your problem). But do follow the steps so that we can conclude that its a browser issue. – Nithin Thampi Oct 28 '19 at 19:33

1 Answers1

1

This is not an answer, but an attempt to prove that the OP's code is valid.

I have copied your code and created a REPL .

https://repl.it/@nithinthampi/PeriodicBraveClients.

Click Run code snippet below to see that the response do have the xml namespace (You can copy the code an run in your browser console too)

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

fetch("https://periodicbraveclients.nithinthampi.repl.co/").then(res => res.text()).then(xml => console.log(xml))

Probably got something to do with browser may be. Below is my browser screenshot.

This is what I see in browser

Nithin Thampi
  • 3,579
  • 1
  • 13
  • 13