How do I comment attributes inside an XML tag?

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

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

Solution 3:

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