I am using classic ASP to create sitemap.xml files however I am getting a small issue which I cant figure out. My objective is to obey this:
http://www.sitemaps.org/protocol.html
The issue in my output. I can not figure out how to stop the xmlns="" being printed inside the url tag. it should only be showing in the urlset tag
Here is the cut n paste working ASP/VBScript:
Dim theDom, theRoot, theParent,theChild, theID, docInstruction
Set theDom = Server.CreateObject("Microsoft.XMLDOM")
Set theRoot = theDom.createElement("urlset")
Set theID = theDom.createAttribute("xmlns")
theID.Text = "http://www.sitemaps.org/schemas/sitemap/0.9"
theRoot.setAttributeNode theID
theDom.appendChild theRoot
Set theParent = theDom.createElement("url")
Set theChild = theDom.createElement("loc")
theChild.Text = "http://someURL.com"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theChild = theDom.createElement("changefreq")
theChild.Text = "weekly"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theParent = theDom.createElement("url")
Set theChild = theDom.createElement("loc")
theChild.Text = "http://someOtherUrl.com"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theChild = theDom.createElement("changefreq")
theChild.Text = "weekly"
theRoot.appendChild theParent
theParent.appendChild theChild
Set docInstruction = theDom.createProcessingInstruction("xml","version='1.0' encoding='UTF-8'")
theDom.insertBefore docInstruction, theDom.childNodes(0)
theDom.Save Server.MapPath("MyXMLDoc.xml")
and here is the output of the XML file it creates. How can I stop the xmlns="" from showing in the url tag but make sure it stays in the urlset tag??
<?xml version="1.0" encoding="UTF-8" ?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <url xmlns="">
<loc>http://someURL.com</loc>
<changefreq>weekly</changefreq>
</url>
- <url xmlns="">
<loc>http://someOtherUrl.com</loc>
<changefreq>weekly</changefreq>
</url>
</urlset>
Try using CreateNode rather than CreateElement. If you pass the namespace as the third argument, the xmlns attribute isn't added to the element anymore. Something like this works for me:
Dim theDom, theRoot, theParent,theChild, theID, docInstruction
const NODE_ELEMENT = 1
const xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
Set theDom = Server.CreateObject("Microsoft.XMLDOM")
Set theRoot = theDom.createElement("urlset")
Set theID = theDom.createAttribute("xmlns")
theID.Text = xmlNs
theRoot.setAttributeNode theID
theDom.appendChild theRoot
Set theParent = theDom.createNode(NODE_ELEMENT, "url", xmlns)
Set theChild = theDom.createNode(NODE_ELEMENT, "loc", xmlns)
theChild.Text = "http://someURL.com"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theChild = theDom.createNode(NODE_ELEMENT, "changefreq", xmlns)
theChild.Text = "weekly"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theParent = theDom.createNode(NODE_ELEMENT, "url", xmlns)
Set theChild = theDom.createNode(NODE_ELEMENT, "loc", xmlns)
theChild.Text = "http://someOtherUrl.com"
theRoot.appendChild theParent
theParent.appendChild theChild
Set theChild = theDom.createNode(NODE_ELEMENT, "changefreq", xmlns)
theChild.Text = "weekly"
theRoot.appendChild theParent
theParent.appendChild theChild
Set docInstruction = theDom.createProcessingInstruction("xml","version='1.0' encoding='UTF-8'")
theDom.insertBefore docInstruction, theDom.childNodes(0)
theDom.Save Server.MapPath("MyXMLDoc.xml")
It generates the following output:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://someURL.com</loc>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>http://someOtherUrl.com</loc>
<changefreq>weekly</changefreq>
</url>
</urlset>