I've heard that using single quotes to surround XML attribute values is a "bad style". Is this correct?
Should I always write:
<element attr="value">
Or is it acceptable to write:
<element attr='value'>
Or does it not matter which style I use?
Double quotes, "" , and single quotes, '' , are valid entities in XML file when they are part of element value in XML.
Attribute values are required to be quoted. It is illegal to omit quotation marks around attribute values. Either single or double quotes can be used.
Character data inside XML elements can contain quote characters without escaping them. The only characters that are not permitted inside an XML element are '<', '&' and '>' (and the '>' character is only disallowed if it's part of a "]]>" sequence of characters.
As a general rule, British usage has in the past usually preferred single quotes for ordinary use, but double quotes are now increasingly common; American usage has always preferred double quotes.
Both are legal. Choose one and stick with it. It doesn't matter.
From the spec:
AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
Showing that both are valid, as is mixing the two styles within an element, per attribute (though I suggest being consistent within any single document/set of documents).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With