[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Beautiful! Thanks Dimitre and Jeff for your elegant solutions both of which do the trick nicely.
Grainne.
At 09:50 PM 12/3/2002 -0800, you wrote:
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Re: [xsl] Re: Count leading spaces using xpath expression
Subject: Re: [xsl] Re: Count leading spaces using xpath expression From: Grainne Reilly <greilly1@xxxxxxxxx> Date: Wed, 04 Dec 2002 01:30:15 -0500 |
Beautiful! Thanks Dimitre and Jeff for your elegant solutions both of which do the trick nicely.
Grainne.
At 09:50 PM 12/3/2002 -0800, you 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
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Re: Count leading spaces usin, Dimitre Novatchev | Thread | [xsl] Re: Count leading spaces usin, Dimitre Novatchev |
[xsl] Re: Count leading spaces usin, Dimitre Novatchev | Date | [xsl] Re: Count leading spaces usin, Dimitre Novatchev |
Month |
Keywords