在XML标签内部注释一个或多个属性是否可能?类似于C中的/* */
。
我已经尝试使用<!-- -->
,但是没有成功。
<element
attribute1="value1"
attribute2="value2"
<!-- attribute3="value3" (commented value) -->
>
在XML标签内部注释一个或多个属性是否可能?类似于C中的/* */
。
我已经尝试使用<!-- -->
,但是没有成功。
<element
attribute1="value1"
attribute2="value2"
<!-- attribute3="value3" (commented value) -->
>
不,这是不可能的。在XML开放标签中不允许评论。根据您的应用程序,您可以通过在属性名称前缀加上“_”来“注释掉”属性,或者您可能无法这样做(如果XML根据架构进行验证或所有属性都被解析)。因为允许空格,并且大多数编辑器支持行操作,所以您可以轻松地通过这种方式“注释”多个属性:
<element
_attr1="value1"
_attr2="value2"
_attr3="value3"
>
但是这些属性仍然是文档的一部分。
唯一合规的方式是创建一个没有相关属性的节点。我经常使用这种方法:
<div>
<!-- This opening div tag replaces the one above.
<div my-attribute="my-value"> -->
div contents here...
</div>
关于注释掉的开放标签的说明视您的需求而定(例如,共同使用此代码的同事等)。
然后,当您需要移动内容时,只需将其更改为:
<!-- <div>
This opening div tag replaces the one below. -->
<div my-attribute="my-value">
div contents here...
</div>
再次强调,每个案例中对评论的需求都会有所不同。
此方法简单易行,使您能够像在“正常”编码中一样进行复制/粘贴来添加或取消注释。
来自w3.org的Liam R. E. Quin:(询问是否可以在XML的当前或将来版本中注释掉属性):
SGML allows this, with e.g.
<sock -- age="19" -- state="clean" -- id="s36" >
<shoe -- id="s12" ></sock>
being the same as
<sock state="clean" id="s12">
该操作无效。您不能评论xml节点标记的属性。如果您想向属性添加一些注释,请将您的注释放在目标节点上方。
<!-- -->
是在xml文件中放置注释的有效方法,但它应该被放置为一个xml节点,而不是一个“节点属性”(在另一个节点标记内部)。
HTML示例:
<!-- I can comment before the node -->
<div>This node I want to comment</div>
<!-- I can comment after the node -->
<div <!--attribute="12" --> >