xsl-list
[Top] [All Lists]

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

2007-04-16 07:12:12
Yves Forkl wrote:
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:

<snip />

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.

That should not stop you from using the separator-attribute of xsl:value-of. Just use an extra variable, like this:

<xsl:variable name="children">
   <xsl:variable name="a-and-b" as="xs:string*">
       <xsl:text>a</xsl:text>
       <xsl:for-each select="('b', 'c')">
           <xsl:value-of select="."/>
       </xsl:for-each>
   </xsl:variable>
   <xsl:value-of select="$a-and-b" separator="|&#10;" />
</xsl:variable>


But that won't remove the entities from the result. Use a character-map to achieve that (note the changes to your XSLT):

<xsl:stylesheet
   xmlns:xs = "http://www.w3.org/2001/XMLSchema";
   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:output indent="yes" use-character-maps="literal-newlines" /> <xsl:character-map name="literal-newlines">
       <xsl:output-character character="&#xE0E1;" string="&#xA;"/>
   </xsl:character-map>

   <xsl:template match="/">
       <myxsl:stylesheet
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
           version="2.0">
           <xsl:variable name="children">
               <xsl:variable name="a-and-b" as="xs:string*">
                   <xsl:text>a</xsl:text>
                   <xsl:for-each select="('b', 'c')">
                       <xsl:sequence select="."/>
                   </xsl:for-each>
               </xsl:variable>
               <xsl:value-of select="$a-and-b" separator="|&#xE0E1;" />
           </xsl:variable>
           <myxsl:template match="/">
               <myxsl:apply-templates select="{$children}"/>
           </myxsl:template>
       </myxsl:stylesheet>
   </xsl:template>
</xsl:stylesheet>

This will produce the output the way you requested it.

HTH,

Cheers,
-- Abel Braaksma

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>