Subject: [xsl] Re: Count leading spaces using xpath expression From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Tue, 3 Dec 2002 21:50:46 -0800 (PST) |
--- 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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] forgetting SGML (was Re: Newb, Mike Brown | Thread | Re: [xsl] Re: Count leading spaces , Grainne Reilly |
RE: [xsl] Count leading spaces usin, Jeff Beadle | Date | Re: [xsl] Re: Count leading spaces , Grainne Reilly |
Month |