Q: Read ahead and preprocess a node A sample xml document I have to process might look like this
<?xml version="1.0" encoding="UTF-8"?>
<div>
    <p>
        This is a text with some<damage>wh</damage>at damaged text
    </p>
</div>

Basically what I need to do is, to create HTML text, where the "damaged" part is indicated with a span and an additional footnote is given. 
The TRICKY part is however, that the footnote index should appear at the end of the word.
Currently I have the following XSLT


<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="p">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="damage">
    <xsl:variable name="alphaNumber">
        <xsl:number level="any" count="damage"/>
    </xsl:variable>
    <span class="{name()}" title="damage in paper (2 chars)">
        <xsl:apply-templates/>
    </span>
    <sup>
        <xsl:value-of select="$alphaNumber"/>
    </sup>
</xsl:template>


The output generated from this XSLT is
<p>
    This is a text with some<span class="damage" title="damage in paper (2 chars)">wh</span><sup>1</sup>at damaged text
</p>

My current solution is to use a variable that contains the node that follows and then to test, if it does not start with a space. The modified part of the XSLT looks like
<xsl:template match="damage">
    <xsl:variable name="nextNode"><xsl:copy-of select="following::node()[1]"></xsl:copy-of> </xsl:variable>
    <xsl:variable name="alphaNumber">
        <xsl:number level="any" count="damage"/>
    </xsl:variable>

    <span class="{name()}" title="damage in paper (2 chars)">
        <xsl:apply-templates/>
    </span>
    <xsl:choose>
        <xsl:when test="not($nextNode/*) and not(starts-with($nextNode, ' '))">
            <xsl:value-of select="substring-before(normalize-space($nextNode),' ')"/>
        </xsl:when>
    </xsl:choose>
    <sup>
        <xsl:value-of select="$alphaNumber"/>
    </sup>
</xsl:template>

With that I can test if the index currently appears in the middle of a word and that I have to actually add the content of the varialble to the output, before I add the  tags with the index.
But this solution leads to a duplicate fragment of the node and I end up with:
<?xml version="1.0" encoding="UTF-8"?>
<p>
    This is a text with some<span class="damage" title="damage in paper (2 chars)">wh</span>at<sup>1</sup>at damaged text
</p>

My question is how I can prevent the duplicaton of the "at" text part using xslt. In the final solution the footnote indexes are not only limited to that one  tag, but can occur in a variety of places. So this is just a sample to indicate the problem in an easy manner.
Any help is appreciated.

A: Is that XSLT 2.0 that you use? You have not shown how the text node following the damage element is output but assuming you use apply-templates you should be able to add a template with a specific match pattern e.g. 
<xsl:template match="text()[preceding-sibling::node()[1][self::damage]] and not(starts-with(., ' '))]">
  <xsl:value-of select="replace(., '^\w+', '')"/>
</xsl:template>

