如何注释掉包含注释的 XML 部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8578616/
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
How do I comment out an XML section that has comments inside?
提问by dov.amir
How do I comment out an XML section that has comments inside?
如何注释掉包含注释的 XML 部分?
The following won't work. It only comments out stuff0:
以下将不起作用。它只注释掉stuff0:
<!--
stuff0
<!-- stuff1 -->
stuff2
<!-- stuff3 -->
stuff4
<!-- stuff5 -->
stuff6
-->
采纳答案by Vincent Biragnet
--is not allowed in XML comments. You can add a space between the two -. If you want to do that programmatically, an XSLT may do the job for you. For example, the following XSLT:
--不允许出现在 XML 注释中。您可以在两者之间添加一个空格-。如果您想以编程方式执行此操作,XSLT 可能会为您完成这项工作。例如,以下 XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<a>
<xsl:comment select="'a -- b -- c'"/>
</a>
</xsl:template>
</xsl:stylesheet>
Gives the following output:
给出以下输出:
<a><!--a - - b - - c--></a>
But the XSLT processor may also output an error. Given the specification, it's up to the implementation to add a space or raise an error.
但 XSLT 处理器也可能输出错误。根据规范,添加空格或引发错误取决于实现。
回答by The Impaler
You shouldn't do it, I know. I mean, per the XML specification you aren't supposed to. Stop reading this answer. It's a hack!
你不应该这样做,我知道。我的意思是,根据 XML 规范,您不应该这样做。停止阅读这个答案。这是一个黑客!
Having said that... I really needed it badly, so here's the trick. Surround the section you want to comment with an unused processing instruction:
话虽如此......我真的非常需要它,所以这就是诀窍。用未使用的处理指令包围要评论的部分:
<some-tags />
<?comment
<!-- traditional comment 1 -->
<badtag prop1="123">
<!-- traditional comment 2 -->
</badtag>
<!-- traditional comment 3 -->
?>
<rest-of-my-tags />
You can use any processing instruction that's not in use. I chose "comment" as the unused processing instruction, so I started the big comment with <?commentand ended it with ?>.
您可以使用任何未使用的处理指令。我选择了“评论”作为未使用的处理指令,所以我开始大评论<?comment并以?>.
Pretty much any word will do as a processing instruction, since they aren't really used most of the time. I mean, when was the last time you used one?
几乎任何单词都可以作为处理指令,因为它们大部分时间都没有真正使用。我的意思是,你最后一次使用它是什么时候?
回答by Ondrej Tucny
You don't. XML as per its specificationdoesn't support nested comments.
你没有。XML 根据其规范不支持嵌套注释。
回答by Paul Butcher
You have two options:
您有两个选择:
- Delete it, rather than commenting out - you can always revert afterwards.
- Replace all instances of
--with something else first.
- 删除它,而不是注释掉 - 您可以随时恢复。
--首先用其他东西替换所有实例。
回答by mbmast
Here's another approach: You could just use nested comments as though it were supported in XML:
这是另一种方法:您可以使用嵌套注释,就好像它在 XML 中受支持一样:
<!-- Outer comment
<SomeXML> ... </SomeXML>
<!-- Inner comment
<SomeXML> ... </SomeXML>
-->
-->
Then only use thisXML inside a text editor. The XML you actually feed into a process is the output of some simple program you write that reads the nested-comment-XML and spits out unnested-comment-XML.
然后仅在文本编辑器中使用此XML。您实际提供给流程的 XML 是您编写的一些简单程序的输出,这些程序读取嵌套注释 XML 并吐出 unnested-comment-XML。
Another similar trick is to use a C compiler's preprocessor. I did this years ago with Microsoft's C++ compiler. The compiler, cl.exe, actually invoked two separate programs, cl1.exe and cl2.exe. Cl1.exe is the preprocessor. So you could feed anything into that thing (XML, nmake scripts, whatever) and it'll preprocess it. So something like this would work:
另一个类似的技巧是使用 C 编译器的预处理器。几年前我用微软的 C++ 编译器做了这个。编译器 cl.exe 实际上调用了两个独立的程序 cl1.exe 和 cl2.exe。Cl1.exe 是预处理器。因此,您可以将任何内容(XML、nmake 脚本等)输入到该内容中,然后它会对其进行预处理。所以这样的事情会起作用:
<SomeXML> ... </SomeXML>
#ifdef PRODUCE_THIS
<SomeMoreXML> ... </SomeMoreXML>
#endif

