xsl-list
[Top] [All Lists]

RE: Count leading spaces using xpath expression

2002-12-03 22:32:26
Hello Grainne,

Here's what I came up with:

 
string-length(substring-before(XPATH,substring(normalize-space(XPATH),1,1)))

where, XPATH is your the xpath to the desired (text) node.

However, keep in mind that this would report text nodes that had nothing but
white-spaces to be of length 0 ... but, now that I think about it, how can
there be the notion of "leading" white-space characters without the
existence of at least one non-white-space character?

I'm sure others will post more concise solutions, so hang-out for a bit ...
also, I'd take a gander at Dimitre Novatchev's FXSL ... that's a library of
some seriously wicked xsl utility and math templates--most of which (if not
all), I believe, are pure xslt implementations ... no script extensions.

Anyway, HTH ... Jeff


-----Original Message-----
From: Grainne Reilly [mailto:greilly1(_at_)attbi(_dot_)com]
Sent: Tuesday, December 03, 2002 10:58 PM
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Count leading spaces using xpath expression


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.



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list