Forum Moderators: open

Message Too Old, No Replies

Preserving paragraphs with XSL?

How do you render paragraphs in XML?

         

Barbacena

12:28 pm on May 26, 2005 (gmt 0)

10+ Year Member



Hi all!

I was wondering if anyone here knew how to format paragraphs in XML. I tried to use <xsl:text> but it doesn't seem to work. Is there a way of optimising it so the document already outputs with paragraphs?

Example:

<my_text>
This is some text that should contain paragraphs as well as your usual spaces.

Here is a new paragraph.

Another paragraph here.</my_text>

What do I have to do in my XSL document to render "my_text" with all it's paragraphs? I was thinking about using a pre tag, but it's not really the solution as it makes it the "same" as source code text, which isn't what I'm looking for.

There must be a way, could anyone help me on this one please?

Regards.

Barbacena

johnl

7:58 am on May 30, 2005 (gmt 0)

10+ Year Member



hi,

assuming that you consider a paragraph as some text following one or more linefeeds,
and assuming that you want to output html,
you could do


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />

<xsl:template match="/">
<html><head><title>LF to p</title></head>
<body>
<xsl:variable name="inStr" select="."></xsl:variable>
<xsl:variable name="outStr">
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr" select="$inStr"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$outStr"/>
</body>
</html>
</xsl:template>

<xsl:template name="lf2p">
<xsl:param name="ckStr"/>
<xsl:choose>
<xsl:when test="contains($ckStr,'&#x0a;')">
<xsl:value-of select="substring-before($ckStr,'&#x0a;')"/>
<p/>
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr">
<xsl:value-of select="substring-after($ckStr,'&#x0a;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$ckStr"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

(although there might be easier ways..)

Barbacena

2:34 pm on Jun 4, 2005 (gmt 0)

10+ Year Member



Thank you for your reply. It works great but I was wondering if it was possible to render it so that I don't have to build all the HTML around the template and only the p tags are implemented?

Regards

johnl

9:02 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Hi,

just change the output method to xml and leave the html-code out. However, if you want to exclude empty lines (which will give <p/> as output) you will have to adapt the code. Also, take care: as this method works with recursion, you may not be able to have big input files.
anyway here is an (untested) example:



<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />

<xsl:template match="/">
<myoutput>
<xsl:variable name="inStr" select="."></xsl:variable>
<xsl:variable name="outStr">
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr" select="$inStr"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$outStr"/>
</myoutput>
</xsl:template>

<xsl:template name="lf2p">
<xsl:param name="ckStr"/>
<xsl:choose>
<xsl:when test="contains($ckStr,'&#x0a;')">
<p>
<xsl:value-of select="substring-before($ckStr,'&#x0a;')"/>
</p>
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr">
<xsl:value-of select="substring-after($ckStr,'&#x0a;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:value-of select="$ckStr"/>
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>