MT#5879 Fix generation for ampersands;

(cherry picked from commit 00c430608881b96d937fa598ae028b5e72586d4a)
MT#5879 Not all characters should be quoted

(cherry picked from commit 1a1362db7e0685864cbe33b1f60c46a70c7f15b2)