如何在XML中注释出一个标签块?
例如,在下面的代码中,我怎样才能注释掉<staticText>
和它里面的所有内容?
<detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
</band>
</detail>
我可以使用<!-- staticText-->
,但那只是针对单个标签(据我所知),比如Java和C中的//
。
你可以在多行中使用这种风格的注释(在HTML中也存在)。
<detail>
<band height="20">
<!--
Hello,
I am a multi-line XML comment
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
-->
</band>
</detail>
如果你问,因为你在使用<!--->
语法时出错,很可能是CDATA部分(还有]]>
部分),然后位于注释的中间。这应该不会有什么区别,但理想和现实世界有时会有很大的差距(尤其是在XML处理方面)。
试着把"]]> "也改一下。
<!--detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]--><!--]></text>
</staticText>
</band>
</detail-->
还有一件事,我想到了。如果你的XML的某个地方的内容包含两个连字符,那么注释就立即在那里结束。
<!-- <a> This is strange -- but true!</a> -->
--------------------------^ comment ends here
这是个很常见的陷阱。它是从SGML处理注释的方式继承下来的。(阅读关于这个主题的XML规范)