views:

28

answers:

3

Is there a simple way to strip padding, IE leading and/or trailing white space. EXSLT padding function seems to only create padding or trim strings to a certain length.

+1  A: 

Would normalize-space() do the job for you? This will also reduce spaces inside the string, e.g.

"  this         string    " 

would become:

"this string"

If you really need a "trim" function, you can probably steal one from someone else who's already implemented it with normalizse-space()...

Matt Gibson
Or one could use the replace function (http://www.w3.org/TR/xpath-functions/#func-replace). It should work more or less (i.e. it's untested) like this: `replace(string, '^\s*(.*?)\s*$', '')`.
musiKk
A: 

Try normalize-space

<xsl:value-of select='normalize-space(string)'/>
nonnb
A: 

It isn't clear what is wanted -- what is "strip padding"?

If you meant the trim() function, then

The FXSL library provides a convenient trim template function.

This transformation:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:import href="trim.xsl"/>

  <xsl:output method="text"/>
  <xsl:template match="/">
    '<xsl:call-template name="trim">
        <xsl:with-param name="pStr" select="string(/*)"/>
    </xsl:call-template>'
  </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<someText>

   This is    some text   

</someText>

produces the wanted, correct result:

'This is    some text'
Dimitre Novatchev