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"
Definition of XSLT substring. XSLT is defined as a manipulation language for XML documents. In such a case there is a function for searching a string for re-arranging the documents. Substring is primarily used to return a section of a string or truncating a string with the help of the length provided.
Include the attribute Method="text" on the xsl:output tag and include newlines in your literal content in the XSL at the appropriate points. If you prefer to keep the source code of your XSL tidy use the entity where you want a new line.
You can use the built-in entities ' and " In XSLT 1.0: Alternatively, you can define your $Q and $APOS variables (put the content (the literal " or the literal ' character) in the body of the xsl:variable , not in the select attribute).
A solution using just xslt 1.0 templates:
<xsl:variable name="whitespace" select="'	 '" />
<!-- Strips trailing whitespace characters from 'string' -->
<xsl:template name="string-rtrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:variable name="length" select="string-length($string)" />
<xsl:if test="$length > 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, $length, 1))">
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="substring($string, 1, $length - 1)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading whitespace characters from 'string' -->
<xsl:template name="string-ltrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:if test="string-length($string) > 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, 1, 1))">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="substring($string, 2)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading and trailing whitespace characters from 'string' -->
<xsl:template name="string-trim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:template>
Test code:
<ltrim>
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</ltrim>
<rtrim>
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</rtrim>
<trim>
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</trim>
Output:
<test>
<ltrim>test </ltrim>
<rtrim>
test</rtrim>
<trim>test</trim>
</test>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With