How do I comment attributes inside an XML tag?
XmlCommentsXml CommentsXml AttributeXml Problem Overview
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) -->
>
Xml Solutions
Solution 1 - Xml
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 - Xml
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 - Xml
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.
> 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
Solution 4 - Xml
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: