XML 注释和“--”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10842131/
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 13:29:56  来源:igfitidea点击:

XML comments and "--"

xmlcomments

提问by Vi.

<!-- here is some comment --
                            ^
                            |
                    what can be here apart from '>'?

XML seems not to like '--' inside comments. I read somewhere that '--' switchs some modes inside <! ... >thing, but <!-- -- -- -->(even number of --s) seem to be invalid too. If it is some historic feature, what is "pro" part of it? ("contra" part is inability to have --in comments).

XML 似乎不喜欢注释中的“--”。我在某处读到“--”会在<! ... >事物内部切换某些模式,但是<!-- -- -- -->(偶数个--s)似乎也无效。如果它是一些历史特征,那么它的“专业”部分是什么?(“反对”部分无法--在评论中使用)。

What is the reason of complicating comment processing by not making just '-->' end of comment and allowing '--' inside?

通过不只将 '-->' 放在注释的结尾并允许 '--' 来使注释处理复杂化的原因是什么?

采纳答案by asawyer

From the standards document:

从标准文件:

http://www.w3.org/TR/REC-xml/#sec-comments

http://www.w3.org/TR/REC-xml/#sec-comments

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. They are not part of the document's character data; an XML processor may, but need not, make it possible for an application to retrieve the text of comments. For compatibility, the string " -- " (double-hyphen) must not occur within comments.]Parameter entity references must not be recognized within comments.

[定义:注释可以出现在文档中其他标记之外的任何位置;此外,它们可能出现在文档类型声明中语法允许的位置。它们不是文档字符数据的一部分;XML 处理器可以(但不是必须)使应用程序能够检索注释文本。为了 兼容性,字符串“--”(双连字符)不能出现在注释中。]不能在注释中识别参数实体引用。

回答by Daniel

Maybe it can be helpful for someone. I had a problem, that I wanted to comment out a command line parameter in XML that starts with --:

也许它对某人有帮助。我有一个问题,我想在 XML 中注释掉一个以 -- 开头的命令行参数:

<arg line="-v --line-break 0" />  

so naturally normal way like this

像这样自然正常的方式

<!-- <arg line="-v --line-break 0" /> -->

didn't work, but I found out, that if the -is replaced by it's UTF-8 equivalent &#x002D;or &#45;it works and can be tolerated inside comments.

没有用,但我发现,如果-被替换为等效的 UTF-8&#x002D;或者&#45;它可以工作并且可以在注释中容忍。

So in my case the string

所以在我的情况下,字符串

<arg line="-v &#45;&#45;line-break 0" />

is parsed correctly and can be part of comments.

被正确解析,可以成为评论的一部分。

Of course it looks a little ugly, but if someone want to keep a string with -- as comment in his XML - I think it's still better than nothing.

当然它看起来有点难看,但是如果有人想保留一个字符串——作为他的 XML 中的注释——我认为它仍然比没有好。

回答by Michael Kay

It's one of those stupid rules that's in XML because it was in SGML and people didn't want to break compatibility. Why it's in SGML is anyone's guess: probably because it saved three lines of code in the original parser.

这是 XML 中那些愚蠢的规则之一,因为它是 SGML,人们不想破坏兼容性。为什么它在 SGML 中是任何人的猜测:可能是因为它在原始解析器中保存了三行代码。

回答by RichardTowers

--is not allowed for compatibility with SGML. From On SGML and HTML:

--不允许与 SGML 兼容。从SGML 和 HTML 开始

White space is not permitted between the markup declaration open delimiter("<!") and the comment open delimiter ("--"), but is permitted between the comment close delimiter ("--") and the markup declaration close delimiter (">"). A common error is to include a string of hyphens ("---") within a comment. Authors should avoid putting two or more adjacent hyphens inside comments.

标记声明开始定界符("<!") 和注释开始定界符("--") 之间不允许有空格,但注释结束定界符("--") 和标记声明结束定界符( ">")。一个常见的错误是在注释中包含一串连字符(“---”)。作者应避免在评论中放置两个或多个相邻的连字符。

So in SGML <!and >open and close "markup declarations" and --opens and closes comments.

因此,在SGML<!>开闭“标记声明”,并--打开和关闭评论。

回答by Faustas

This problem sooner or later will affect anyone who likes to use comments in XML to disable the content that they don't need. I had major issues for several days with Spring context configs failing to load, without any detail explanation why. Problem was a habit to comment blocks of content like this:

这个问题迟早会影响到任何喜欢在 XML 中使用注释来禁用他们不需要的内容的人。几天来,我遇到了 Spring 上下文配置无法加载的重大问题,但没有详细解释原因。问题是对这样的内容块进行评论的习惯:

<value>ABC1</value>
<!-- <value>ABC2</value> -->
<value>ABC3</value>
<value>ABC1</value>
<!-- <value>ABC2</value> -->
<value>ABC3</value>

commented out it has to be changed to this:

注释掉它必须更改为:

<!--
    <value>ABC1</value>
    !-- <value>ABC2</value> --
    <value>ABC3</value>
-->

The dashes in the commented out block instead of being ignored, were messing up the parsing of the whole file no matter where they are placed.

注释掉的块中的破折号而不是被忽略,无论它们放在什么地方,都会弄乱整个文件的解析。