[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Count leading spaces using xpath expression

Subject: RE: Count leading spaces using xpath expression
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Wed, 4 Dec 2002 09:46:11 -0000
count leading spaces
First translate the first non-space to "x":

translate($str, translate($str, ' ', ''), 'x')

then it's easy:

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

If you need to handle strings containing all spaces, you may need to
concat() an 'x' at the end before you start.

Not tested.

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Grainne Reilly
> Sent: 04 December 2002 03:58
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.