Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XSL: automatically wrapping text in

From: sq@-------.--- (--- -------)
To: NULL
Date: 7/1/2004 8:06:00 AM
Hi,

I want to use XSL to wrap paragraphs of text in <p> tags
automatically, basically using the heuristic that two chunks of text
separated by 2 consecutive newlines are to be treated as two <p>s... 
I do this right now by putting the input xml (the text to be wrapped)
in a <paraset> tag, and using the following XSL:

<xsl:template match="paraset">
  <xsl:call-template name="wrapinp">
    <xsl:with-param name="input">
      <xsl:value-of select="."/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template name="wrapinp">
  <xsl:param name="input"/>
  <xsl:choose>
    <!-- if the input string contains two consecutive newlines -->
    <xsl:when test="contains($input,'&#xA;&#xA;')">
      <!-- wrap first part in a <p> -->
      <p>
 	<xsl:value-of select="substring-before($input,'&#xA;&#xA;')"/>
      </p>
      <!-- recurse into second part to find further paras -->
      <xsl:call-template name="wrapinp">
	<xsl:with-param name="input">
	  <xsl:value-of select="substring-after($input,'&#xA;&#xA;')"/>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <!-- string does not contain consecutive newlines, so just wrap it
-->
    <xsl:otherwise>
      <p>
 	<xsl:value-of select="$input"/> 
      </p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

the thing works nicely, except for one hitch: tags within the
paragraphs don't get interpreted.  so, if my input looks like

<paraset>
blah blah <emph>HEY</emph> blah

foo foo <emph>HO</emph> foo
</paraset>

I get as output

<p>blah blah HEY blah</p>
<p>foo foo HO foo</p>

despite an XSL template that turns <emph>s into <i>s:

<xsl:template name="emphasis" match="emph">
  <i>
    <xsl:apply-templates />
  </i>
</xsl:template>

What am I doing wrong?  I'm relatively new to XSL, so perhaps I'm just
getting my apply-templates and value-ofs confused -- but any help
would be appreciated...

Also, if there's some magic (not involving XSLT 2.0) that would allow
me to loosen the paragraph heuristic to allow optional whitespace
between newlines, i'd love to hear about it.

Thanks,
-sq
(also: please reply directly via email if possible)


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent