I. XSLT 2.0 solution:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:for-each select="tokenize(., '\n\r?')[.]">
<para><xsl:sequence select="."></xsl:sequence></para>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<programlisting>
public static void main(String[] args){
System.out.println("Be happy!");
System.out.println("And now we add annotations.");
}
</programlisting>
the wanted, correct result is produced:
<programlisting>
<para>public static void main(String[] args){</para>
<para> System.out.println("Be happy!");</para>
<para> System.out.println("And now we add annotations.");</para>
<para>}</para>
</programlisting>
II. XSLT 1.0 solution, using the str-split-to-words
template of FXSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="pDelims" select="'

'"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="$pDelims"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*[normalize-space()]"/>
</xsl:template>
<xsl:template match="word">
<para><xsl:value-of select="."/></para>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document (above), the same correct result is produced:
<para>public static void main(String[] args){</para>
<para> System.out.println("Be happy!");</para>
<para> System.out.println("And now we add annotations.");</para>
<para>}</para>