Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quotes in XML. Single or double?

Tags:

formatting

xml

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?

like image 685
Sergey Metlov Avatar asked Jul 23 '11 12:07

Sergey Metlov


People also ask

Are double quotes allowed in XML?

Double quotes, "" , and single quotes, '' , are valid entities in XML file when they are part of element value in XML.

Can you use single quotes 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.

Are quotes allowed in XML?

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.

Should quotes be double or single?

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.


1 Answers

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).

like image 89
Oded Avatar answered Oct 06 '22 18:10

Oded