What's the accepted way of storing quoted data in XML? What's the accepted way of storing quoted data in XML? xml xml

What's the accepted way of storing quoted data in XML?


Your correct answer is A & C as the " is not a character that must be encoded in element data.

You should always be XML encoding characters such as >, <, and & to ensure that you don't have issues if they are NOT inside a CDATA section. These are key items to be concerned about for element data.

When talking about attributes you have to then also be careful of ' and " inside attribute values depending on the type of symbol you use to surround the value.

I've found that often encoding " and ' is a better idea in all aspects as it helps at times when converting to other formats, where the " or ' might cause problems there as well.


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.

That's not to say that escaping the quotes is not a good idea - I'm just saying that not escaping the quotes is perfectly valid XML. See section 2.4 - "Character Data and Markup" in the XML spec.

So both (a) and (c) are OK.

As far as attributes are concerned, attribute values can be enclosed in either single or double quotes, so if it contains one or the other you can use the opposite one to enclose the value. If it'll contain both, then you'll have to use a character entity for one or both.

As far as 'curly-quotes' are concerned, if you're talking about the special, non-ASCII quotes that Word sometimes converts quotes to - they have no special meaning in XML, so you can do whichever (but they can't be used to enclose attribute values". You'll also need to make sure the character encoding for the document is correct, so they are interpreted correctly.


Double quotes in text nodes can be represented either as the double-quote character or as the " entity. Double quotes in attribute values can be represented as the double-quote character if the value is delimited by single quotes, and vice versa; otherwise, escape them as "

This is only relevant if you're a) editing XML in a non-XML-aware text editor or b) creating XML programmatically through string manipulation. Generally speaking, you should avoid (a) unless you really know what you're doing, or at least have a way of checking the well-formedness of your XML after editing is complete.

And you should avoid (b) under all circumstances. Never create XML through string manipulation; always use a DOM or some other tool.