1

Given this example XML file:

<doc>
<tag>

    Hello !

</tag>
<tag>
    My
    name
    is
    John
</tag>
</doc>

And the following XSLT sheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> 
    <xsl:for-each select="doc/tag">
        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

How should I change it in order to ignore line feeds and convert any group of white-space characters to just one space in the items? In other words, I would like to obtain:

Hello!
My name is John

Without all those those silly line feeds. ...the question is how.

Thanks in advance !

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
dagnelies
  • 5,203
  • 5
  • 38
  • 56

2 Answers2

3
<xsl:template match="tag">
  ...
  <xsl:value-of select="normalize-space(.)" />
  ...
</xsl:template>

In fact, there is a good post about it.

Community
  • 1
  • 1
St.Woland
  • 5,357
  • 30
  • 30
2

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. If the argument is omitted, it defaults to the context node converted to a string, in other words the string-value of the context node.

<xsl:value-of select="normalize-space()"/>
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77