如何注释 XML 标记内的属性?

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

How do I comment attributes inside an XML tag?

xmlcommentsxml-commentsxml-attribute

提问by Ntropy Nameless

Is it possible to comment one or several attributes inside an XML tag? Something like /* */from C.

是否可以在 XML 标记中注释一个或多个属性?类似于/* */来自 C.

I have tried using <!-- -->, but it was unsuccessful.

我曾尝试使用<!-- -->,但没有成功。

<element
    attribute1="value1"
    attribute2="value2"
    <!-- attribute3="value3" (commented value) -->
>

回答by Jeroen Mostert

No, this isn't possible. Comments are not allowed in an XML open tag. Depending on your application, you might get away with "commenting out" the attributes by prefixing their names with "_", or you might not (if the XML is validated against a schema or all attributes are parsed). Because whitespace is allowed, and most editors support line operations, you can "comment" multiple attributes easily this way:

不,这是不可能的。XML 开放标记中不允许有注释。根据您的应用程序,您可能会通过在属性名称前加上“_”前缀来“注释掉”属性,或者您可能不会(如果根据架构验证 XML 或解析所有属性)。由于允许使用空格,并且大多数编辑器都支持行操作,因此您可以通过这种方式轻松“注释”多个属性:

<element
   _attr1="value1"
   _attr2="value2"
   _attr3="value3"
>

But these attributes are still part of the document.

但是这些属性仍然是文档的一部分。

回答by Andy

The only compliant way is to create a node without the attribute in question. I regularly use this approach:

唯一合规的方法是创建一个没有相关属性的节点。我经常使用这种方法:

<div>
<!-- This opening div tag replaces the one above.
<div my-attribute="my-value"> -->
  div contents here...
</div>

The comment to clarify what the commented-out open tag is depends on your need (co-workers using this code, etc.).

澄清注释掉的开放标签是什么的注释取决于您的需要(使用此代码的同事等)。

Then, when you need to shift things around, simply change it to:

然后,当您需要改变事物时,只需将其更改为:

<!-- <div>
This opening div tag replaces the one below. -->
<div my-attribute="my-value">
  div contents here...
</div>

Again, your need for commenting will change with each case.

同样,您对评论的需求会因每种情况而异。

It's simple and allows you to do copy/paste to comment/uncomment as you would in "normal" coding.

它很简单,允许您像在“正常”编码中一样进行复制/粘贴以进行注释/取消注释。

回答by ggb667

Sorry, no it is not.

对不起,不,不是。

From Liam R. E. Quin at w3.org: (Asked if it were possible to comment out attributes if not now then in a future version of XML):

来自 w3.org 的 Liam RE 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">
> 
> 
> 

but the use of the same start & end delimiter caused a lot of problems, and we got rid of that feature when we defined XML. I'd wanted to change comment start & end to --* and *-- which would have let us keep the ability to have comments inside tags & declarations, and for a while it was in the XML spec, but I seem to remember it was dropped because of SGML compatibility problems.

I'm afraid it's no longer possible to change XML in incompatible ways - it has become too pervasive - and we no longer have a Working Group doing active work in XML itself.

Thank you for writing.

Liam

> 
>     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 规范中,但我似乎记得由于 SGML 兼容性问题,它被删除了。

恐怕不再可能以不兼容的方式更改 XML - 它已经变得太普遍了 - 我们不再有一个工作组在 XML 本身做积极的工作。

谢谢你的写作。

利亚姆

回答by Beri

That operation is not valid. You can't comment attributes of xml node tags. If you are looking to add some comments to your attributes, place your comment above target node.

该操作无效。您不能评论 xml 节点标签的属性。如果您想为您的属性添加一些注释,请将您的注释放在目标节点上方。

< !-- -- >is a valid way to put comments inside an xml file, but it should be placed as a xml node, not a "node attribute" (inside another node tag).

< !-- -- >是将注释放入 xml 文件的有效方法,但它应该作为 xml 节点放置,而不是“节点属性”(在另一个节点标记内)。

Example with HTML:

HTML 示例:

<!-- I can comment before the node -->
<div>This node I want to comment</div>
<!-- I can comment after the node -->

But this is not allowed:

但这是不允许的:

<div       <!--attribute="12" --> >

According to W3C documentation

根据W3C 文档

Note that comments are markup.

请注意,注释是标记。

Reference:

参考: