The function normalize-space
replaces sequences of whitespaces by a single space and trims the provided string. How can I only trim the string without the replacement of whitespaces? There are proprietary solutions like orcl:left-trim
, but I am looking for a non-proprietary one.
Example:
<xsl:value-of select="trim(/Car/Description)"/>
should turn
<car>
<description> To get more information look at: www.example.com </description>
</car>
into
"To get more information look at: www.example.com"
A solution using just xslt 1.0 templates:
Test code:
Output:
A very short solution with XSLT1:
Using FXSL (open source library for XSLT functional programming, written entirely in XSLT) one simply writes:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
How does the
trim
template work?It trims the left leading whitespace, then it reverses the resulting string and trims its leading whitespace, then it finally reverses the resulting string.
II. XPath 2.0 solution:
Use:
Here is an XSLT - 2.0 - based verification:
When this transformation is applied on the provided XML document (above), the XPath expression is evaluated and the result of this evaluation is copied to the output:
normalize-space(actualSting) - This will do it.
If you don't have any spaces in the middle, you can simply use: