Fixing the “&” Symbol Issue in Google Text-to-Speech SSML

If you’ve ever tried using Google’s Text-to-Speech (TTS) API with SSML and encountered an error when using the “&” (ampersand) symbol, you’re not alone. This issue is a common pitfall for developers working with Speech Synthesis Markup Language (SSML). In this article, we’ll explore why this happens and how to fix it.

Understanding the Issue

Google’s Text-to-Speech API uses SSML, which is based on XML. In XML, certain characters are reserved because they have special meanings. The ampersand (&) is one of these characters, as it is used to define entity references like &amp;for & or &lt; for <.

Why is “&” Not Allowed in SSML?

In XML (and SSML), the ampersand is a control character. When an XML parser encounters an unescaped &, it expects an entity reference to follow (e.g., &lt; for < or &gt; for >). If no valid reference follows, the parser throws an error because it cannot interpret the & as a regular character.

Example of Incorrect SSML

The following SSML snippet will cause an error in Google TTS:

<speak>
    Welcome to A & B Store!
</speak>

Since & is not followed by a valid entity, the SSML parser will reject it.

How to Fix the “&” Issue in SSML

The solution is simple: use XML entities. Replace & with &amp; to ensure the parser correctly interprets it as text.

Correct SSML Example

<speak>
    Welcome to A &amp; B Store!
</speak>

Now, the XML parser reads &amp; as a literal &, and the TTS engine correctly pronounces it.

Other XML Special Characters to Watch Out For

The ampersand isn’t the only special character in XML/SSML. Here are other characters that need encoding:

Character Meaning Correct Encoding
< Less than &lt;
> Greater than &gt;
" Double quote &quot;
' Single quote (apostrophe) &apos;
& Ampersand &amp;

If you use any of these characters without encoding them, your SSML may break.

Conclusion

The & symbol is a reserved character in XML-based SSML and cannot be used directly in Google Text-to-Speech API. The fix is straightforward: replace & with &amp; to avoid parsing errors. This small adjustment ensures your SSML script runs smoothly and produces the desired speech output.

By keeping this best practice in mind, you can avoid frustrating errors and make the most of Google’s powerful TTS features. Happy coding!