normalize-space() Function  
Removes extra whitespace from its argument string.
 
Inputs

An optional string. If the argument is omitted, the normalize-space() function uses the string value of the context node.

 
Output

The argument string, with whitespace removed as follows:

    All leading whitespace is removed.

    All trailing whitespace is removed.

    Within the string, any sequence of whitespace characters is replaced with a single space.

 
Defined in

XPath section 4.2, String Functions.

 
Example

Here is a short example that demonstrates how normalize-space() works:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>


  <xsl:variable name="testString">
    <xsl:text>                 This
is


a string
with lots of


whitespace.

</xsl:text>
    </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Tests of the normalize-space() function:</xsl:text>

    <xsl:value-of select="$newline"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   normalize-space('       Hello,            World!')="</xsl:text>
    <xsl:value-of select="normalize-space('       Hello,            World!')"/>
    <xsl:text>"</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:text>   normalize-space($newline)="</xsl:text>
    <xsl:value-of select="normalize-space($newline)"/>
    <xsl:text>"</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:text>   normalize-space($testString)="</xsl:text>
    <xsl:value-of select="normalize-space($testString)"/>
    <xsl:text>"</xsl:text>
    <xsl:value-of select="$newline"/>
  </xsl:template>

</xsl:stylesheet>

The stylesheet generates this output:


Tests of the normalize-space() function:

   normalize-space('       Hello,            World!')="Hello, World!"
   normalize-space($newline)="
   normalize-space($testString)="This is a string with lots of whitespace."