A picture of me taking it easy

I'm currently on parental leave till September. During that time, I will not read any e-mail or blog comments.

Until I'm back, please read through my archives, take a look at my code/applications and check out my pictures.

Have a great summer, and a splendid winter to you aussies and kiwis! :-)

Transforming text to paragraphs

Published on Thursday, July 21st, 2005

Recently, in a customer project, I came upon a situation where I would get a bulk of text delivered from the database through XML. Naturally, the customer wanted the text to be presented in neat paragraphs and the only way to differentiate one paragraph from another in the source text was line breaks.

I then created a JavaScript in the XSLT file to wrap every text entity separated by line breaks within p tags.

My solution can be found in RobLab’s Text to paragraphs article.

Posted in JavaScript | 4 comments

4 comments

  • Tom Greuter
    July 21st, 2005 at 13:07

    If the text in the database is allowed to contain HTML you can possibly run into validation trouble when this script surrounds blockquotes or unnumbered lists with paragraph tags. In that case you probably want to apply more filters.

  • Robert Nyman - author
    July 21st, 2005 at 14:17

    Tom,

    Interesting that you bring that up!

    In my case, the text contained HTML as well, which they wanted removed.

    So I ran a regular expression script to remove all tags first; basically, like this:
    strNodeText = strNodeText.replace(/<\/?[^>]+(>|$)/g, "");

    However, if the text also contains tags that should be left there, this solution isn’t optimal. This script is only meant for plain text.

  • Devon
    July 21st, 2005 at 20:48

    There’s two things I would love to see done in XSLT -

    1. chat log to XML.
    2. e-mail (from say, my thunderbird client) to XML.

    If I had the money I’d pay someone to do it. That’s how much I want these. But, I just lost my job recently.

  • Robert Nyman - author
    July 21st, 2005 at 21:00

    Devon,

    First, sorry to hear about your job. I hope things work out.

    Regarding chat logs: If you have Message History enabled in MSN Messenger, it writes an XML file per contact with your conversations.

    When it comes to the e-mailing thing, I have no concrete suggestion.

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview

All the proceeds from ad clicks will go to charity. However, if you like to give something directly to charity yourself, I recommend choosing from the listed ones below.

  • Save the Children
  • Red Cross
  • Cancer Research UK
  • WWF

Top results