使用经典ASP创建sitemap.xml时的urlset标记问题

时间:2014-04-16 04:30:18

标签: xml asp-classic sitemap xmldom

我正在使用经典的ASP来创建sitemap.xml文件但是我遇到了一个小问题,我无法弄清楚。我的目标是遵守这个:

http://www.sitemaps.org/protocol.html

我输出中的问题。我无法弄清楚如何停止xmlns =""被打印在url标签内。它应该只显示在urlset标签

这是剪辑n粘贴工作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")

,这是它创建的XML文件的输出。如何停止xmlns =""从显示在url标签但确保它保留在urlset标签?

  <?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>

1 个答案:

答案 0 :(得分:1)

尝试使用CreateNode而不是CreateElement。如果将命名空间作为第三个参数传递,则xmlns属性不再添加到元素中。这样的事情对我有用:

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

它会生成以下输出:

<?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>