Preserving attribute whitespace Preserving attribute whitespace xml xml

Preserving attribute whitespace


This is actually a raw XML parsing problem, not something XSLT can help you with. An XML parse must convert the newlines in that attribute value to spaces, as per ‘3.3.3 Attribute-Value Normalization’ in the XML standard. So anything currently reading your description attributes and keeping the newlines in is doing it wrong.

You may be able to recover the newlines by pre-processing the XML to escape the newlines to & #10; character references, as long as you haven't also got newlines where charrefs are disallowed, such as inside tag bodies. Charrefs should survive as control characters through to the attribute value, where you can then turn them into text nodes.


According to the Annotated XML Spec, white space in attribute values are normalized by the XML processor (See the (T) annotation on 3.3.3). So, it looks like the answer is probably no.


As others have pointed out, the XML spec doesn't allow for the preservation of spaces in attributes. In fact, this is one of the few differentiators between what you can do with attributes and elements (the other main one being that elements can contain other tags while attributes cannot).

You will have to process the file outside of XML first in order to preserve the spaces.