[xsl] Putting a line feed into XML output (XSLT 2.0)

Subject: [xsl] Putting a line feed into XML output (XSLT 2.0)
From: Yves Forkl <Y.Forkl@xxxxxx>
Date: Mon, 16 Apr 2007 15:53:35 +0200
Using XSLT 2 with Saxon 8.8, I am trying to insert line feeds in the resulting (main) XML document, which happens to be an XSLT stylesheet.

This proved to be not as trivial as I thought it would be, due to the fact that the line feed characters are meant to increase the readability of a complex pattern which makes up the value of an XSLT attribute. (Additionally, the pattern is composed using a for-each loop and is held in a variable.)

My real stylesheet is rather complex, but I managed to derive from it a simplified minimal stylesheet that illustrates the problem I face:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:myxsl="http://www.srz.de/xmlns/yforkl/xslt/TransformAlias";
  exclude-result-prefixes="myxsl"
  version="2.0">
  <xsl:namespace-alias stylesheet-prefix="myxsl" result-prefix="xsl"/>
  <xsl:template match="/">
    <myxsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      version="2.0">
      <xsl:variable name="children">
        <xsl:text>a</xsl:text>
        <xsl:for-each select="('b', 'c')">
          <xsl:text>|&#10;</xsl:text>
          <xsl:value-of select="."/>
        </xsl:for-each>
      </xsl:variable>
      <myxsl:template match="/">
        <myxsl:apply-templates select="{$children}"/>
      </myxsl:template>
    </myxsl:stylesheet>
  </xsl:template>
</xsl:stylesheet>

Run on any well-formed XML input, the resulting output is:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"><xsl:template match="/">
<xsl:apply-templates select="a|&#xA;b|&#xA;c"/></xsl:template></xsl:stylesheet>


The desired output should of course look like this:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"><xsl:template match="/">
<xsl:apply-templates select="a|
b|
c"/></xsl:template></xsl:stylesheet>


Now my question is:

How can I manage to create a real line feed in the given situation, i.e. when the select attribute's value is taken from a variable, whose value results from walking through a sequence of strings?

I know about the separator attribute of xsl:value-of, but unfortunately, at this place I can not do without the xsl:for loop as it also cares for other things in my real stylesheet. For some reason, the variable must be declared before the myxsl:template starts, too. And don't bother about the ugly way of including the "a" child above, this is just for leaving out any technique of restricting the separator to between the items from this simplified example. Last, I am not really applying my stylesheet to a meaningless input file.

Yves

Current Thread