xsl-list
[Top] [All Lists]

Trim() as a single XPath expression (Was: Re: Count leading spaces using xpath expression)

2002-12-04 00:41:12

--- Dimitre Novatchev <dnovatchev(_at_)yahoo(_dot_)com> 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



<Prev in Thread] Current Thread [Next in Thread>
  • Trim() as a single XPath expression (Was: Re: Count leading spaces using xpath expression), Dimitre Novatchev <=