0
votes

I need to know the character that precedes a certain element using XSLT 1.0.

Example XML:

<p>Das <hi>Konzert</hi> brachte die „<hi>Sinfonie</hi>“ von Gassmann-<hi>Kröger</hi>.</p>

I need to examine all elements in this document and would expect my XSLT to return " ", "„" and "." as preceding characters, and " ", "“" and "-" as following characters.

I tried this:

<xsl:template match="hi">
    <xsl:text>[</xsl:text>
    <xsl:value-of select="substring(preceding-sibling::text(),string-length(preceding-sibling::text()))"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="substring(following-sibling::text(),1,1)"/>
    <xsl:text>]</xsl:text>
</xsl:template>

This works fine for the following character, but not for the preceding character. It always returns " ", which always seems to be, in this case, the final character of "Das ". What can I do?

1
What if the immediately preceding sibling node is not a text node, but say another element?michael.hor257k
For the moment, I assume that there is always at least one whitespace between elements.friedemann_bach

1 Answers

1
votes

The obvious mistake you are making here is applying the string function to preceding-sibling::text() instead of preceding-sibling::text()[1] - which would isolate the immediately preceding text node.