Name

normalize-space()

Removes extra whitespace from its argument string.

Syntax

[1.0] string normalize-space(string?)
[2.0] xs:string normalize-space(xs:string?)

Inputs

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

[2.0] If the argument is the empty sequence, normalize-space() returns a zero-length string.

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.

If the string is all whitespace, a zero-length string is returned.

[2.0] In XSLT 2.0, passing the empty sequence to normalize-space() returns the empty sequence.

Defined in

[1.0] XPath section 4.2, “String Functions.”

[2.0] XQuery 1.0 and XPath 2.0 Functions and Operators section 7.4, “Functions on String Values.”

Example

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

<?xml version="1.0"?>
<!-- normalize-space1.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text>&#xA;</xsl:text> </xsl:variable> <xsl:variable name="testString"> <xsl:text> This is a string that had &#x9;&#x9; lots of &#xA;&#xA;&#xA; whitespace. </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:text>&#xA;Tests of the normalize-space() function:</xsl:text> <xsl:text>&#xA;&#xA; normalize-space(' </xsl:text> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.