xsl-list
[Top] [All Lists]

Re: Count leading spaces using xpath expression

2002-12-03 23:30:23
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




--- Dimitre Novatchev <dnovatchev(_at_)yahoo(_dot_)com> wrote:

--- Grainne Reilly  wrote:
 
Hi,
I am looking for a way to count the number of leading spaces in the
text of 
an element.  I have written the following template which does this:

    <xsl:template name="countIndent">
            <xsl:param name="str" />
            <xsl:param name="leadingSpaces" select="0"/>
            <xsl:choose>
                    <xsl:when test="(string-length($str) != 0) and 
(normalize-space(substring($str, 1, 1)) = '' )" >
                                    <xsl:call-template name="countIndent">
                                    <xsl:with-param name="str" 
select="substring($str, 2)" />
                                    <xsl:with-param name="leadingSpaces" 
select="$leadingSpaces +
1"/>
                            </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:value-of select="$leadingSpaces" />
                    </xsl:otherwise>
            </xsl:choose>
    </xsl:template>

However, I'd like to get this information as an xpath expression
(so
I
can 
use it in a predicate e.g. /myElement[(indent expression) > 3]). Is
this 
possible (it would only have to work for spaces, not all
whitespace)?
Thanks for any advice.
Grainne.

Hi Graine,

Use:

string-length(
              substring-before(translate($x, 
                                         translate($x, ' ', ''), 
                                         'Z'
                                          ), 
                               'Z'
                               )
              )



This is the double translate technique used by Mile Kay sometimes
ago.

The inner translate() gets a string of all non-space characters in
$x.
The outer translate() replaces the first non-space character in $x
with
'Z'.

Then we have a string which is the same as the initial one, but the
first non-space character now is "Z'.

The rest is easy -- the group of leading spaces is immediately before
this 'Z', so we can get it using substring-before().

Then string-length() gives us the length of the group of leading
spaces.

So, if we have defined $x to contain '    xyz a b c  ' (four leading
spaces and three trailing), the above expression is evaluated to:

4


Hope this helped.



=====
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



=====
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>