Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trim space in XSLT without replacing repating whitespaces by single ones?

Tags:

xml

xslt

xpath

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"
like image 649
Mathias Bader Avatar asked Dec 20 '12 14:12

Mathias Bader


People also ask

What is substring in XSLT?

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.

How do you break a line in XSLT?

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 &#10; where you want a new line.

How do you escape a single quote in XSLT?

You can use the built-in entities &apos; and &quot; 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).


1 Answers

A solution using just xslt 1.0 templates:

<xsl:variable name="whitespace" select="'&#09;&#10;&#13; '" />

<!-- 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 &gt; 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) &gt; 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="'   &#10;   test  '" />
    </xsl:call-template>
</ltrim>
<rtrim>
    <xsl:call-template name="string-rtrim">
        <xsl:with-param name="string" select="'    &#10;    test  &#10;  '" />
    </xsl:call-template>
</rtrim>
<trim>
    <xsl:call-template name="string-trim">
        <xsl:with-param name="string" select="'    &#10;    test  &#10;  '" />
    </xsl:call-template>
</trim>

Output:

<test>
    <ltrim>test  </ltrim>
    <rtrim>   
    test</rtrim>
    <trim>test</trim>
</test>
like image 197
Jörn Horstmann Avatar answered Oct 26 '22 10:10

Jörn Horstmann