Subject: [xsl] Trim() as a single XPath expression (Was: Re: Count leading spaces using xpath expression) From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Tue, 3 Dec 2002 23:41:12 -0800 (PST) |
--- Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote: > What seems to be simpler: > > string-length(substring-before($x, > substring(normalize-space($x), 1, 1) > ) > ) > > > ===== > Cheers, > > Dimitre Novatchev. > http://fxsl.sourceforge.net/ -- the home of FXSL Thanks to Grainne Reilly, who raised the problem of getting the count of leading blanks of a string, I've come to a single XPath expression that returns a trimmed string. If we have the following source xml: ----------------------------------- <t> xyz a b c </t> Four leading spaces and two trailing ones. and this transformation: ----------------------- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="x" select="string(/)"/> <xsl:template match="/"> Original string: <xsl:text/> '<xsl:value-of select="$x"/>' Leading blanks: <xsl:text/> <xsl:value-of select="string-length(substring-before($x, substring(normalize-space($x), 1, 1) ) )"/> Trailing blanks: <xsl:text/> <xsl:value-of select="string-length(substring-after($x, substring(normalize-space($x), string-length(normalize-space($x)), 1 ) ) )"/> Trimmed: <xsl:text/> '<xsl:value-of select="substring($x, string-length(substring-before($x, substring(normalize-space($x), 1, 1) ) ) + 1, string-length($x) - string-length( substring-before($x, substring(normalize-space($x), 1, 1) ) ) - string-length( substring-after ($x, substring(normalize-space($x), string-length( normalize-space($x) ), 1) ) ) )"/>' </xsl:template> </xsl:stylesheet> The result is: ------------- Original string: ' xyz a b c ' Leading blanks: 4 Trailing blanks: 2 Trimmed: 'xyz a b c' ===== 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] Re: Count leading spaces usin, Dimitre Novatchev | Thread | [xsl] nbsp (in parameter) question, Jitu |
Re: [xsl] Newbie encoding query, Mike Brown | Date | Re: [xsl] Reusable XSLT templates, Cams Ismael |
Month |