58

I want to have an XML attribute without any value, which simply has one meaning when it exists or does not exist.

Is that valid?

Sionnach733
  • 4,686
  • 4
  • 36
  • 51
Andreas
  • 591
  • 1
  • 4
  • 3

3 Answers3

73

An attribute must be specified with the following syntax:

Name Eq AttValue

where Name is a legal XML name, Eq is = optionally preceded or followed by whitespace, and AttValue is a legal attribute value.

This definition is true for both XML 1.0 and XML 1.1.

If you are trying to specify an attribute as below:

<car owned/>

then no, that is not valid. If you are trying to specify it this way:

<car owned=""/>

then yes, that is valid.

Dave DuPlantis
  • 6,378
  • 3
  • 26
  • 30
25

No.

Boolean attributes in XML are of the form foo="foo".

Even in SGML, you must provide the value, (it is the name, = and quotes that you can omit, which is why you have things like <select multiple> in HTML).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 10
    I have hard time parsing your remark about attribute's value being required in SGML followed by example where the value is omitted. As to where do HTML attributes without value come from; *Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form. For instance, authors may want to specify: ` – Piotr Dobrogost Aug 13 '18 at 14:11
  • 1
    @PiotrDobrogost — In the example you give, `selected` is the **value** not the **name** of the attribute (HTML happens to only use values which are the same as the name for boolean attributes). – Quentin Aug 13 '18 at 14:55
4

You can have an attribute whose only permitted value is the empty string, "". I'm not sure it's good design, though; I would normally suggest a boolean attribute with values true/false, and a default value of false.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 23
    An empty string is still a value though. – Quentin Aug 03 '11 at 13:46
  • 7
    My answer has been downvoted presumably because people think I interpreted the question incorrectly. They may be right. Interpreting unclear questions is a bit of an art form, and we sometimes get it right and sometimes not. A comment on the answer is more helpful than an unexplained downvote. – Michael Kay Mar 14 '20 at 22:50