473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XSL: automatically wrapping text in <p>

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)
Jul 20 '05 #1
1 4448


Sam Quigley wrote:

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="."/>


Here you pass in the value of a <paraset> element to the named template
wrapinp meaning here you throw out all structure the <paraset> content
might have and reduce it to its string value.
If you want to process <emph> elements for instance you need to make
sure you do process text nodes and element nodes in <paraset>
recursively with e.g. <xsl:apply-templates />.
Of course you want to split text by newlines however that is hard with
XSLT if you have child elements as well, if possible try to fix the
original markup to use elements to indicate the paragraph structure and
not newlines.
I realize I have only explained what goes wrong in your current solution
without providing a better approach with XSLT, I would start looking at
http://www.dpawson.co.uk/xsl/sect2/sect21.html
for a solution.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Derek Fountain | last post by:
When is the use of <xsl:text>blah blah</xsl:text> necessary? I'm finding that, although it's used religously in the book I'm learning from, if I miss it out, the output is just the same.
6
by: John Kooistra | last post by:
Hi! I'm looking to generate an HTML table via XSL using XML data. Specifically, I want to arrange my data in a table with rows of 6 columns. So, I have this conditional XSL statement setup to...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
1
by: NicoAgenci | last post by:
Hello, I have a page in .net (DEFAULT.ASPX), i load a XmlDocument, and a XslTransform to Add a XsltArgumentList (with the param : urlPage), and send all to a <asp:xml> in the page. The...
0
by: Rick Walsh | last post by:
I am using the following code to export a dataset via xml and xsl to html: ------------------------------------------------------------- .. .. .. Dim dataDoc as new...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: Clodo | last post by:
I need to create a PHP page that use an XSL file for transforming a generic XSD in their XHTML with inputbox,select etc.., for allow users to insert data in a user-friendly html-based editor, and...
2
by: Paul Verbelen | last post by:
I have a file with topics. I like to copy them in another file but want to have some blank lines between the different topics. I use <xsl:text> element with as data some blank lines to perform...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
5
by: ajos | last post by:
hi frnds, this is the way i ve written--> <html:text name="bdgtmastForm" property="publicity_code" size="5" maxlength="5"> but its giving me an error which seems irrelevent.. type Exception...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.