Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2008, 02:03 PM
Authorized User
 
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split a string by 80 chars, but not during a word

Hi,

I have a string value, that is sometimes too long to display.

After about 80(ish) characters, I want to stop displaying it, but I don't want to stop splitting a word in the middle. It needs to look back to the first space to prevent that.

A space will define the end of a word.

Note: If a word is more than 80 characters, than the word should be split.

Any help will be much appreciated!

An example input is the following:
The sky is blue. The rose is red. The carpet is orangepurple.

The expected output is the following (if # of chars to split is 10):
The sky is
 blue. The
 rose is
red. The
carpet is
orangepurp
le.


I like my input parameters to be a string and the # of chars to split. Basically, 2 input parameters.
 
Old December 4th, 2008, 02:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT 1.0 or 2.0? As always, it's going to be easier with 2.0, but a 1.0 solution isn't impossible.

Also you haven't said what the output is. Let's suppose you want to output a <br/> at the end of a line (putting a <p>...</p> around each line is probably a little bit harder, though when you've done one you should be able to do the other.

In principle you can do it like this:

* Call normalize-space() on the input so there are no multiple internal spaces.

* define a function or template "wordwrap" that takes three arguments: the text: the desired line-length, and the number of characters output on the current line so far.

* in this function, find the first space in the text (using string-length(substring-before())). If this plus the length-output-so-far exceeds the desired line length, output a <br/>, and call yourself recursively with length-output-so-far reset to zero. But if the first space position is greater than the desired line length and the length-so-far is zero, force a break after the desired-line-length number of characters. In all other cases, output the characters up to the first space and call yourself recursively, with substring($in, first-space-position + 1) as the text to be processed, and $length-so-far + first-space-position + 1 as the length-so-far.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 4th, 2008, 02:39 PM
Authorized User
 
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

XSLT 1.0

 
Old December 4th, 2008, 02:43 PM
Authorized User
 
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The expected output should be the following:
The sky is<br/>
 blue. The<br/>
 rose is <br/>
red. The <br/>
carpet is <br/>
orangepurp<br/>
le.<br/>


 
Old December 5th, 2008, 04:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think the purpose of Michael's post was that he gave you the methodology behind the code, and you should try it yourself and if you have further problems come back to us - we are not a free code writing service you know.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to split string krevathi1912 Access VBA 10 September 6th, 2007 02:21 AM
How to split this string chanduboorla XSLT 1 April 13th, 2006 04:15 AM
string split vidhya XSLT 1 August 10th, 2005 04:53 AM
Want to Strip Chars from a String - Not Working WebDevel Javascript How-To 2 January 10th, 2004 01:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.