Eliminate line-breaks with XSLT 2.0 analyze-string
===

I use the XSLT 2.0 element analyze-string in a stylesheet that transforms XML to HTML; specifically, I use it to convert string encoding for subscripts in chemical formulae to HTML subscripts. Therefore, the result is a string, to go in a p or td element, with embedded mark-up.

The transformation is supposed to produce output like H<sub>2</sub>O but in fact inserts a line-break in the HTML:

    H
    <sub>2</sub>O

and this break is (correctly) interpreted by the browser as a space:

H
<sub>2</sub>O

which is ugly.

Is there a way to remove the line-break? I've tried putting the whole analyze-string element on one line and that doesn't work. 

The input would be something like 

    <OrdinaryStructralFormula>H$_2$O</OrdinaryStructuralFormula>

for a simple case and 

    <OrdinaryStructralFormula>C$_2$OH$_5$$^-</OrdinaryStructuralFormula>

for a more-complicated one. Note that the subscript pattern can match multiple times in the general case and can be either in the middle or at the end of the string. The pattern also has to match and eliminate any notation for charge: the $^- bit at the end of the second example.

The XSLT processor is Saxon 9.4 and the XSLT template follows.

    <xsl:template name="formula">
      <xsl:param name="formula"/>
      <xsl:if test="$formula">
        <xsl:variable name="f" select="translate($formula, '$', '')"/>
        <xsl:analyze-string select="$f" regex="(_)(\d+)|(\^)\d*\+|(\^)\d*\-">
          <xsl:matching-substring>
            <xsl:if test="regex-group(1)='_'">
              <sub><xsl:value-of select="regex-group(2)"/></sub>
            </xsl:if>
          </xsl:matching-substring>
          <xsl:non-matching-substring>
            <xsl:value-of select="."/>
          </xsl:non-matching-substring>
        </xsl:analyze-string>
      </xsl:if>
    </xsl:template>