Share via


normalize-space Function 

Returns the argument string with the leading, trailing, and repeating white spaces stripped.

string normalize-space(string)

Remarks

White space is normalized by stripping leading and trailing white space and replacing sequences of white space characters with a single space. If the argument is omitted, the string-value of the context node is normalized and returned.

The following function call returns "abc def":

normalize-space(" abc def ")

If an argument is not of the type string*,* it is first converted to a string and then evaluated. See the example below.

If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated.

Warning

String conversions for node sets that are passed as arguments to this function may yield unexpected results. For more information, see string Function.

This function is case-sensitive.

Example

The following example normalizes a block of text string with unnormalized white spaces (tabs, leading and trailing spaces, and multiple spaces between words. The text string is the value of an <text> element.

XML File (normSpace.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" 

href="normalizeSpace.xsl"?>      
<text>
    This is a
  test, with a   lot    of 
    irregular  spacing and
    waiting to be   normalizaed.


</text>

XSLT File (normSpace.xsl)

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

  <xsl:output method="xml"   
       omit-xml-declaration="yes"/>

<xsl:template match="/text">
Unnormalized: 
"<xsl:value-of select='.'/>"
Normalized: "<xsl:value-of select='normalize-space()'/>"
</xsl:template>

</xsl:stylesheet>

This XSLT produces the following output:

Unormalized:

"

This is a

test, with a lot of

irregular spacing and

waiting to be normalizaed.

"

Normalized:

"This is a test, with a lot of irregular spacing and waiting to be normalized."

See Also

Reference

XML Data Types Reference