DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jason has posted 22 posts at DZone. View Full User Profile

Normalize Space And Keep Line Breaks In XSTL

05.16.2007
| 6223 views |
  • submit to reddit
        XSLT function pattern to normalize space but keep line breaks, without using a template.

It replaces both line break characters with non-whitespace placeholders, normalizes, then puts the line breaks back in, nothing fancy. Note that it will handle all four types of line breaks (LF, CR, CRLF, LFCR) even though only LF is XML standard. In our case, the placeholders are characters 160 and 173, the hard space and soft hyphen respectively. They're used, but generally only by programmers and Office documents.

For reference, pretend "FieldValue/." is the path to a <FieldValue>foo</FieldValue> element.

translate(
 normalize-space(
  translate(FieldValue/., '

', ' ­')
 ), ' ­Â­', '

'
)