views:

576

answers:

3

Disclaimer: the following is a sin against XML. That's why I'm trying to change it with XSLT :)

My XML currently looks like this:

<root>
    <object name="blarg" property1="shablarg" property2="werg".../>
    <object name="yetanotherobject" .../>
</root>

Yes, I'm putting all the textual data in attributes. I'm hoping XSLT can save me; I want to move toward something like this:

<root>
    <object>
        <name>blarg</name>
        <property1>shablarg</name>
        ...
    </object>
    <object>
        ...
    </object>
</root>

I've actually got all of this working so far, with the exception that my sins against XML have been more... exceptional. Some of the tags look like this:

<object description = "This is the first line

This is the third line.  That second line full of whitespace is meaningful"/>

I'm using xsltproc under linux, but it doesn't seem to have any options to preserve whitespace. I've attempted to use xsl:preserve-space and xml:space="preserve" to no avail. Every option I've found seems to apply to keeping whitespace within the elements themselves, but not the attributes. Every single time, the above gets changed to:

This is the first line This is the third line.  That second line full of whitespace is meaningful

So the question is, can I preserve the attribute whitespace?

+2  A: 

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.

James Sulak
+1  A: 

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.

bobince
I'm not sure this will work. Charrefs get replaced by the bytes they represent by the XML processor, and so a charref referring to a whitespace character (like LINE FEED) will be normalized as whitespace.
ChuckB
The standard and DOM Test Suite say it works; Your Implementation May Vary, but the ones I've tested do.
bobince
+2  A: 

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.

Ned Batchelder