Forum Moderators: open
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> Here is a new paragraph. Another paragraph here.</my_text>
This is some text that should contain paragraphs as well as your usual spaces.
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
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,'
')">
<xsl:value-of select="substring-before($ckStr,'
')"/>
<p/>
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr">
<xsl:value-of select="substring-after($ckStr,'
')"/>
</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..)
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,'
')">
<p>
<xsl:value-of select="substring-before($ckStr,'
')"/>
</p>
<xsl:call-template name="lf2p">
<xsl:with-param name="ckStr">
<xsl:value-of select="substring-after($ckStr,'
')"/>
</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>