[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Functional trim()

Subject: Re: Functional trim()
From: Adriano Rodrigues Ferreira <ferreira@xxxxxxxxxxxxx>
Date: Fri, 28 Dec 2001 12:30:31 -0200
perldoc trim
Dimitre Novatchev wrote:
>What is described above is quite simple -- we trim-left the string, then reverse it,
>then trim-left the reversed string (at this point all the trimming is done), then
>finally reverse the reversed and trimmed string.

This solution is immediately understable from the point of view of
someone used to functional programming. But it is cryptical from
the point of view of someone looking for a simple solution to
a simple problem (the XSL code inflates with respect to the Haskell
code and is quite intimidating).

Strings represented as lists, spaces eaten at the lead, reversed,
spaces eaten at the lead, reversed. You got be kidding to say this
is simple.

>>   Does anyone know if there is a function such as trim() that can be
>> applied on a text node, i.e., remove all leading and trailing white-space
>> ('\t', '\n', '\r', ' ', '\f'), given the text node can contain non
>> white-space characters.

The kind of solution that I think it would be natural for the original
statement of the problem would be something like the Perl function

sub trim {
    for (@_) {
        s/^\s*//; # trim leading spaces
        s/\s*$//; # trim trailing spaces
    }
    return @_;
}

The only lines that really matter here are
        s/^\s*//; # trim leading spaces
        s/\s*$//; # trim trailing spaces
and that means 
(1) match the start of the string ^, (2) scan leading spaces \s*, 
and (3) trim them. After this step of pattern match and substitution,
(4) match trailing spaces \s*, (5) match the end of the string $,
and (6) trim them. (This comes from 
	perldoc -q "strip blank space"
a reference pointed to me by John W. Khran.)

Now this solution was coded in a few lines
(being as small as the problem statement). With a pattern match library 
on XSL, maybe this could look like:

<xsl:template match="text()" mode="trim">
     <pattern-match:substitute match-string="^\s*" replace-string=""/>
     <pattern-match:substitute match-string="\s*$" replace-string=""/>
</xsl:template>

A few days ago someone has asked about pattern matching abilities in XSL,
which XSL 1.0 specification predicted for XSL 2.0. Michael Kay told him
to take a look at some documents on preparation. This is the kind of library functions 
which will give this kind of problem really simple solutions.

The toolkit available to XSL programmers would become quite powerful
with additions such as the pattern match library. And some questions on the 
XSL list would be answered with shorter pieces of codes. 

I really respect the wisdom of people who answer all kind of questions posted here
(like Dimitre) and just wanted to post my amateur's point of view.

Regards,

Adriano.





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.