[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Count leading spaces using xpath expression
--- 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|