xsl-list
[Top] [All Lists]

Re: [xsl] Removing unwanted space

2021-06-05 00:54:25
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
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>