Re: [xsl] Removing unwanted space

Subject: Re: [xsl] Removing unwanted space
From: "Joel Kalvesmaki director@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Jun 2021 05:54:11 -0000
Hi Charles,

As I understand it, you want to normalize space, but exercise the option to specify if the initial or terminal space should be replaced by a single space or removed altogether. Here's how I'd handle it:

<xsl:template match="text()" priority="-1" mode="normalize-space">
<!-- By default, normalize initial and terminal space to a single space, but
space-normalize all the intervening text. -->
<xsl:if test="matches(., '^\s')">
<xsl:value-of select="' '"/>
</xsl:if>
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="matches(., '\S\s+$')">
<xsl:value-of select="' '"/>
</xsl:if>
</xsl:template>
<xsl:template match="p/text()" mode="normalize-space">
<!-- These tests permit comments and processing instructions to be inserted inside initial
or final indentations, without affecting the results. -->
<xsl:variable name="is-not-initial-indentation" as="xs:boolean" select="
some $i in (preceding-sibling::* | preceding-sibling::text())
satisfies matches($i, '\S')"/>
<xsl:variable name="is-not-final-indentation" as="xs:boolean" select="
some $i in (following-sibling::* | following-sibling::text())
satisfies matches($i, '\S')"/>


   <xsl:if test="$is-not-initial-indentation and matches(., '^\s')">
      <xsl:value-of select="' '"/>
   </xsl:if>
   <xsl:value-of select="normalize-space(.)"/>
   <xsl:if test="$is-not-final-indentation and matches(., '\S\s+$')">
      <xsl:value-of select="' '"/>
   </xsl:if>
</xsl:template>

Good luck!

jk

Joel, to answer your question (incompletely), given

<p>
    <anchor> </anchor>
    The rain in <bold> <underline> Spain </underline> </bold> <italic>
is </italic> wet.
</p>

I'd likely want

<p><anchor> </anchor> The rain in <bold> <underline> Spain
</underline> </bold> <italic> is </italic> wet.</p>



--
Joel Kalvesmaki
Director, Text Alignment Network
http://textalign.net

Current Thread