How do I comment attributes inside an XML tag? How do I comment attributes inside an XML tag? xml xml

How do I comment attributes inside an XML tag?


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.


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.


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