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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46355454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:13:12  来源:igfitidea点击:

How to fix error: The markup in the document following the root element must be well-formed

xmlxsltxml-parsingxml-validation

提问by Mereinid

I put my code in the XML validation website and it gives me this error:

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

Line 8: 4 The markup in the document following the root element must be well-formed.

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

The line that is having an issue is the <xsl:output method = "html" doctype-system = "about:legacy-compat"/>, line.

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

XML

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>

回答by kjhughes

General case

一般情况

The markup in the document following the root element must be well-formed.

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

This error indicates that your XML has markup following the root element. In order to be well-formed, XML must have exactly oneroot element, and there can be no further markup following the single root element.

此错误表明您的 XML 在根元素之后有标记。为了格式良好,XML必须恰好有一个根元素,并且在单个根元素之后不能有进一步的标记。

One root element example (GOOD)

一个根元素示例(好)

<r>
  <a/>
  <b/>
  <c/>
</r>

The most common sources for this error are:

此错误的最常见来源是:

  1. Including stray or extra close tags (BAD):

    <r>
      <a/>
      <b/>
      <c/>
    </r>
    </r>  <!-- shouldn't be here -->
    
  2. Intentionally having multiple root elements (BAD):

    <a/>
    <b/>  <!-- second root element shouldn't be here -->
    <c/>  <!-- third root element shouldn't be here -->
    
  3. Unintentionally having multiple root elements (BAD):

    <r/>  <!-- shouldn't be self-closing -->
      <a/>
      <b/>
      <c/>
    </r>
    
  4. Parsing different XML than you think (BAD):

    Log the XML immediately before providing to the parse that's failing in order to make sure that the XML that the parser is seeing is the same as the XML you think it's seeing. Common errors here include:

    • The filename of the XML document being passed to the parser differs from what you believe it is.
    • The buffer of the XML being dirty. Make sure it's been cleared prior to adding your XML.
    • An earlier program from a prior stage in your pipeline changing the XML prior to the parsing that's yielding this error message.
  1. 包括杂散或额外的关闭标签 (BAD):

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

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

    <r/>  <!-- shouldn't be self-closing -->
      <a/>
      <b/>
      <c/>
    </r>
    
  4. 解析与您想象的不同的 XML (BAD):

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

    • 传递给解析器的 XML 文档的文件名与您认为的不同。
    • XML 的缓冲区变脏。在添加 XML 之前确保它已被清除。
    • 来自管道中先前阶段的较早程序在产生此错误消息的解析之前更改了 XML。


Your particular problem

你的特殊问题

In your particular case, your XML appears to have multiple root elements because the xsl:stylesheetelement is closed prematurely (case #3above).

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

Change

改变

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>

to

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

to fix your immediate problem, and add a closing tag,

解决您的紧迫问题,并添加结束标记,

</xsl:stylesheet>

if one does not already exist in your real document.

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

回答by fady ahmed

this also may show up because of wrong spaces in this file

这也可能因为此文件中的空格错误而显示