проблема с тегом urlset при создании sitemap.xml с использованием классического ASP

Я использую классический ASP для создания файлов sitemap.xml, однако у меня возникает небольшая проблема, которую я не могу понять. Моя цель - повиноваться этому:

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

Проблема в моем выводе. Я не могу понять, как остановить печать xmlns="" внутри тега URL. он должен отображаться только в теге urlset

Вот рабочий 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>

person Mat41    schedule 16.04.2014    source источник
comment
Я смотрел и смотрел на это и не могу понять, что может быть причиной моей проблемы. Кто-нибудь?? Есть ли у кого-нибудь мнение, является ли наличие xmlns= в теге url проблемой?   -  person Mat41    schedule 22.04.2014


Ответы (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>
person Imar Spaanjaars    schedule 22.04.2014