New Horizons Get Schooled Blog

Remove unwanted whitespace with XSLT

Use XSLT with XMLRemove unwanted whitespace from your result text document with XSLT

When transforming XML data into a text document, often unwanted whitespace nodes (e.g., extra line breaks) appear in the output. You can control this by using the <xsl:strip-space> element.

The <xsl:strip-space> element is a top-level element, and must follow immediately after the <xsl:stylesheet> start tag:

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

<xsl:strip-space elements="*"/>

Selecting "*" will strip extra whitespace from all elements. You can limit this to specific elements by including the element names, separated by spaces like so:

<xsl:strip-space elements="para section topic"/>

Note that <xsl:strip-space> doesn't strip extra space within text nodes (e.g., textual content in a tag), unless there is no other textual content in the tag.

Check out our XSLT and XML training!

Posted on 1/23/11 10:00 AM by Get Schooled in Whitespace, in XML, in XSLT, in Remove whitespace, in Text Document, in Tips & Tricks

Get Schooled

Written by Get Schooled

Search by Topic

    Lists by Topic

    see all

    Be at the center of it all.

    SUBSCRIBE TO NEWSLETTER

    Knowledge Ninjas

    a web-based service that allows software application end users to access a subject matter expert in real-time via a chat interface to resolve questions or problems they encounter in their specific software.