Insert space between two elements
Posted: Mon Apr 09, 2018 2:13 pm
Hi,
I have a stylesheet that just sorts out white-space in a document.Where there's any sort of white-space at the start of end of a text node it inserts a single space regardless of how much white-space there was originally. My problem is that if I just have white-space between two elements say -
I loose the space between bob and harry.
The part that sorts out the leading and trailing space is this -
What can I do so that I retain the space between elements as per the above for nodes that also have text?
Many thanks
I have a stylesheet that just sorts out white-space in a document.Where there's any sort of white-space at the start of end of a text node it inserts a single space regardless of how much white-space there was originally. My problem is that if I just have white-space between two elements say -
Code: Select all
<root>
some text <abbrev>bob</abbrev> <emph>harry</emph>
</root>
The part that sorts out the leading and trailing space is this -
Code: Select all
<xsl:template match="*[../text()[normalize-space(.) != '']]" mode="strip-and-inject">
<!--but this template matches any element appearing in mixed
content-->
<xsl:variable name="textbefore" select="preceding-sibling::node()[1][self::text()]"/>
<xsl:variable name="textafter" select="following-sibling::node()[1][self::text()]"/>
<!--Either of the preceding variables will be an empty node set if the
neighbor node is not text(), right?-->
<xsl:variable name="prevchar" select="substring($textbefore, string-length($textbefore))"/>
<xsl:variable name="nextchar" select="substring($textafter, 1, 1)"/>
<!--Now the action:-->
<xsl:if test="$prevchar != normalize-space($prevchar)">
<!--If the original text had a space before, add one back-->
<xsl:text> </xsl:text>
</xsl:if>
<xsl:copy>
<!--Copy the element over-->
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
<xsl:if test="$nextchar != normalize-space($nextchar)">
<!--If the original text had a space after, add one back-->
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
Many thanks