Re: [xsl] insert tags out of context in XSL

Subject: Re: [xsl] insert tags out of context in XSL
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Fri, 3 May 2002 11:58:47 -0400
[Peter Carlson]
> I am trying to break up a single block of text into multiple blocks at the
> linefeed marker.
> I found the l2br solution in the FAQ, but I don't want to put a <br />, I
> want to use </para><para>.

Do not think like this.  XSLT is intended to produce trees, not text
fragments.  Try to think in terms of tree fragments, not string fragments.
> Since these tags are out of context in the xsl, it won't work since it's
> valid XML. I tried using the &lt;para&gt;, but they are considered tags in
> the next transform.

Thinking in tree - or well-formed element - terms, you really want to break
up the text into blocks, each of which is contained in its own "para"
element.  The break marker is a line feed, which will be a &#10; character.
You can break up the blocks using the substring-before and substring-after

You process all the blocks,  not by inserting a text fragment when you get
to a marker, but by sending each successive part of the text to the template
that knows how to do the job.  Substring-before lets you get the first
block, which you process, and substring-after gets you the rest of the text,
which you recursively send to the same template until there are no more
blocks to separate out.

No doubt Dimitre has an FXSL functional programming function that will do it

Here's a stylesheet fragment that does the job:

<xsl:template match="/root">
 <xsl:call-template name='split-into-paras'>
  <xsl:with-param name='text' select='para'/>

<xsl:template name='split-into-paras'>
 <xsl:param name='text'/>
 <xsl:variable name='first' select='substring-before($text,"&#10;")'/>
 <xsl:variable name='rest' select='substring-after($text,"&#10;")'/>
 <xsl:if test='$first!=""'>
    <para><xsl:value-of select='$first'/></para>

 <xsl:if test='$rest!=""'>
  <xsl:call-template name='split-into-paras'>
   <xsl:with-param name='text' select='$rest'/>


Tom P

 XSL-List info and archive:

Current Thread