XSLT: Remove unexpected end of lines

09-01-2005

This won't be a smart hint. I even feel a bit ashamed of spending time figuring this out... But here it is in case it helps anybody.

My issue was. I started with this XML element:

<title>Test Doc</title>

I processed it with this XSLT template:

<u><xsl:value-of select="title/text()"/></u>

And the result was this:

<u>Test Doc
</u>

The result is valid XML, but because I was using the <u> tag, the extra space will be displayed underlined, and this will loook ugly on a web browser. Won't it?

Where did that extra end of line character came from? That bugged me. As I reviewed the XML specs I believe that the text of the tag contains not only the data between tags, but also the end of line character if it follows the closing tag. (I wonder why this is this way).

Whatever, I just wanted this return character to disappear. For that, I just need the XPATH function normalize-space() (it removes extra spaces as well as end of line characters) :

<u><xsl:value-of select="normalize-space(title/text())"/></u>

And this was what I wanted:

<u>Test Doc</u>

comment feed - top


·La Rabadilla· ·Laconada· ·iRamos· ·O Vello Corvo· ·Palabros·
counter [CSS 2 Valid!] [XHTML 1.0 Valid!] [Made with Ant] [Get Firefox] RSS 1.0RSS 1.0 Atom 1.0RSS 1.0