How do I comment attributes inside an XML tag?

时光总嘲笑我的痴心妄想 提交于 2019-12-09 02:27:47

问题


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

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

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

回答1:


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:

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

But these attributes are still part of the document.




回答2:


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.




回答3:


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):

> 
>     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




回答4:


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.

< !-- -- > 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).

Example with 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

Note that comments are markup.

Reference:

  • http://www.functionx.com/xml/Lesson04.htm
  • https://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4
  • How to comment HTML tag attribute in HTML source code?


来源:https://stackoverflow.com/questions/26461869/how-do-i-comment-attributes-inside-an-xml-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!