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

Trim() as a single XPath expression (Was: Re: Count le

Subject: Trim() as a single XPath expression (Was: Re: Count leading spaces using xpath expression)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 3 Dec 2002 23:41:12 -0800 (PST)
xpath trim
--- Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> What seems to be simpler:
> 
> string-length(substring-before($x, 
>                                substring(normalize-space($x), 1, 1)
>                                )
>               )
> 
> 
> =====
> Cheers,
> 
> Dimitre Novatchev.
> http://fxsl.sourceforge.net/ -- the home of FXSL

Thanks to Grainne Reilly, who raised the problem of getting the count
of leading blanks of a string, I've come to a single XPath expression
that returns a trimmed string.

If we have the following source xml:
-----------------------------------
<t>    xyz a b c  </t>


Four leading spaces and two trailing ones.

and this transformation:
-----------------------
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>
   
   <xsl:variable name="x" select="string(/)"/>
  <xsl:template match="/">
Original string: <xsl:text/>
      '<xsl:value-of select="$x"/>'
      
Leading blanks: <xsl:text/>
<xsl:value-of 
 select="string-length(substring-before($x, 
                                        substring(normalize-space($x),
                                                  1, 
                                                  1)
                                        )
                       )"/>
      
Trailing blanks: <xsl:text/>
<xsl:value-of 
 select="string-length(substring-after($x, 
                          substring(normalize-space($x),
                                    string-length(normalize-space($x)),
                                    1
                                    )
                                      )
                       )"/>
      
Trimmed: <xsl:text/>
'<xsl:value-of 
select="substring($x,
                  string-length(substring-before($x, 
                                substring(normalize-space($x), 1, 1)
                                                 )
                                ) + 1,
                  string-length($x)
                -
                  string-length(
                        substring-before($x, 
                                         substring(normalize-space($x),
                                                   1, 
                                                   1)
                                         )
                                )
                - 
                  string-length(
                        substring-after
                                   ($x, 
                                    substring(normalize-space($x),
                                              string-length(
                                                  normalize-space($x)
                                                            ), 
                                              1)
                                    )
                               )
                 )"/>'
      
      
  </xsl:template>
</xsl:stylesheet>

The result is:
-------------

Original string: 
      '    xyz a b c  '
      
Leading blanks: 4
      
Trailing blanks: 2
      
Trimmed: 
      'xyz a b c'






=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

 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.