views:

77

answers:

3

Is the code below valid XML?

<sometag
    attribute1="value1"
    <!-- attribute2="value2" -->
    attribute3="value3">

</sometag>
+1  A: 

No. Comments cannot appear in the middle of tags.

David Dorward
+6  A: 

Questions like this are best answered by referring to the relevant specifications. In this case the Extended Markup Language (XML) 1.1 specification.

This says:

Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar.

And the grammar for an element start tag is:

[40]    STag       ::=      '<' Name (S  Attribute)* S? '>'
[41]    Attribute  ::=      Name Eq AttValue

where the non-terminal symbols S Name Eq and AttValue are defined elsewhere. These 2 productions (and the others which I haven't included here) do not allow the Comment non-terminal symbol in this context.

So that is a definitive NO.

Stephen C
Thanks for the link, and thanks for explicitly referencing the grammar.
aioobe
I respect that your answer is correct, but I disagree with answering these questions by referring to specifications - Would you ask someone to refer to the C language specification on whether or not a comment was valid?
Kragen
@Kragen - 1) depending on who asked, yes I would. 2) It is what the OP **actually wanted**. I don't think it is appropriate to treat someone with ~12k reputation points as if they were a beginner. A good software engineer implements according to the relevant specifications. If he doesn't, he's not being professional, IMO.
Stephen C
"is it valid?" = no.Will many parsers still accept it? = yesWhich unfortunately duplicates the browser problem and HTML. Tolerating bad markup promotes bad markup.That's why looking at the spec is good. Even tho you may not have an error on your platform doesn't promise you won't have a problem on another platform.
Jim Leonardo
I commented - *"A good software engineer implements according to the relevant specifications."*. Its a bit more complicated that that. For pragmatic reasons it may be necessary "bend the rules" to cope with someone / something else's non-compliance with standards. But that should be the exception rather than the rule.
Stephen C