How do I comment attributes inside an XML tag?
Asked Answered
B

4

73

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) -->
>
Bovill answered 20/10, 2014 at 8:59 Comment(0)
A
52

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.

Aesthetics answered 20/10, 2014 at 9:8 Comment(1)
Interesting onePegues
S
18

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.

San answered 22/12, 2015 at 21:11 Comment(0)
W
15

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

Woll answered 26/12, 2016 at 20:53 Comment(2)
Do you have a source for this quote?Veradis
An email fromn Liam to me personally.Woll
F
7

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:

Forepart answered 20/10, 2014 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.