【问题标题】:no doctype found for this xml sitemap - how to fix?找不到此 xml 站点地图的文档类型 - 如何修复?
【发布时间】:2012-08-16 16:29:25
【问题描述】:

我有一个带有 xsl 文件的 xml 格式的站点地图...我在验证站点地图时收到一个警告:“找不到此 xml 站点地图的文档类型,请检查您的语法”

这是我在尝试验证我的站点地图时收到的唯一警告...如何解决?

http://www.alecos.it/sitemap.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="sitemap.xsl" type="text/xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.alecos.it/games.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_31032007.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_30012007.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_28082006.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_26012006.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_25102008.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_15072007.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_06062005.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary_04092010.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.alecos.it/diary.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  cut... cut...
</urlset>

http://www.alecos.it/sitemap.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:html="http://www.w3.org/TR/REC-html40"
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
  xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SiteMap - Alecos Web Site By Alessandro Marinuzzi</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 68.5%;
  color: #333;
}
table {
  border: none;
  border-collapse: collapse;
}
.sitemap tr.odd {
  background-color: #f7f7f7;
}
.sitemap tbody tr:hover {
  background-color: #ddf4df;
}
.sitemap tbody tr:hover td, .sitemap tbody tr:hover td a {
  color: #060;
}
a {
  color: #000;
  text-decoration: none;
}
.intro p {
  color: #060;
}
a:hover {
  text-decoration: underline;
  color: #690;
}
table {
  font-size: 1em;
  width: 100%;
}
th {
  text-align: left;
  padding: 5px;
}
thead th {
  background-color: #ccffcc;
  border-bottom: 1px solid #99cc99;
  border-top: 1px solid #99cc99;
}
tbody tr {
  border-bottom: 1px dotted #CCC;
  padding: 5px 0;
}
</style>
</head>
<body>
<div id="content">
<h1><a href="index.php" title="Alecos Web Site"><img src="gfx/Logo.png" alt="[Alecos WebSite SiteMap]" border="0"/></a></h1>
<div class="intro">
<p>Generated By <a href="http://www.alecos.it/">www.alecos.it</a>. Windows, Linux, Unix, MacOS(X) and Amiga programs, scripts, translations, articles and more...</p>
<p>This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> URLs.</p>
</div>
<table class="sitemap" cellpadding="3">
  <thead>
    <tr>
      <th width="80%">URL</th>
      <th width="10%">Priority</th>
      <th width="10%">Change Frequency</th>
    </tr>
  </thead>
  <tbody>
    <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:for-each select="sitemap:urlset/sitemap:url">
      <tr>
        <td>
          <xsl:variable name="itemURL">
            <xsl:value-of select="sitemap:loc"/>
          </xsl:variable>
          <a href="{$itemURL}">
            <xsl:value-of select="sitemap:loc"/>
          </a>
        </td>
        <td>
          <xsl:value-of select="concat(sitemap:priority*100,'%')"/>
        </td>
        <td>
          <xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
        </td>
      </tr>
    </xsl:for-each>
  </tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我尝试了所有方法,但没有一个目标......想法?提示?技巧?

Google 没有错误或警告地接受我的站点地图,但 xml 验证器给了我一个错误/警告...谢谢

【问题讨论】:

  • 你如何写出转换后的结果,以便它显示在浏览器中?您始终可以在 XSL 中将输出类型更改为 XML,因为输出仍然是浏览器应该能够解析的 HTML(格式良好的 HTML)。然而,正如@BoltClock 所提到的,它确实不是必需的。当然,除非您在谈论 tml 文档并且正在使用 w3c 验证器。在这种情况下,只需修改 xls 模板以同时写出适当的 DOCTYPE。

标签: xml validation xslt sitemap doctype


【解决方案1】:

这是一个如何生成所需 DOCTYPE 的示例

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>

 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于此 XML 文档时:

<html>
 <title>Some Title</title>
 <body>
  <p>Hello, World!</p>
 </body>
</html>

它“按原样”复制文档并生成 XHTML 过渡文档类型

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <title>Some Title</title>
   <body>
      <p>Hello, World!</p>
   </body>
</html>

解释

使用 xsl:output 的以下属性:doctype-publicdoctype-system

