3

Is the code below valid XML?

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

</sometag>
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • When i tried this in xmlspy or try to open the xml in IE it gives error.So i think this is not valid – Raghuram Aug 03 '10 at 05:02

3 Answers3

6

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
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for the link, and thanks for explicitly referencing the grammar. – aioobe Aug 02 '10 at 22:42
  • 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? – Justin Aug 02 '10 at 23:43
  • 1
    @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 Aug 03 '10 at 00:09
  • "is it valid?" = no. Will many parsers still accept it? = yes Which 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 L Aug 03 '10 at 00:22
  • 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 Aug 05 '10 at 13:55
1

No. Comments cannot appear in the middle of tags.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335