summaryrefslogtreecommitdiff
path: root/doc/lib/trim.text.html
blob: ee1dc069de04a1f8bef9c2f6cb01a0583ff84025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>trim.text</title><link rel="stylesheet" type="text/css" href="../reference.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><link rel="home" href="../index.html" title="DocBook XSL Stylesheets: Reference Documentation"><link rel="up" href="generallibrary.html" title="General Library Templates"><link rel="prev" href="comment-escape-string.recursive.html" title="comment-escape-string.recursive"><link rel="next" href="str.tokenize.keep.delimiters.html" title="str.tokenize.keep.delimiters"><link rel="copyright" href="copyright.html" title="License"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">trim.text</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="comment-escape-string.recursive.html">Prev</a> </td><th width="60%" align="center">General Library Templates</th><td width="20%" align="right"> <a accesskey="n" href="str.tokenize.keep.delimiters.html">Next</a></td></tr></table><hr></div><div class="refentry" title="trim.text"><a name="trim.text"></a><div class="titlepage"></div>
<div class="refnamediv"><h2>trim.text</h2><p>
trim.text
 &#8212; Trim leading and trailing whitespace from a text node
</p></div>

<div class="refsect1" title="Description"><a name="id36258595"></a><h2>Description</h2>

<p>Given a text node, this function trims leading and trailing
whitespace from it and returns the trimmed contents.</p>

<a name="trim.text.frag"></a><pre class="programlisting">

  &lt;xsl:template name="trim.text"&gt;
    &lt;xsl:param name="contents" select="."&gt;&lt;/xsl:param&gt;
    &lt;xsl:variable name="contents-left-trimmed"&gt;
      &lt;xsl:call-template name="trim-left"&gt;
        &lt;xsl:with-param name="contents" select="$contents"&gt;&lt;/xsl:with-param&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:variable name="contents-trimmed"&gt;
      &lt;xsl:call-template name="trim-right"&gt;
        &lt;xsl:with-param name="contents" select="$contents-left-trimmed"&gt;&lt;/xsl:with-param&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:value-of select="$contents-trimmed"&gt;&lt;/xsl:value-of&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template name="trim-left"&gt;
    &lt;xsl:param name="contents"&gt;&lt;/xsl:param&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="starts-with($contents,'
') or                       starts-with($contents,'
') or                       starts-with($contents,' ') or                       starts-with($contents,'	')"&gt;
        &lt;xsl:call-template name="trim-left"&gt;
          &lt;xsl:with-param name="contents" select="substring($contents, 2)"&gt;&lt;/xsl:with-param&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:value-of select="$contents"&gt;&lt;/xsl:value-of&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template name="trim-right"&gt;
    &lt;xsl:param name="contents"&gt;&lt;/xsl:param&gt;
    &lt;xsl:variable name="last-char"&gt;
      &lt;xsl:value-of select="substring($contents, string-length($contents), 1)"&gt;&lt;/xsl:value-of&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="($last-char = '
') or                       ($last-char = '
') or                       ($last-char = ' ') or                       ($last-char = '	')"&gt;
        &lt;xsl:call-template name="trim-right"&gt;
          &lt;xsl:with-param name="contents" select="substring($contents, 1, string-length($contents) - 1)"&gt;&lt;/xsl:with-param&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:value-of select="$contents"&gt;&lt;/xsl:value-of&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
</pre>

</div>
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="comment-escape-string.recursive.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="generallibrary.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="str.tokenize.keep.delimiters.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">comment-escape-string.recursive </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> str.tokenize.keep.delimiters</td></tr></table></div></body></html>