XSLT to left/right trim text

5,490 views
Skip to first unread message

hanasaki

unread,
Jan 29, 2006, 12:34:17 PM1/29/06
to kcj...@googlegroups.com
Anyone have a small code snip (src xslt and how to apply it to a doc)
that will left and right trim a text field?

" some value "
transformed to
"some value"

gotta love padded CHAR(25) in the DB :(

Manfred Riem

unread,
Jan 29, 2006, 12:38:07 PM1/29/06
to KCJ...@googlegroups.com
Hi there,

Use normalize ;)

Manfred

hanasaki

unread,
Jan 29, 2006, 12:42:54 PM1/29/06
to KCJ...@googlegroups.com
http://www.zvon.org/xxl/XSLTreference/Output/function_normalize-space.html

XPath function: string normalize-space (string?)

Standard excerpt:
The normalize-space function returns the argument string with whitespace
normalized by stripping leading and trailing whitespace and replacing
sequences of whitespace characters by a single space. Whitespace
characters are the same as those allowed by the S production in XML. If
the argument is omitted, it defaults to the context node converted to a
string, in other words the string-value of the context node.

**** notice it modifies spaces IN THE STRING also :P

I am no XSLT guru... Is there a "normalize" vs "normalize-space" that
you were referring to?

Manfred Riem

unread,
Jan 29, 2006, 12:52:36 PM1/29/06
to KCJ...@googlegroups.com
Hi,

You can use this one too. Normalize would do it for the entire XML document,
while the function expects an input

Doug

unread,
Jan 30, 2006, 9:54:32 AM1/30/06
to KCJava
Mr. Saki.

XSLT doesn't have the trim() or the replace() functions which drives me
crazy. normalize-space() is useless. You have to do recursive calls for
now until XPath 2.0 gives us more options.

Here's a solution for you:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="data" />
</xsl:copy>
</xsl:template>

<xsl:template match="data">
<xsl:copy>
<xsl:for-each select="datum">
<xsl:copy>
<xsl:call-template name="trim">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>

<xsl:template name="trim">
<xsl:param name="input"/>
<xsl:choose>
<xsl:when test="starts-with($input,' ')">
<xsl:call-template name="trim">
<xsl:with-param name="input" select="substring-after($input,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="substring($input, string-length($input) ) = ' ' ">
<xsl:call-template name="trim">
<xsl:with-param name="input" select="substring($input, 1,
string-length($input)-1)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


and the test data:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="trim.xslt" ?>
<data>
<datum> x </datum>
<datum> xx </datum>
<datum> x x </datum>
<datum> x x </datum>
<datum> x x x x </datum>
</data>


--Doug Hoff

hanasaki

unread,
Feb 5, 2006, 9:18:31 AM2/5/06
to KCJ...@googlegroups.com
Sometimes the simplest solutions are the best...

we ended up skipping transforms on each field and just traversed the
entire DOM, in memory, with node.setValue(node.getValue().trim())

* everything is not a Nail ;)

Xanian

unread,
Feb 10, 2006, 1:00:49 PM2/10/06
to KCJava
Not to state obvious, but doesn't the database you're using support a
trim() call in the SQL? I suppose if it is XML based you can stil use
the XPath trim or the XQuery trim function...

Xanian

hanasaki

unread,
Feb 10, 2006, 9:47:39 PM2/10/06
to KCJ...@googlegroups.com
actually... nope.. trim is not supported.. it is not even SQL or a rdbms
on the back end.

The simple solution ended up Very Simple... Walk the tree and call
setValue(getValue().trim)

Nathan Beyer

unread,
Feb 13, 2006, 12:43:42 PM2/13/06
to KCJ...@googlegroups.com
Do you have to navigate the DOM tree anyway, for some other processing reason that is? Otherwise, you may want to be careful with this solution, as it could quickly get expensive if the size of the DOM isn't fixed or guaranteed to be relatively small. If you have the choice, this might be a good case for using a SAX parser, which would allow you to ignore the whitespace via the "characters" handler method before even creating the end result.

--
-Nathan

new...@att.net

unread,
Feb 13, 2006, 3:50:35 PM2/13/06
to KCJ...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages