Pages

Thursday, January 24, 2013

Remove 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.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.