【讨论】:

    【解决方案2】:

    sitemap.xsl:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0"
      xmlns:html="http://www.w3.org/TR/REC-html40"
      xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
      xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output
      omit-xml-declaration="no" method="xml"
      doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
      indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">
    <html>
    <head>
    <title>SiteMap - Alecos Web Site By Alessandro Marinuzzi</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
    body {
      font-family: Helvetica, Arial, sans-serif;
      font-size: 68.5%;
      color: #333;
    }
    table {
      border: none;
      border-collapse: collapse;
    }
    .sitemap tr.odd {
      background-color: #f7f7f7;
    }
    .sitemap tbody tr:hover {
      background-color: #ddf4df;
    }
    .sitemap tbody tr:hover td, .sitemap tbody tr:hover td a {
      color: #060;
    }
    a {
      color: #000;
      text-decoration: none;
    }
    .intro p {
      color: #060;
    }
    a:hover {
      text-decoration: underline;
      color: #690;
    }
    table {
      font-size: 1em;
      width: 100%;
    }
    th {
      text-align: left;
      padding: 5px;
    }
    thead th {
      background-color: #ccffcc;
      border-bottom: 1px solid #99cc99;
      border-top: 1px solid #99cc99;
    }
    tbody tr {
      border-bottom: 1px dotted #CCC;
      padding: 5px 0;
    }
    </style>
    </head>
    <body>
    <div id="content">
    <h1><a href="index.php" title="Alecos Web Site"><img src="gfx/Logo.png" alt="[Alecos WebSite SiteMap]" border="0"/></a></h1>
    <div class="intro">
      <p>Generated By <a href="http://www.alecos.it/">www.alecos.it</a>. Windows, Linux, Unix, MacOS(X) and Amiga programs, scripts, translations, articles and more...</p>
      <p>This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> URLs.</p>
    </div>
    <table class="sitemap" cellpadding="3">
      <thead>
        <tr>
          <th width="80%">URL</th>
          <th width="10%">Priority</th>
          <th width="10%">Change Frequency</th>
        </tr>
      </thead>
      <tbody>
        <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
        <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
        <xsl:for-each select="sitemap:urlset/sitemap:url">
          <tr>
            <td>
              <xsl:variable name="itemURL">
                <xsl:value-of select="sitemap:loc"/>
              </xsl:variable>
              <a href="{$itemURL}">
                <xsl:value-of select="sitemap:loc"/>
              </a>
            </td>
            <td>
              <xsl:value-of select="concat(sitemap:priority*100,'%')"/>
            </td>
            <td>
              <xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
            </td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
    </div>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    

    效果很好,没有错误或警告,并且 sitemap.xml 现在有一个正确的文档类型,可以毫无问题地对其进行验证。随心所欲地使用和分享!谢谢!

    【讨论】:

    • 太大了,伙计。这与最初的问题和正确答案无关——您还有一些其他错误需要查找和修复。其实这里有很多错误。请提出一个新的、单独的问题,但要使用小型 XML 文档,并指定想要的结果。看来您需要阅读一本好的 XSLT/XPath 书籍才能了解基础知识。
    • 好吧,这篇文章是错误的......我自己可以理解,但因为我知道的只是一点,而不是更多的 xsl,你能告诉我代码有多正确吗?谢谢。也许将来我会买一本关于 xml/xsl 的书,但目前我需要修复找不到 doctype 的问题...在开始时阅读我的​​第一篇文章(我在第一篇文章中询问了如何解决 doctype 的问题这就是我的问题,简单明了)。谢谢。
    • 这不是一个“帖子”——这是一个答案——实际上包含一个新问题。请提出一个新问题作为......问题 - 而不是作为答案。您的 question 已完全回答——我已经给出了一个完整的示例,您想如何完成。请从小型 XML 文档开始,并确保您的转换工作正常。为什么它不起作用 - 问一个问题。读一本书,否则你很难理解答案。
    • 我为什么要打开一个新问题?看看我现在编辑的代码......目前有效,但我不得不将输出从 xml 更改为 html,因为我只能看到纯文本而不是 html,并且存在 doctype 丢失的老问题.. .
    • user1582059,问一个新问题比较合适,因为你的代码有很多错误,这些错误*与当前问题没有直接关系。
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 2021-03-28
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多