Search

I have the following code in an html element:

<xsl:value-of select="/data/klant/entry/voornaam" /> <xsl:value-of select="/data/klant/entry/tussenvoegsel" /> <xsl:value-of select="/data/klant/entry/achternaam" />

However, the white space between is removed by XSLT. How can I prevent this? Note that sometimes the middle "value-of" returns an empty string, so I don't want to use &nbsp;

You could concatenate them together.

<xsl:value-of select="concat(voornaam, ' ', tussenvoegsel, ' ', achternaam)" />

Take @nick's advice, it's best.

But if you ever want to be sure your white spaces appear in any situation, output them with &#160; or <xsl:text> </xsl:text>

Great, thanks a lot! I did find something about whitespace-preserve but couldn't get it working. The concat solution does work though and it makes my xsl statements shorter as well :)

@frankel: the problem with Nick's solution is that you will now get 2 whitespaces if there is no tussenvoegsel, which is quite common in dutch names.

I would recommend that you check if tussenvoegsel is set, and change the value-of accordingly:

<xsl:choose>
    <xsl:when test="tussenvoegsel=''"><xsl:value-of select="concat(voornaam, ' ', achternaam)" /></xsl:when>
    <xsl:otherwise><xsl:value-of select="concat(voornaam, ' ', tussenvoegsel, ' ', achternaam)" /></xsl:otherwise>
</xsl:choose>

Although @remie is right, I wouldn't mind too much of it, since 2 (or more) spaces in your HTML code would always be rendered as one space. Unless you have the name in a <pre> or something...

I know... it's not strictly correct, but it keeps your XSL a bit tidier ;-)

Yep, I don't bother checking it because HTML won't care. But thanks for the suggestion :)

Or for purists it's cleaner and shorter to revert back from double spaces than using a conditional:

<xsl:value-of select="normalize-space(concat(voornaam, ' ', tussenvoegsel, ' ', achternaam))" />

That's exactly why everyone hates XSLT. Making simple things so ridiculously ugly and complicated.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details