Mixing Quotes in XML Attribute Values

XML allows single quotes to appear inside double-quoted attribute values—and vice versa—because of how XML parsers determine where an attribute value begins and ends. The parser uses the opening quotation mark as the designated boundary delimiter and scans the string until it encounters the exact matching delimiter. This rule allows developers to include literal quote characters inside attributes cleanly without relying on entity encoding.

The Parsing Mechanism

According to the W3C XML specification, an attribute value (AttValue) must be enclosed either in a matching pair of single quotes (') or double quotes (").

When an XML parser processes an attribute: 1. It identifies the opening quote character (" or '). 2. It treats all subsequent characters as literal string content. 3. It stops reading the attribute value only when it reaches the identical closing quote character.

Because the parser is strictly searching for the matching delimiter, any alternative quotation mark is interpreted as standard character data rather than a structural delimiter.

Practical Examples

You can embed single quotes directly inside an attribute enclosed by double quotes:

<element message="It's a valid XML string" />
<button onclick="alert('Hello, World!')" />

Conversely, you can embed double quotes directly inside an attribute enclosed by single quotes:

<element quote='She said, "XML is simple."' />

When to Use Entity References

The only scenario where an embedded quote causes a syntax error is when it matches the enclosing delimiter. In those cases, the inner quote must be escaped using its predefined XML entity reference:

Alternating between single and double quotes avoids the need for these entity references, keeping XML documents cleaner and more human-readable.