1 <?xml version="1.0"?>
\r
4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
\r
5 xmlns:str="http://exslt.org/strings"
\r
6 xmlns:func="http://exslt.org/functions"
\r
7 xmlns:exsl="http://exslt.org/common"
\r
8 extension-element-prefixes="str exsl func">
\r
10 <func:function name="str:split">
\r
11 <xsl:param name="string" select="''" />
\r
12 <xsl:param name="pattern" select="' '" />
\r
14 <xsl:when test="not($string)">
\r
15 <func:result select="/.." />
\r
17 <xsl:when test="not(function-available('exsl:node-set'))">
\r
18 <xsl:message terminate="yes">
\r
19 ERROR: EXSLT - Functions implementation of str:split relies on exsl:node-set().
\r
23 <xsl:variable name="tokens">
\r
25 <xsl:when test="not($pattern)">
\r
26 <xsl:call-template name="str:_split-characters">
\r
27 <xsl:with-param name="string" select="$string" />
\r
28 </xsl:call-template>
\r
31 <xsl:call-template name="str:_split-pattern">
\r
32 <xsl:with-param name="string" select="$string" />
\r
33 <xsl:with-param name="pattern" select="$pattern" />
\r
34 </xsl:call-template>
\r
38 <func:result select="exsl:node-set($tokens)/token" />
\r
43 <xsl:template name="str:_split-characters">
\r
44 <xsl:param name="string" />
\r
45 <xsl:if test="$string">
\r
46 <token><xsl:value-of select="substring($string, 1, 1)" /></token>
\r
47 <xsl:call-template name="str:_split-characters">
\r
48 <xsl:with-param name="string" select="substring($string, 2)" />
\r
49 </xsl:call-template>
\r
53 <xsl:template name="str:_split-pattern">
\r
54 <xsl:param name="string" />
\r
55 <xsl:param name="pattern" />
\r
57 <xsl:when test="contains($string, $pattern)">
\r
58 <xsl:if test="not(starts-with($string, $pattern))">
\r
59 <token><xsl:value-of select="substring-before($string, $pattern)" /></token>
\r
61 <xsl:call-template name="str:_split-pattern">
\r
62 <xsl:with-param name="string" select="substring-after($string, $pattern)" />
\r
63 <xsl:with-param name="pattern" select="$pattern" />
\r
64 </xsl:call-template>
\r
67 <token><xsl:value-of select="$string" /></token>
\r
72 <!-- ==================================================================== -->
\r
73 <!-- node-set str:replace(string,object,object) -->
\r
75 <!-- This implements the EXSLT str:replace function -->
\r
77 <!-- Copyright Jeni Tenison -->
\r
78 <!-- ==================================================================== -->
\r
80 <func:function name="str:replace">
\r
81 <xsl:param name="string" select="''" />
\r
82 <xsl:param name="search" select="/.." />
\r
83 <xsl:param name="replace" select="/.." />
\r
85 <xsl:when test="not($string)">
\r
86 <func:result select="/.." />
\r
88 <xsl:when test="function-available('exsl:node-set')">
\r
89 <!-- this converts the search and replace arguments to node sets
\r
90 if they are one of the other XPath types -->
\r
91 <xsl:variable name="search-nodes-rtf">
\r
92 <xsl:copy-of select="$search" />
\r
94 <xsl:variable name="replace-nodes-rtf">
\r
95 <xsl:copy-of select="$replace" />
\r
97 <xsl:variable name="replacements-rtf">
\r
98 <xsl:for-each select="exsl:node-set($search-nodes-rtf)/node()">
\r
99 <xsl:variable name="pos" select="position()" />
\r
100 <replace search="{.}">
\r
101 <xsl:copy-of select="exsl:node-set($replace-nodes-rtf)/node()[$pos]" />
\r
105 <xsl:variable name="sorted-replacements-rtf">
\r
106 <xsl:for-each select="exsl:node-set($replacements-rtf)/replace">
\r
107 <xsl:sort select="string-length(@search)" data-type="number" order="descending" />
\r
108 <xsl:copy-of select="." />
\r
111 <xsl:variable name="result">
\r
113 <xsl:when test="not($search)">
\r
114 <xsl:value-of select="$string" />
\r
117 <xsl:call-template name="str:_replace">
\r
118 <xsl:with-param name="string" select="$string" />
\r
119 <xsl:with-param name="replacements" select="exsl:node-set($sorted-replacements-rtf)/replace" />
\r
120 </xsl:call-template>
\r
124 <func:result select="exsl:node-set($result)/node()" />
\r
127 <xsl:message terminate="yes">
\r
128 ERROR: function implementation of str:replace() relies on exsl:node-set().
\r
134 <xsl:template name="str:_replace">
\r
135 <xsl:param name="string" select="''" />
\r
136 <xsl:param name="replacements" select="/.." />
\r
138 <xsl:when test="not($string)" />
\r
139 <xsl:when test="not($replacements)">
\r
140 <xsl:value-of select="$string" />
\r
143 <xsl:variable name="replacement" select="$replacements[1]" />
\r
144 <xsl:variable name="search" select="$replacement/@search" />
\r
146 <xsl:when test="not(string($search))">
\r
147 <xsl:value-of select="substring($string, 1, 1)" />
\r
148 <xsl:copy-of select="$replacement/node()" />
\r
149 <xsl:call-template name="str:_replace">
\r
150 <xsl:with-param name="string" select="substring($string, 2)" />
\r
151 <xsl:with-param name="replacements" select="$replacements" />
\r
152 </xsl:call-template>
\r
154 <xsl:when test="contains($string, $search)">
\r
155 <xsl:call-template name="str:_replace">
\r
156 <xsl:with-param name="string" select="substring-before($string, $search)" />
\r
157 <xsl:with-param name="replacements" select="$replacements[position() > 1]" />
\r
158 </xsl:call-template>
\r
159 <xsl:copy-of select="$replacement/node()" />
\r
160 <xsl:call-template name="str:_replace">
\r
161 <xsl:with-param name="string" select="substring-after($string, $search)" />
\r
162 <xsl:with-param name="replacements" select="$replacements" />
\r
163 </xsl:call-template>
\r
166 <xsl:call-template name="str:_replace">
\r
167 <xsl:with-param name="string" select="$string" />
\r
168 <xsl:with-param name="replacements" select="$replacements[position() > 1]" />
\r
169 </xsl:call-template>
\r