[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Removing unwanted space

Subject: Re: Removing unwanted space
From: "Joel Kalvesmaki director@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Jun 2021 05:54:11 -0000
Re:  Removing unwanted space
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.