normalize-space Function

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

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.

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

Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference