How to quote and encode XML attribute valuesFri, Oct 21, 2005 in Programming
Attribute values in XML are usually double-quoted, but single-quotes can be used as well, according to the relevant part of the XML Spec. Here is the production:
 AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
In plain English: an attribute consists of
- a double or single quote
- any number of the following:
- any character but <, & or “ OR
- an entity reference
- the same character that was used to begin the attribute (double or single quote)
What’s most interesting about this to me is that a < is forbidden inside attribute values, but a > is not. I always assumed both were illegal.
This is why I love reading specs. The XML spec is a great example of clear and terse writing. There is no chance for confusion when reading the productions themselves! Any secondary source can only obscure the matter, in my opinion.
I'm Baron Schwartz, the founder and CEO of VividCortex. I am the author of High Performance MySQL and lots of open-source software for performance analysis, monitoring, and system administration. I contribute to various database communities such as Oracle, PostgreSQL, Redis and MongoDB. More about me.