如何修复错误:根元素后面的文档中的标记必须格式正确

Mer*_*nid 5 xml xslt xml-validation xml-parsing

我把我的代码放在XML验证网站上,它给了我这个错误:

第8行:4根元素后面的文档中的标记必须格式正确.

有问题的<xsl:output method = "html" doctype-system = "about:legacy-compat"/>行是,行.

XML

<?xml version="1.0"?>

<!-- Fig. 15.21: sorting.xsl -->
<xsl:stylesheet version = "1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>

<!-- write XML declaration and DOCTYPE DTD information -->
*<xsl:output method = "html" doctype-system = "about:legacy-compat" />*

 <!-- match document root -->
 <xsl:template match="/"> -<html> <xsl:apply-templates/> </html> 
 </xsl:template>
Run Code Online (Sandbox Code Playgroud)

kjh*_*hes 24

一般情况

根元素后面的文档中的标记必须格式正确.

此错误表示您的XML在根元素后面有标记.为了格式良好,XML 必须只有一个根元素,并且单个根元素后面不能再有标记.

一个根元素示例(GOOD)

<r>
  <a/>
  <b/>
  <c/>
</r>
Run Code Online (Sandbox Code Playgroud)

此错误的最常见来源是:

  1. 包括杂散或额外关闭标签(BAD):

    <r>
      <a/>
      <b/>
      <c/>
    </r>
    </r>  <!-- shouldn't be here -->
    
    Run Code Online (Sandbox Code Playgroud)
  2. 故意有多个根元素(BAD):

    <a/>
    <b/>  <!-- second root element shouldn't be here -->
    <c/>  <!-- third root element shouldn't be here -->
    
    Run Code Online (Sandbox Code Playgroud)
  3. 无意中有多个根元素(BAD):

    <r/>  <!-- shouldn't be self-closing -->
      <a/>
      <b/>
      <c/>
    </r>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 解析不同于您的XML(BAD):

    在提供失败的解析之前立即记录XML,以确保解析器看到的XML与您认为它看到的XML相同.这里的常见错误包括:

    • 传递给解析器的XML文档的文件名与您认为的不同.
    • XML的缓冲区很脏.确保在添加XML之前已将其清除.
    • 您的管道中前一个阶段的早期程序在解析之前更改XML,从而产生此错误消息.

你的特殊问题

在您的特定情况下,您的XML似乎具有多个根元素,因为该xsl:stylesheet元素过早关闭(上面的案例#3).

更改

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
Run Code Online (Sandbox Code Playgroud)

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Run Code Online (Sandbox Code Playgroud)

解决你的问题,并添加一个结束标记,

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

如果您的真实文档中尚不存在.