This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: insert tags out of context in XSL


[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
not
> 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
functions.

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
too.

Here's a stylesheet fragment that does the job:

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

<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>

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

Cheers,

Tom P




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]