Merge pull request #10651 from jitendrapurohit/CRM-19739
[civicrm-core.git] / tools / scripts / phpunit-xslt / str.replace.function.xsl
1 <?xml version="1.0"?>
2 <xsl:stylesheet version="1.0"
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:str="http://exslt.org/strings"
5 xmlns:func="http://exslt.org/functions"
6 xmlns:exsl="http://exslt.org/common"
7 extension-element-prefixes="str exsl func">
8
9 <func:function name="str:replace">
10 <xsl:param name="string" select="''" />
11 <xsl:param name="search" select="/.." />
12 <xsl:param name="replace" select="/.." />
13 <xsl:choose>
14 <xsl:when test="not($string)">
15 <func:result select="/.." />
16 </xsl:when>
17 <xsl:when test="function-available('exsl:node-set')">
18 <!-- this converts the search and replace arguments to node sets
19 if they are one of the other XPath types -->
20 <xsl:variable name="search-nodes-rtf">
21 <xsl:copy-of select="$search" />
22 </xsl:variable>
23 <xsl:variable name="replace-nodes-rtf">
24 <xsl:copy-of select="$replace" />
25 </xsl:variable>
26 <xsl:variable name="replacements-rtf">
27 <xsl:for-each select="exsl:node-set($search-nodes-rtf)/node()">
28 <xsl:variable name="pos" select="position()" />
29 <replace search="{.}">
30 <xsl:copy-of select="exsl:node-set($replace-nodes-rtf)/node()[$pos]" />
31 </replace>
32 </xsl:for-each>
33 </xsl:variable>
34 <xsl:variable name="sorted-replacements-rtf">
35 <xsl:for-each select="exsl:node-set($replacements-rtf)/replace">
36 <xsl:sort select="string-length(@search)" data-type="number" order="descending" />
37 <xsl:copy-of select="." />
38 </xsl:for-each>
39 </xsl:variable>
40 <xsl:variable name="result">
41 <xsl:choose>
42 <xsl:when test="not($search)">
43 <xsl:value-of select="$string" />
44 </xsl:when>
45 <xsl:otherwise>
46 <xsl:call-template name="str:_replace">
47 <xsl:with-param name="string" select="$string" />
48 <xsl:with-param name="replacements" select="exsl:node-set($sorted-replacements-rtf)/replace" />
49 </xsl:call-template>
50 </xsl:otherwise>
51 </xsl:choose>
52 </xsl:variable>
53 <func:result select="exsl:node-set($result)/node()" />
54 </xsl:when>
55 <xsl:otherwise>
56 <xsl:message terminate="yes">
57 ERROR: function implementation of str:replace() relies on exsl:node-set().
58 </xsl:message>
59 </xsl:otherwise>
60 </xsl:choose>
61 </func:function>
62
63 <xsl:template name="str:_replace">
64 <xsl:param name="string" select="''" />
65 <xsl:param name="replacements" select="/.." />
66 <xsl:choose>
67 <xsl:when test="not($string)" />
68 <xsl:when test="not($replacements)">
69 <xsl:value-of select="$string" />
70 </xsl:when>
71 <xsl:otherwise>
72 <xsl:variable name="replacement" select="$replacements[1]" />
73 <xsl:variable name="search" select="$replacement/@search" />
74 <xsl:choose>
75 <xsl:when test="not(string($search))">
76 <xsl:value-of select="substring($string, 1, 1)" />
77 <xsl:copy-of select="$replacement/node()" />
78 <xsl:call-template name="str:_replace">
79 <xsl:with-param name="string" select="substring($string, 2)" />
80 <xsl:with-param name="replacements" select="$replacements" />
81 </xsl:call-template>
82 </xsl:when>
83 <xsl:when test="contains($string, $search)">
84 <xsl:call-template name="str:_replace">
85 <xsl:with-param name="string" select="substring-before($string, $search)" />
86 <xsl:with-param name="replacements" select="$replacements[position() > 1]" />
87 </xsl:call-template>
88 <xsl:copy-of select="$replacement/node()" />
89 <xsl:call-template name="str:_replace">
90 <xsl:with-param name="string" select="substring-after($string, $search)" />
91 <xsl:with-param name="replacements" select="$replacements" />
92 </xsl:call-template>
93 </xsl:when>
94 <xsl:otherwise>
95 <xsl:call-template name="str:_replace">
96 <xsl:with-param name="string" select="$string" />
97 <xsl:with-param name="replacements" select="$replacements[position() > 1]" />
98 </xsl:call-template>
99 </xsl:otherwise>
100 </xsl:choose>
101 </xsl:otherwise>
102 </xsl:choose>
103 </xsl:template>
104
105 </xsl:stylesheet>