1 | <?xml version="1.0"?>
|
---|
2 |
|
---|
3 | <xsl:stylesheet version="1.0"
|
---|
4 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
---|
5 | xmlns:doc="http://xsltsl.org/xsl/documentation/1.0"
|
---|
6 | xmlns:str="http://xsltsl.org/string"
|
---|
7 | extension-element-prefixes="doc str">
|
---|
8 |
|
---|
9 | <doc:reference xmlns="">
|
---|
10 | <referenceinfo>
|
---|
11 | <releaseinfo role="meta">
|
---|
12 | $Id: string.xsl,v 1.1 2004/11/10 06:51:55 balls Exp $
|
---|
13 | </releaseinfo>
|
---|
14 | <author>
|
---|
15 | <surname>Ball</surname>
|
---|
16 | <firstname>Steve</firstname>
|
---|
17 | </author>
|
---|
18 | <copyright>
|
---|
19 | <year>2002</year>
|
---|
20 | <year>2001</year>
|
---|
21 | <holder>Steve Ball</holder>
|
---|
22 | </copyright>
|
---|
23 | </referenceinfo>
|
---|
24 |
|
---|
25 | <title>String Processing</title>
|
---|
26 |
|
---|
27 | <partintro>
|
---|
28 | <section>
|
---|
29 | <title>Introduction</title>
|
---|
30 |
|
---|
31 | <para>This module provides templates for manipulating strings.</para>
|
---|
32 |
|
---|
33 | </section>
|
---|
34 | </partintro>
|
---|
35 |
|
---|
36 | </doc:reference>
|
---|
37 |
|
---|
38 | <!-- Common string constants and datasets as XSL variables -->
|
---|
39 |
|
---|
40 | <!-- str:lower and str:upper contain pairs of lower and upper case
|
---|
41 | characters. Below insanely long strings should contain the
|
---|
42 | official lower/uppercase pairs, making this stylesheet working
|
---|
43 | for every language on earth. Hopefully. -->
|
---|
44 | <!-- These values are not enough, however. There are some
|
---|
45 | exceptions, dealt with below. -->
|
---|
46 | <xsl:variable name="xsltsl-str-lower" select="'abcdefghijklmnopqrstuvwxyzµàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıijĵķĺļľŀłńņňŋōŏőœŕŗřśŝşšţťŧũūŭůűųŵŷźżžſƃƅƈƌƒƕƙơƣƥƨƭưƴƶƹƽƿDždžLjljNjnjǎǐǒǔǖǘǚǜǝǟǡǣǥǧǩǫǭǯDzdzǵǹǻǽǿȁȃȅȇȉȋȍȏȑȓȕȗșțȝȟȣȥȧȩȫȭȯȱȳɓɔɖɗəɛɠɣɨɩɯɲɵʀʃʈʊʋʒͅάέήίαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώϐϑϕϖϛϝϟϡϣϥϧϩϫϭϯϰϱϲϵабвгдежзийклмнопрстуфхцчшщъыьэюяѐёђѓєѕіїјљњћќѝўџѡѣѥѧѩѫѭѯѱѳѵѷѹѻѽѿҁҍҏґғҕҗҙқҝҟҡңҥҧҩҫҭүұҳҵҷҹһҽҿӂӄӈӌӑӓӕӗәӛӝӟӡӣӥӧөӫӭӯӱӳӵӹաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆḁḃḅḇḉḋḍḏḑḓḕḗḙḛḝḟḡḣḥḧḩḫḭḯḱḳḵḷḹḻḽḿṁṃṅṇṉṋṍṏṑṓṕṗṙṛṝṟṡṣṥṧṩṫṭṯṱṳṵṷṹṻṽṿẁẃẅẇẉẋẍẏẑẓẕẛạảấầẩẫậắằẳẵặẹẻẽếềểễệỉịọỏốồổỗộớờởỡợụủứừửữựỳỵỷỹἀἁἂἃἄἅἆἇἐἑἒἓἔἕἠἡἢἣἤἥἦἧἰἱἲἳἴἵἶἷὀὁὂὃὄὅὑὓὕὗὠὡὢὣὤὥὦὧὰάὲέὴήὶίὸόὺύὼώᾀᾁᾂᾃᾄᾅᾆᾇᾐᾑᾒᾓᾔᾕᾖᾗᾠᾡᾢᾣᾤᾥᾦᾧᾰᾱᾳιῃῐῑῠῡῥῳⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩabcdefghijklmnopqrstuvwxyz𐐨𐐩𐐪𐐫𐐬𐐭𐐮𐐯𐐰𐐱𐐲𐐳𐐴𐐵𐐶𐐷𐐸𐐹𐐺𐐻𐐼𐐽𐐾𐐿𐑀𐑁𐑂𐑃𐑄𐑅𐑆𐑇𐑈𐑉𐑊𐑋𐑌𐑍'"/>
|
---|
47 | <xsl:variable name="xsltsl-str-upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZΜÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪĬĮIIJĴĶĹĻĽĿŁŃŅŇŊŌŎŐŒŔŖŘŚŜŞŠŢŤŦŨŪŬŮŰŲŴŶŹŻŽSƂƄƇƋƑǶƘƠƢƤƧƬƯƳƵƸƼǷDŽDŽLJLJNJNJǍǏǑǓǕǗǙǛƎǞǠǢǤǦǨǪǬǮDZDZǴǸǺǼǾȀȂȄȆȈȊȌȎȐȒȔȖȘȚȜȞȢȤȦȨȪȬȮȰȲƁƆƉƊƏƐƓƔƗƖƜƝƟƦƩƮƱƲƷΙΆΈΉΊΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΣΤΥΦΧΨΩΪΫΌΎΏΒΘΦΠϚϜϞϠϢϤϦϨϪϬϮΚΡΣΕАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏѠѢѤѦѨѪѬѮѰѲѴѶѸѺѼѾҀҌҎҐҒҔҖҘҚҜҞҠҢҤҦҨҪҬҮҰҲҴҶҸҺҼҾӁӃӇӋӐӒӔӖӘӚӜӞӠӢӤӦӨӪӬӮӰӲӴӸԱԲԳԴԵԶԷԸԹԺԻԼԽԾԿՀՁՂՃՄՅՆՇՈՉՊՋՌՍՎՏՐՑՒՓՔՕՖḀḂḄḆḈḊḌḎḐḒḔḖḘḚḜḞḠḢḤḦḨḪḬḮḰḲḴḶḸḺḼḾṀṂṄṆṈṊṌṎṐṒṔṖṘṚṜṞṠṢṤṦṨṪṬṮṰṲṴṶṸṺṼṾẀẂẄẆẈẊẌẎẐẒẔṠẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰỲỴỶỸἈἉἊἋἌἍἎἏἘἙἚἛἜἝἨἩἪἫἬἭἮἯἸἹἺἻἼἽἾἿὈὉὊὋὌὍὙὛὝὟὨὩὪὫὬὭὮὯᾺΆῈΈῊΉῚΊῸΌῪΎῺΏᾈᾉᾊᾋᾌᾍᾎᾏᾘᾙᾚᾛᾜᾝᾞᾟᾨᾩᾪᾫᾬᾭᾮᾯᾸᾹᾼΙῌῘῙῨῩῬῼⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏABCDEFGHIJKLMNOPQRSTUVWXYZ𐐀𐐁𐐂𐐃𐐄𐐅𐐆𐐇𐐈𐐉𐐊𐐋𐐌𐐍𐐎𐐏𐐐𐐑𐐒𐐓𐐔𐐕𐐖𐐗𐐘𐐙𐐚𐐛𐐜𐐝𐐞𐐟𐐠𐐡𐐢𐐣𐐤𐐥'"/>
|
---|
48 | <xsl:variable name="xsltsl-str-digits" select="'0123456789'"/>
|
---|
49 | <!-- space (#x20) characters, carriage returns, line feeds, or tabs. -->
|
---|
50 | <xsl:variable name="xsltsl-str-ws" select="' 	
'"/>
|
---|
51 |
|
---|
52 | <doc:template name="str:to-upper" xmlns="">
|
---|
53 | <refpurpose>Make string uppercase</refpurpose>
|
---|
54 |
|
---|
55 | <refdescription>
|
---|
56 | <para>Converts all lowercase letters to uppercase.</para>
|
---|
57 | </refdescription>
|
---|
58 |
|
---|
59 | <refparameter>
|
---|
60 | <variablelist>
|
---|
61 | <varlistentry>
|
---|
62 | <term>text</term>
|
---|
63 | <listitem>
|
---|
64 | <para>The string to be converted</para>
|
---|
65 | </listitem>
|
---|
66 | </varlistentry>
|
---|
67 | </variablelist>
|
---|
68 | </refparameter>
|
---|
69 |
|
---|
70 | <refreturn>
|
---|
71 | <para>Returns string with all uppercase letters.</para>
|
---|
72 | </refreturn>
|
---|
73 | </doc:template>
|
---|
74 |
|
---|
75 | <xsl:template name="str:to-upper">
|
---|
76 | <xsl:param name="text"/>
|
---|
77 |
|
---|
78 | <!-- Below exception is extracted from unicode's SpecialCasing.txt
|
---|
79 | file. It's the german lowercase "eszett" (the thing looking
|
---|
80 | like a greek beta) that's to become "SS" in uppercase (note:
|
---|
81 | that are *two* characters, that's why it doesn't fit in the
|
---|
82 | list of upper/lowercase characters). There are more
|
---|
83 | characters in that file (103, excluding the locale-specific
|
---|
84 | ones), but they seemed to be much less used to me and they
|
---|
85 | add up to a hellish long stylesheet.... - Reinout -->
|
---|
86 | <xsl:param name="modified-text">
|
---|
87 | <xsl:call-template name="str:subst">
|
---|
88 | <xsl:with-param name="text">
|
---|
89 | <xsl:value-of select="$text"/>
|
---|
90 | </xsl:with-param>
|
---|
91 | <xsl:with-param name="replace">
|
---|
92 | <xsl:text>ß</xsl:text>
|
---|
93 | </xsl:with-param>
|
---|
94 | <xsl:with-param name="with">
|
---|
95 | <xsl:text>S</xsl:text>
|
---|
96 | <xsl:text>S</xsl:text>
|
---|
97 | </xsl:with-param>
|
---|
98 | </xsl:call-template>
|
---|
99 | </xsl:param>
|
---|
100 |
|
---|
101 | <xsl:value-of select="translate($modified-text, $xsltsl-str-lower, $xsltsl-str-upper)"/>
|
---|
102 | </xsl:template>
|
---|
103 |
|
---|
104 | <doc:template name="str:to-lower" xmlns="">
|
---|
105 | <refpurpose>Make string lowercase</refpurpose>
|
---|
106 |
|
---|
107 | <refdescription>
|
---|
108 | <para>Converts all uppercase letters to lowercase.</para>
|
---|
109 | </refdescription>
|
---|
110 |
|
---|
111 | <refparameter>
|
---|
112 | <variablelist>
|
---|
113 | <varlistentry>
|
---|
114 | <term>text</term>
|
---|
115 | <listitem>
|
---|
116 | <para>The string to be converted</para>
|
---|
117 | </listitem>
|
---|
118 | </varlistentry>
|
---|
119 | </variablelist>
|
---|
120 | </refparameter>
|
---|
121 |
|
---|
122 | <refreturn>
|
---|
123 | <para>Returns string with all lowercase letters.</para>
|
---|
124 | </refreturn>
|
---|
125 | </doc:template>
|
---|
126 |
|
---|
127 | <xsl:template name="str:to-lower">
|
---|
128 | <xsl:param name="text"/>
|
---|
129 |
|
---|
130 | <xsl:value-of select="translate($text, $xsltsl-str-upper, $xsltsl-str-lower)"/>
|
---|
131 | </xsl:template>
|
---|
132 |
|
---|
133 | <doc:template name="str:capitalise" xmlns="">
|
---|
134 | <refpurpose>Capitalise string</refpurpose>
|
---|
135 |
|
---|
136 | <refdescription>
|
---|
137 | <para>Converts first character of string to an uppercase letter. All remaining characters are converted to lowercase.</para>
|
---|
138 | </refdescription>
|
---|
139 |
|
---|
140 | <refparameter>
|
---|
141 | <variablelist>
|
---|
142 | <varlistentry>
|
---|
143 | <term>text</term>
|
---|
144 | <listitem>
|
---|
145 | <para>The string to be capitalised</para>
|
---|
146 | </listitem>
|
---|
147 | </varlistentry>
|
---|
148 | <varlistentry>
|
---|
149 | <term>all</term>
|
---|
150 | <listitem>
|
---|
151 | <para>Boolean controlling whether all words in the string are capitalised.</para>
|
---|
152 | <para>Default is true.</para>
|
---|
153 | </listitem>
|
---|
154 | </varlistentry>
|
---|
155 | </variablelist>
|
---|
156 | </refparameter>
|
---|
157 |
|
---|
158 | <refreturn>
|
---|
159 | <para>Returns string with first character uppcase and all remaining characters lowercase.</para>
|
---|
160 | </refreturn>
|
---|
161 | </doc:template>
|
---|
162 |
|
---|
163 | <xsl:template name="str:capitalise">
|
---|
164 | <xsl:param name="text"/>
|
---|
165 | <xsl:param name="all" select="true()"/>
|
---|
166 |
|
---|
167 | <xsl:choose>
|
---|
168 | <xsl:when test="$all and (contains($text, ' ') or contains($text, ' ') or contains($text, ' '))">
|
---|
169 | <xsl:variable name="firstword">
|
---|
170 | <xsl:call-template name="str:substring-before-first">
|
---|
171 | <xsl:with-param name="text" select="$text"/>
|
---|
172 | <xsl:with-param name="chars" select="$xsltsl-str-ws"/>
|
---|
173 | </xsl:call-template>
|
---|
174 | </xsl:variable>
|
---|
175 | <xsl:call-template name="str:capitalise">
|
---|
176 | <xsl:with-param name="text">
|
---|
177 | <xsl:value-of select="$firstword"/>
|
---|
178 | </xsl:with-param>
|
---|
179 | <xsl:with-param name="all" select="false()"/>
|
---|
180 | </xsl:call-template>
|
---|
181 | <xsl:value-of select="substring($text, string-length($firstword) + 1, 1)"/>
|
---|
182 | <xsl:call-template name="str:capitalise">
|
---|
183 | <xsl:with-param name="text">
|
---|
184 | <xsl:value-of select="substring($text, string-length($firstword) + 2)"/>
|
---|
185 | </xsl:with-param>
|
---|
186 | </xsl:call-template>
|
---|
187 | </xsl:when>
|
---|
188 |
|
---|
189 | <xsl:otherwise>
|
---|
190 | <xsl:call-template name="str:to-upper">
|
---|
191 | <xsl:with-param name="text" select="substring($text, 1, 1)"/>
|
---|
192 | </xsl:call-template>
|
---|
193 | <xsl:call-template name="str:to-lower">
|
---|
194 | <xsl:with-param name="text" select="substring($text, 2)"/>
|
---|
195 | </xsl:call-template>
|
---|
196 | </xsl:otherwise>
|
---|
197 | </xsl:choose>
|
---|
198 | </xsl:template>
|
---|
199 |
|
---|
200 | <doc:template name="str:to-camelcase" xmlns="">
|
---|
201 | <refpurpose>Convert a string to one camelcase word</refpurpose>
|
---|
202 |
|
---|
203 | <refdescription>
|
---|
204 | <para>Converts a string to one lowerCamelCase or UpperCamelCase
|
---|
205 | word, depending on the setting of the "upper"
|
---|
206 | parameter. UpperCamelCase is also called MixedCase while
|
---|
207 | lowerCamelCase is also called just camelCase. The template
|
---|
208 | removes any spaces, tabs and slashes, but doesn't deal with
|
---|
209 | other punctuation. It's purpose is to convert strings like
|
---|
210 | "hollow timber flush door" to a term suitable as identifier or
|
---|
211 | XML tag like "HollowTimberFlushDoor".
|
---|
212 | </para>
|
---|
213 | </refdescription>
|
---|
214 |
|
---|
215 | <refparameter>
|
---|
216 | <variablelist>
|
---|
217 | <varlistentry>
|
---|
218 | <term>text</term>
|
---|
219 | <listitem>
|
---|
220 | <para>The string to be capitalised</para>
|
---|
221 | </listitem>
|
---|
222 | </varlistentry>
|
---|
223 | <varlistentry>
|
---|
224 | <term>upper</term>
|
---|
225 | <listitem>
|
---|
226 | <para>Boolean controlling whether the string becomes an
|
---|
227 | UpperCamelCase word or a lowerCamelCase word.</para>
|
---|
228 | <para>Default is true.</para>
|
---|
229 | </listitem>
|
---|
230 | </varlistentry>
|
---|
231 | </variablelist>
|
---|
232 | </refparameter>
|
---|
233 |
|
---|
234 | <refreturn>
|
---|
235 | <para>Returns string with first character uppcase and all remaining characters lowercase.</para>
|
---|
236 | </refreturn>
|
---|
237 | </doc:template>
|
---|
238 |
|
---|
239 | <xsl:template name="str:to-camelcase">
|
---|
240 | <xsl:param name="text"/>
|
---|
241 | <xsl:param name="upper" select="true()"/>
|
---|
242 | <!-- First change all 'strange' characters to spaces -->
|
---|
243 | <xsl:param name="string-with-only-spaces">
|
---|
244 | <xsl:value-of select="translate($text,concat($xsltsl-str-ws,'/'),' ')"/>
|
---|
245 | </xsl:param>
|
---|
246 | <!-- Then process them -->
|
---|
247 | <xsl:param name="before-space-removal">
|
---|
248 | <xsl:variable name="firstword">
|
---|
249 | <xsl:call-template name="str:substring-before-first">
|
---|
250 | <xsl:with-param name="text" select="$string-with-only-spaces"/>
|
---|
251 | <xsl:with-param name="chars" select="$xsltsl-str-ws"/>
|
---|
252 | </xsl:call-template>
|
---|
253 | </xsl:variable>
|
---|
254 | <xsl:choose>
|
---|
255 | <xsl:when test="$upper">
|
---|
256 | <xsl:call-template name="str:to-upper">
|
---|
257 | <xsl:with-param name="text" select="substring($firstword, 1, 1)"/>
|
---|
258 | </xsl:call-template>
|
---|
259 | <xsl:call-template name="str:to-lower">
|
---|
260 | <xsl:with-param name="text" select="substring($firstword, 2)"/>
|
---|
261 | </xsl:call-template>
|
---|
262 | </xsl:when>
|
---|
263 | <xsl:otherwise>
|
---|
264 | <xsl:call-template name="str:to-lower">
|
---|
265 | <xsl:with-param name="text" select="$firstword"/>
|
---|
266 | </xsl:call-template>
|
---|
267 | </xsl:otherwise>
|
---|
268 | </xsl:choose>
|
---|
269 |
|
---|
270 | <xsl:call-template name="str:capitalise">
|
---|
271 | <xsl:with-param name="text">
|
---|
272 | <xsl:value-of select="substring($string-with-only-spaces, string-length($firstword) + 2)"/>
|
---|
273 | </xsl:with-param>
|
---|
274 | <xsl:with-param name="all" select="true()"/>
|
---|
275 | </xsl:call-template>
|
---|
276 | </xsl:param>
|
---|
277 | <xsl:value-of select="translate($before-space-removal,' ','')"/>
|
---|
278 | </xsl:template>
|
---|
279 |
|
---|
280 | <doc:template name="str:substring-before-first" xmlns="">
|
---|
281 | <refpurpose>String extraction</refpurpose>
|
---|
282 |
|
---|
283 | <refdescription>
|
---|
284 | <para>Extracts the portion of string 'text' which occurs before any of the characters in string 'chars'.</para>
|
---|
285 | </refdescription>
|
---|
286 |
|
---|
287 | <refparameter>
|
---|
288 | <variablelist>
|
---|
289 | <varlistentry>
|
---|
290 | <term>text</term>
|
---|
291 | <listitem>
|
---|
292 | <para>The string from which to extract a substring.</para>
|
---|
293 | </listitem>
|
---|
294 | </varlistentry>
|
---|
295 | <varlistentry>
|
---|
296 | <term>chars</term>
|
---|
297 | <listitem>
|
---|
298 | <para>The string containing characters to find.</para>
|
---|
299 | </listitem>
|
---|
300 | </varlistentry>
|
---|
301 | </variablelist>
|
---|
302 | </refparameter>
|
---|
303 |
|
---|
304 | <refreturn>
|
---|
305 | <para>Returns string.</para>
|
---|
306 | </refreturn>
|
---|
307 | </doc:template>
|
---|
308 |
|
---|
309 | <xsl:template name="str:substring-before-first">
|
---|
310 | <xsl:param name="text"/>
|
---|
311 | <xsl:param name="chars"/>
|
---|
312 |
|
---|
313 | <xsl:choose>
|
---|
314 |
|
---|
315 | <xsl:when test="string-length($text) = 0"/>
|
---|
316 |
|
---|
317 | <xsl:when test="string-length($chars) = 0">
|
---|
318 | <xsl:value-of select="$text"/>
|
---|
319 | </xsl:when>
|
---|
320 |
|
---|
321 | <xsl:when test="contains($text, substring($chars, 1, 1))">
|
---|
322 | <xsl:variable name="this" select="substring-before($text, substring($chars, 1, 1))"/>
|
---|
323 | <xsl:variable name="rest">
|
---|
324 | <xsl:call-template name="str:substring-before-first">
|
---|
325 | <xsl:with-param name="text" select="$text"/>
|
---|
326 | <xsl:with-param name="chars" select="substring($chars, 2)"/>
|
---|
327 | </xsl:call-template>
|
---|
328 | </xsl:variable>
|
---|
329 |
|
---|
330 | <xsl:choose>
|
---|
331 | <xsl:when test="string-length($this) < string-length($rest)">
|
---|
332 | <xsl:value-of select="$this"/>
|
---|
333 | </xsl:when>
|
---|
334 | <xsl:otherwise>
|
---|
335 | <xsl:value-of select="$rest"/>
|
---|
336 | </xsl:otherwise>
|
---|
337 | </xsl:choose>
|
---|
338 | </xsl:when>
|
---|
339 |
|
---|
340 | <xsl:otherwise>
|
---|
341 | <xsl:call-template name="str:substring-before-first">
|
---|
342 | <xsl:with-param name="text" select="$text"/>
|
---|
343 | <xsl:with-param name="chars" select="substring($chars, 2)"/>
|
---|
344 | </xsl:call-template>
|
---|
345 | </xsl:otherwise>
|
---|
346 |
|
---|
347 | </xsl:choose>
|
---|
348 | </xsl:template>
|
---|
349 |
|
---|
350 | <doc:template name="str:substring-after-last" xmlns="">
|
---|
351 | <refpurpose>String extraction</refpurpose>
|
---|
352 |
|
---|
353 | <refdescription>
|
---|
354 | <para>Extracts the portion of string 'text' which occurs after the last of the character in string 'chars'.</para>
|
---|
355 | </refdescription>
|
---|
356 |
|
---|
357 | <refparameter>
|
---|
358 | <variablelist>
|
---|
359 | <varlistentry>
|
---|
360 | <term>text</term>
|
---|
361 | <listitem>
|
---|
362 | <para>The string from which to extract a substring.</para>
|
---|
363 | </listitem>
|
---|
364 | </varlistentry>
|
---|
365 | <varlistentry>
|
---|
366 | <term>chars</term>
|
---|
367 | <listitem>
|
---|
368 | <para>The string containing characters to find.</para>
|
---|
369 | </listitem>
|
---|
370 | </varlistentry>
|
---|
371 | </variablelist>
|
---|
372 | </refparameter>
|
---|
373 |
|
---|
374 | <refreturn>
|
---|
375 | <para>Returns string.</para>
|
---|
376 | </refreturn>
|
---|
377 | </doc:template>
|
---|
378 |
|
---|
379 | <xsl:template name="str:substring-after-last">
|
---|
380 | <xsl:param name="text"/>
|
---|
381 | <xsl:param name="chars"/>
|
---|
382 |
|
---|
383 | <xsl:choose>
|
---|
384 |
|
---|
385 | <xsl:when test="contains($text, $chars)">
|
---|
386 | <xsl:variable name="last" select="substring-after($text, $chars)"/>
|
---|
387 |
|
---|
388 | <xsl:choose>
|
---|
389 | <xsl:when test="contains($last, $chars)">
|
---|
390 | <xsl:call-template name="str:substring-after-last">
|
---|
391 | <xsl:with-param name="text" select="$last"/>
|
---|
392 | <xsl:with-param name="chars" select="$chars"/>
|
---|
393 | </xsl:call-template>
|
---|
394 | </xsl:when>
|
---|
395 | <xsl:otherwise>
|
---|
396 | <xsl:value-of select="$last"/>
|
---|
397 | </xsl:otherwise>
|
---|
398 | </xsl:choose>
|
---|
399 | </xsl:when>
|
---|
400 |
|
---|
401 | <xsl:otherwise>
|
---|
402 | <xsl:value-of select="$text"/>
|
---|
403 | </xsl:otherwise>
|
---|
404 |
|
---|
405 | </xsl:choose>
|
---|
406 | </xsl:template>
|
---|
407 |
|
---|
408 | <doc:template name="str:substring-before-last" xmlns="">
|
---|
409 | <refpurpose>String extraction</refpurpose>
|
---|
410 |
|
---|
411 | <refdescription>
|
---|
412 | <para>Extracts the portion of string 'text' which occurs before the first character of the last occurance of string 'chars'.</para>
|
---|
413 | </refdescription>
|
---|
414 |
|
---|
415 | <refparameter>
|
---|
416 | <variablelist>
|
---|
417 | <varlistentry>
|
---|
418 | <term>text</term>
|
---|
419 | <listitem>
|
---|
420 | <para>The string from which to extract a substring.</para>
|
---|
421 | </listitem>
|
---|
422 | </varlistentry>
|
---|
423 | <varlistentry>
|
---|
424 | <term>chars</term>
|
---|
425 | <listitem>
|
---|
426 | <para>The string containing characters to find.</para>
|
---|
427 | </listitem>
|
---|
428 | </varlistentry>
|
---|
429 | </variablelist>
|
---|
430 | </refparameter>
|
---|
431 |
|
---|
432 | <refreturn>
|
---|
433 | <para>Returns string.</para>
|
---|
434 | </refreturn>
|
---|
435 | </doc:template>
|
---|
436 |
|
---|
437 | <xsl:template name="str:substring-before-last">
|
---|
438 | <xsl:param name="text"/>
|
---|
439 | <xsl:param name="chars"/>
|
---|
440 |
|
---|
441 | <xsl:choose>
|
---|
442 |
|
---|
443 | <xsl:when test="string-length($text) = 0"/>
|
---|
444 |
|
---|
445 | <xsl:when test="string-length($chars) = 0">
|
---|
446 | <xsl:value-of select="$text"/>
|
---|
447 | </xsl:when>
|
---|
448 |
|
---|
449 | <xsl:when test="contains($text, $chars)">
|
---|
450 | <xsl:call-template name="str:substring-before-last-aux">
|
---|
451 | <xsl:with-param name="text" select="$text"/>
|
---|
452 | <xsl:with-param name="chars" select="$chars"/>
|
---|
453 | </xsl:call-template>
|
---|
454 | </xsl:when>
|
---|
455 |
|
---|
456 | <xsl:otherwise>
|
---|
457 | <xsl:value-of select="$text"/>
|
---|
458 | </xsl:otherwise>
|
---|
459 |
|
---|
460 | </xsl:choose>
|
---|
461 | </xsl:template>
|
---|
462 |
|
---|
463 | <xsl:template name="str:substring-before-last-aux">
|
---|
464 | <xsl:param name="text"/>
|
---|
465 | <xsl:param name="chars"/>
|
---|
466 |
|
---|
467 | <xsl:choose>
|
---|
468 | <xsl:when test="string-length($text) = 0"/>
|
---|
469 |
|
---|
470 | <xsl:when test="contains($text, $chars)">
|
---|
471 | <xsl:variable name="after">
|
---|
472 | <xsl:call-template name="str:substring-before-last-aux">
|
---|
473 | <xsl:with-param name="text" select="substring-after($text, $chars)"/>
|
---|
474 | <xsl:with-param name="chars" select="$chars"/>
|
---|
475 | </xsl:call-template>
|
---|
476 | </xsl:variable>
|
---|
477 |
|
---|
478 | <xsl:value-of select="substring-before($text, $chars)"/>
|
---|
479 | <xsl:if test="string-length($after) > 0">
|
---|
480 | <xsl:value-of select="$chars"/>
|
---|
481 | <xsl:copy-of select="$after"/>
|
---|
482 | </xsl:if>
|
---|
483 | </xsl:when>
|
---|
484 |
|
---|
485 | <xsl:otherwise/>
|
---|
486 | </xsl:choose>
|
---|
487 | </xsl:template>
|
---|
488 |
|
---|
489 | <doc:template name="str:subst" xmlns="">
|
---|
490 | <refpurpose>String substitution</refpurpose>
|
---|
491 |
|
---|
492 | <refdescription>
|
---|
493 | <para>Substitute 'replace' for 'with' in string 'text'.</para>
|
---|
494 | </refdescription>
|
---|
495 |
|
---|
496 | <refparameter>
|
---|
497 | <variablelist>
|
---|
498 | <varlistentry>
|
---|
499 | <term>text</term>
|
---|
500 | <listitem>
|
---|
501 | <para>The string upon which to perform substitution.</para>
|
---|
502 | </listitem>
|
---|
503 | </varlistentry>
|
---|
504 | <varlistentry>
|
---|
505 | <term>replace</term>
|
---|
506 | <listitem>
|
---|
507 | <para>The string to substitute.</para>
|
---|
508 | </listitem>
|
---|
509 | </varlistentry>
|
---|
510 | <varlistentry>
|
---|
511 | <term>with</term>
|
---|
512 | <listitem>
|
---|
513 | <para>The string to be substituted.</para>
|
---|
514 | </listitem>
|
---|
515 | </varlistentry>
|
---|
516 | <varlistentry>
|
---|
517 | <term>disable-output-escaping</term>
|
---|
518 | <listitem>
|
---|
519 | <para>A value of <literal>yes</literal> indicates that the result should have output escaping disabled. Any other value allows normal escaping of text values. The default is to enable output escaping.</para>
|
---|
520 | </listitem>
|
---|
521 | </varlistentry>
|
---|
522 | </variablelist>
|
---|
523 | </refparameter>
|
---|
524 |
|
---|
525 | <refreturn>
|
---|
526 | <para>Returns string.</para>
|
---|
527 | </refreturn>
|
---|
528 | </doc:template>
|
---|
529 |
|
---|
530 | <xsl:template name="str:subst">
|
---|
531 | <xsl:param name="text"/>
|
---|
532 | <xsl:param name="replace"/>
|
---|
533 | <xsl:param name="with"/>
|
---|
534 | <xsl:param name='disable-output-escaping'>no</xsl:param>
|
---|
535 |
|
---|
536 | <xsl:choose>
|
---|
537 | <xsl:when test="string-length($replace) = 0 and $disable-output-escaping = 'yes'">
|
---|
538 | <xsl:value-of select="$text" disable-output-escaping='yes'/>
|
---|
539 | </xsl:when>
|
---|
540 | <xsl:when test="string-length($replace) = 0">
|
---|
541 | <xsl:value-of select="$text"/>
|
---|
542 | </xsl:when>
|
---|
543 | <xsl:when test="contains($text, $replace)">
|
---|
544 |
|
---|
545 | <xsl:variable name="before" select="substring-before($text, $replace)"/>
|
---|
546 | <xsl:variable name="after" select="substring-after($text, $replace)"/>
|
---|
547 |
|
---|
548 | <xsl:choose>
|
---|
549 | <xsl:when test='$disable-output-escaping = "yes"'>
|
---|
550 | <xsl:value-of select="$before" disable-output-escaping="yes"/>
|
---|
551 | <xsl:value-of select="$with" disable-output-escaping="yes"/>
|
---|
552 | </xsl:when>
|
---|
553 | <xsl:otherwise>
|
---|
554 | <xsl:value-of select="$before"/>
|
---|
555 | <xsl:value-of select="$with"/>
|
---|
556 | </xsl:otherwise>
|
---|
557 | </xsl:choose>
|
---|
558 | <xsl:call-template name="str:subst">
|
---|
559 | <xsl:with-param name="text" select="$after"/>
|
---|
560 | <xsl:with-param name="replace" select="$replace"/>
|
---|
561 | <xsl:with-param name="with" select="$with"/>
|
---|
562 | <xsl:with-param name="disable-output-escaping" select="$disable-output-escaping"/>
|
---|
563 | </xsl:call-template>
|
---|
564 | </xsl:when>
|
---|
565 | <xsl:when test='$disable-output-escaping = "yes"'>
|
---|
566 | <xsl:value-of select="$text" disable-output-escaping="yes"/>
|
---|
567 | </xsl:when>
|
---|
568 | <xsl:otherwise>
|
---|
569 | <xsl:value-of select="$text"/>
|
---|
570 | </xsl:otherwise>
|
---|
571 | </xsl:choose>
|
---|
572 | </xsl:template>
|
---|
573 |
|
---|
574 | <doc:template name="str:count-substring" xmlns="">
|
---|
575 | <refpurpose>Count Substrings</refpurpose>
|
---|
576 |
|
---|
577 | <refdescription>
|
---|
578 | <para>Counts the number of times a substring occurs in a string. This can also counts the number of times a character occurs in a string, since a character is simply a string of length 1.</para>
|
---|
579 | </refdescription>
|
---|
580 |
|
---|
581 | <example>
|
---|
582 | <title>Counting Lines</title>
|
---|
583 | <programlisting><![CDATA[
|
---|
584 | <xsl:call-template name="str:count-substring">
|
---|
585 | <xsl:with-param name="text" select="$mytext"/>
|
---|
586 | <xsl:with-param name="chars" select="'
'"/>
|
---|
587 | </xsl:call-template>
|
---|
588 | ]]></programlisting>
|
---|
589 | </example>
|
---|
590 |
|
---|
591 | <refparameter>
|
---|
592 | <variablelist>
|
---|
593 | <varlistentry>
|
---|
594 | <term>text</term>
|
---|
595 | <listitem>
|
---|
596 | <para>The source string.</para>
|
---|
597 | </listitem>
|
---|
598 | </varlistentry>
|
---|
599 | <varlistentry>
|
---|
600 | <term>chars</term>
|
---|
601 | <listitem>
|
---|
602 | <para>The substring to count.</para>
|
---|
603 | </listitem>
|
---|
604 | </varlistentry>
|
---|
605 | </variablelist>
|
---|
606 | </refparameter>
|
---|
607 |
|
---|
608 | <refreturn>
|
---|
609 | <para>Returns a non-negative integer value.</para>
|
---|
610 | </refreturn>
|
---|
611 | </doc:template>
|
---|
612 |
|
---|
613 | <xsl:template name="str:count-substring">
|
---|
614 | <xsl:param name="text"/>
|
---|
615 | <xsl:param name="chars"/>
|
---|
616 |
|
---|
617 | <xsl:choose>
|
---|
618 | <xsl:when test="string-length($text) = 0 or string-length($chars) = 0">
|
---|
619 | <xsl:text>0</xsl:text>
|
---|
620 | </xsl:when>
|
---|
621 | <xsl:when test="contains($text, $chars)">
|
---|
622 | <xsl:variable name="remaining">
|
---|
623 | <xsl:call-template name="str:count-substring">
|
---|
624 | <xsl:with-param name="text" select="substring-after($text, $chars)"/>
|
---|
625 | <xsl:with-param name="chars" select="$chars"/>
|
---|
626 | </xsl:call-template>
|
---|
627 | </xsl:variable>
|
---|
628 | <xsl:value-of select="$remaining + 1"/>
|
---|
629 | </xsl:when>
|
---|
630 | <xsl:otherwise>
|
---|
631 | <xsl:text>0</xsl:text>
|
---|
632 | </xsl:otherwise>
|
---|
633 | </xsl:choose>
|
---|
634 | </xsl:template>
|
---|
635 |
|
---|
636 | <doc:template name="str:substring-after-at" xmlns="">
|
---|
637 | <refpurpose>String extraction</refpurpose>
|
---|
638 | <refdescription>
|
---|
639 | <para>Extracts the portion of a 'char' delimited 'text' string "array" at a given 'position'.</para>
|
---|
640 | </refdescription>
|
---|
641 | <refparameter>
|
---|
642 | <variablelist>
|
---|
643 | <varlistentry>
|
---|
644 | <term>text</term>
|
---|
645 | <listitem>
|
---|
646 | <para>The string from which to extract a substring.</para>
|
---|
647 | </listitem>
|
---|
648 | </varlistentry>
|
---|
649 | <varlistentry>
|
---|
650 | <term>chars</term>
|
---|
651 | <listitem>
|
---|
652 | <para>delimiters</para>
|
---|
653 | </listitem>
|
---|
654 | </varlistentry>
|
---|
655 | <varlistentry>
|
---|
656 | <term>position</term>
|
---|
657 | <listitem>
|
---|
658 | <para>position of the elements</para>
|
---|
659 | </listitem>
|
---|
660 | </varlistentry>
|
---|
661 | <varlistentry>
|
---|
662 | <term>all</term>
|
---|
663 | <listitem>
|
---|
664 | <para>If true all of the remaining string is returned, otherwise only the element at the given position is returned. Default: false().</para>
|
---|
665 | </listitem>
|
---|
666 | </varlistentry>
|
---|
667 | </variablelist>
|
---|
668 | </refparameter>
|
---|
669 | <refreturn>
|
---|
670 | <para>Returns string.</para>
|
---|
671 | </refreturn>
|
---|
672 | </doc:template>
|
---|
673 |
|
---|
674 |
|
---|
675 | <xsl:template name="str:substring-after-at">
|
---|
676 | <xsl:param name="text"/>
|
---|
677 | <xsl:param name="chars"/>
|
---|
678 | <xsl:param name="position"/>
|
---|
679 | <xsl:param name="all" select='false()'/>
|
---|
680 |
|
---|
681 | <xsl:choose>
|
---|
682 | <xsl:when test='number($position) = 0 and $all'>
|
---|
683 | <xsl:value-of select='$text'/>
|
---|
684 | </xsl:when>
|
---|
685 | <xsl:when test='number($position) = 0 and not($chars)'>
|
---|
686 | <xsl:value-of select='$text'/>
|
---|
687 | </xsl:when>
|
---|
688 | <xsl:when test='number($position) = 0 and not(contains($text, $chars))'>
|
---|
689 | <xsl:value-of select='$text'/>
|
---|
690 | </xsl:when>
|
---|
691 | <xsl:when test='not(contains($text, $chars))'>
|
---|
692 | </xsl:when>
|
---|
693 | <xsl:when test="number($position) = 0">
|
---|
694 | <xsl:value-of select="substring-before($text, $chars)"/>
|
---|
695 | </xsl:when>
|
---|
696 | <xsl:otherwise>
|
---|
697 | <xsl:call-template name="str:substring-after-at">
|
---|
698 | <xsl:with-param name="text" select="substring-after($text, $chars)"/>
|
---|
699 | <xsl:with-param name="chars" select="$chars"/>
|
---|
700 | <xsl:with-param name="all" select="$all"/>
|
---|
701 | <xsl:with-param name="position" select="$position - 1"/>
|
---|
702 | </xsl:call-template>
|
---|
703 | </xsl:otherwise>
|
---|
704 | </xsl:choose>
|
---|
705 | </xsl:template>
|
---|
706 |
|
---|
707 | <doc:template name="str:substring-before-at" xmlns="">
|
---|
708 | <refpurpose>String extraction</refpurpose>
|
---|
709 | <refdescription>
|
---|
710 | <para>Extracts the portion of a 'char' delimited 'text' string "array" at a given 'position' </para>
|
---|
711 | </refdescription>
|
---|
712 | <refparameter>
|
---|
713 | <variablelist>
|
---|
714 | <varlistentry>
|
---|
715 | <term>text</term>
|
---|
716 | <listitem>
|
---|
717 | <para>The string from which to extract a substring.</para>
|
---|
718 | </listitem>
|
---|
719 | </varlistentry>
|
---|
720 | <varlistentry>
|
---|
721 | <term>chars</term>
|
---|
722 | <listitem>
|
---|
723 | <para>delimiters</para>
|
---|
724 | </listitem>
|
---|
725 | </varlistentry>
|
---|
726 | <varlistentry>
|
---|
727 | <term>position</term>
|
---|
728 | <listitem>
|
---|
729 | <para>position of the elements</para>
|
---|
730 | </listitem>
|
---|
731 | </varlistentry>
|
---|
732 | </variablelist>
|
---|
733 | </refparameter>
|
---|
734 | <refreturn>
|
---|
735 | <para>Returns string.</para>
|
---|
736 | </refreturn>
|
---|
737 | </doc:template>
|
---|
738 |
|
---|
739 | <xsl:template name="str:substring-before-at">
|
---|
740 | <xsl:param name="text"/>
|
---|
741 | <xsl:param name="chars"/>
|
---|
742 | <xsl:param name="position"/>
|
---|
743 |
|
---|
744 | <xsl:choose>
|
---|
745 | <xsl:when test="$position <= 0"/>
|
---|
746 | <xsl:when test="not(contains($text, $chars))"/>
|
---|
747 | <xsl:otherwise>
|
---|
748 | <xsl:value-of select='substring-before($text, $chars)'/>
|
---|
749 | <xsl:value-of select='$chars'/>
|
---|
750 |
|
---|
751 | <xsl:call-template name="str:substring-before-at">
|
---|
752 | <xsl:with-param name="text" select="substring-after($text, $chars)"/>
|
---|
753 | <xsl:with-param name="position" select="$position - 1"/>
|
---|
754 | <xsl:with-param name="chars" select="$chars"/>
|
---|
755 | </xsl:call-template>
|
---|
756 |
|
---|
757 | </xsl:otherwise>
|
---|
758 | </xsl:choose>
|
---|
759 | </xsl:template>
|
---|
760 |
|
---|
761 | <doc:template name="str:insert-at" xmlns="">
|
---|
762 | <refpurpose>String insertion</refpurpose>
|
---|
763 | <refdescription>
|
---|
764 | <para>Insert 'chars' into "text' at any given "position'</para>
|
---|
765 | </refdescription>
|
---|
766 | <refparameter>
|
---|
767 | <variablelist>
|
---|
768 | <varlistentry>
|
---|
769 | <term>text</term>
|
---|
770 | <listitem>
|
---|
771 | <para>The string upon which to perform insertion</para>
|
---|
772 | </listitem>
|
---|
773 | </varlistentry>
|
---|
774 | <varlistentry>
|
---|
775 | <term>position</term>
|
---|
776 | <listitem>
|
---|
777 | <para>the position where insertion will be performed</para>
|
---|
778 | </listitem>
|
---|
779 | </varlistentry>
|
---|
780 | <varlistentry>
|
---|
781 | <term>with</term>
|
---|
782 | <listitem>
|
---|
783 | <para>The string to be inserted</para>
|
---|
784 | </listitem>
|
---|
785 | </varlistentry>
|
---|
786 | </variablelist>
|
---|
787 | </refparameter>
|
---|
788 | <refreturn>
|
---|
789 | <para>Returns string.</para>
|
---|
790 | </refreturn>
|
---|
791 | </doc:template>
|
---|
792 |
|
---|
793 | <xsl:template name="str:insert-at">
|
---|
794 | <xsl:param name="text"/>
|
---|
795 | <xsl:param name="position"/>
|
---|
796 | <xsl:param name="chars"/>
|
---|
797 |
|
---|
798 | <xsl:variable name="firstpart" select="substring($text, 0, $position)"/>
|
---|
799 | <xsl:variable name="secondpart" select="substring($text, $position, string-length($text))"/>
|
---|
800 |
|
---|
801 | <xsl:value-of select="concat($firstpart, $chars, $secondpart)"/>
|
---|
802 | </xsl:template>
|
---|
803 |
|
---|
804 |
|
---|
805 | <doc:template name="str:backward" xmlns="">
|
---|
806 | <refpurpose>String reversal</refpurpose>
|
---|
807 |
|
---|
808 | <refdescription>
|
---|
809 | <para>Reverse the content of a given string</para>
|
---|
810 | </refdescription>
|
---|
811 |
|
---|
812 | <refparameter>
|
---|
813 | <variablelist>
|
---|
814 | <varlistentry>
|
---|
815 | <term>text</term>
|
---|
816 | <listitem>
|
---|
817 | <para>The string to be reversed</para>
|
---|
818 | </listitem>
|
---|
819 | </varlistentry>
|
---|
820 | </variablelist>
|
---|
821 | </refparameter>
|
---|
822 |
|
---|
823 | <refreturn>
|
---|
824 | <para>Returns string.</para>
|
---|
825 | </refreturn>
|
---|
826 | </doc:template>
|
---|
827 |
|
---|
828 | <xsl:template name="str:backward">
|
---|
829 | <xsl:param name="text"/>
|
---|
830 | <xsl:variable name="mirror">
|
---|
831 | <xsl:call-template name="str:build-mirror">
|
---|
832 | <xsl:with-param name="text" select="$text"/>
|
---|
833 | <xsl:with-param name="position" select="string-length($text)"/>
|
---|
834 | </xsl:call-template>
|
---|
835 | </xsl:variable>
|
---|
836 | <xsl:value-of select="substring($mirror, string-length($text) + 1, string-length($text))"/>
|
---|
837 | </xsl:template>
|
---|
838 |
|
---|
839 | <xsl:template name="str:build-mirror">
|
---|
840 | <xsl:param name="text"/>
|
---|
841 | <xsl:param name="position"/>
|
---|
842 |
|
---|
843 | <xsl:choose>
|
---|
844 | <xsl:when test="$position > 0">
|
---|
845 | <xsl:call-template name="str:build-mirror">
|
---|
846 | <xsl:with-param name="text" select="concat($text, substring($text, $position, 1))"/>
|
---|
847 | <xsl:with-param name="position" select="$position - 1"/>
|
---|
848 | </xsl:call-template>
|
---|
849 | </xsl:when>
|
---|
850 | <xsl:otherwise>
|
---|
851 | <xsl:value-of select="$text"/>
|
---|
852 | </xsl:otherwise>
|
---|
853 | </xsl:choose>
|
---|
854 | </xsl:template>
|
---|
855 |
|
---|
856 | <doc:template name="str:justify" xmlns="">
|
---|
857 | <refpurpose>Format a string</refpurpose>
|
---|
858 |
|
---|
859 | <refdescription>
|
---|
860 | <para>Inserts newlines and spaces into a string to format it as a block of text.</para>
|
---|
861 | </refdescription>
|
---|
862 |
|
---|
863 | <refparameter>
|
---|
864 | <variablelist>
|
---|
865 | <varlistentry>
|
---|
866 | <term>text</term>
|
---|
867 | <listitem>
|
---|
868 | <para>String to be formatted.</para>
|
---|
869 | </listitem>
|
---|
870 | </varlistentry>
|
---|
871 | <varlistentry>
|
---|
872 | <term>max</term>
|
---|
873 | <listitem>
|
---|
874 | <para>Maximum line length.</para>
|
---|
875 | </listitem>
|
---|
876 | </varlistentry>
|
---|
877 | <varlistentry>
|
---|
878 | <term>indent</term>
|
---|
879 | <listitem>
|
---|
880 | <para>Number of spaces to insert at the beginning of each line.</para>
|
---|
881 | </listitem>
|
---|
882 | </varlistentry>
|
---|
883 | <varlistentry>
|
---|
884 | <term>justify</term>
|
---|
885 | <listitem>
|
---|
886 | <para>Justify left, right or both. Not currently implemented (fixed at "left").</para>
|
---|
887 | </listitem>
|
---|
888 | </varlistentry>
|
---|
889 | </variablelist>
|
---|
890 | </refparameter>
|
---|
891 |
|
---|
892 | <refreturn>
|
---|
893 | <para>Formatted block of text.</para>
|
---|
894 | </refreturn>
|
---|
895 | </doc:template>
|
---|
896 |
|
---|
897 | <xsl:template name='str:justify'>
|
---|
898 | <xsl:param name='text'/>
|
---|
899 | <xsl:param name='max' select='"80"'/>
|
---|
900 | <xsl:param name='indent' select='"0"'/>
|
---|
901 | <xsl:param name='justify' select='"left"'/>
|
---|
902 |
|
---|
903 | <xsl:choose>
|
---|
904 | <xsl:when test='string-length($text) = 0 or $max <= 0'/>
|
---|
905 |
|
---|
906 | <xsl:when test='string-length($text) > $max and contains($text, " ") and string-length(substring-before($text, " ")) > $max'>
|
---|
907 | <xsl:call-template name='str:generate-string'>
|
---|
908 | <xsl:with-param name='text' select='" "'/>
|
---|
909 | <xsl:with-param name='count' select='$indent'/>
|
---|
910 | </xsl:call-template>
|
---|
911 | <xsl:value-of select='substring-before($text, " ")'/>
|
---|
912 | <xsl:text>
|
---|
913 | </xsl:text>
|
---|
914 | <xsl:call-template name='str:justify'>
|
---|
915 | <xsl:with-param name='text' select='substring-after($text, " ")'/>
|
---|
916 | <xsl:with-param name='max' select='$max'/>
|
---|
917 | <xsl:with-param name='indent' select='$indent'/>
|
---|
918 | <xsl:with-param name='justify' select='$justify'/>
|
---|
919 | </xsl:call-template>
|
---|
920 | </xsl:when>
|
---|
921 |
|
---|
922 | <xsl:when test='string-length($text) > $max and contains($text, " ")'>
|
---|
923 | <xsl:variable name='first'>
|
---|
924 | <xsl:call-template name='str:substring-before-last'>
|
---|
925 | <xsl:with-param name='text' select='substring($text, 1, $max)'/>
|
---|
926 | <xsl:with-param name='chars' select='" "'/>
|
---|
927 | </xsl:call-template>
|
---|
928 | </xsl:variable>
|
---|
929 |
|
---|
930 | <xsl:call-template name='str:generate-string'>
|
---|
931 | <xsl:with-param name='text' select='" "'/>
|
---|
932 | <xsl:with-param name='count' select='$indent'/>
|
---|
933 | </xsl:call-template>
|
---|
934 | <xsl:value-of select='$first'/>
|
---|
935 | <xsl:text>
|
---|
936 | </xsl:text>
|
---|
937 | <xsl:call-template name='str:justify'>
|
---|
938 | <xsl:with-param name='text' select='substring($text, string-length($first) + 2)'/>
|
---|
939 | <xsl:with-param name='max' select='$max'/>
|
---|
940 | <xsl:with-param name='indent' select='$indent'/>
|
---|
941 | <xsl:with-param name='justify' select='$justify'/>
|
---|
942 | </xsl:call-template>
|
---|
943 | </xsl:when>
|
---|
944 |
|
---|
945 | <xsl:otherwise>
|
---|
946 | <xsl:call-template name='str:generate-string'>
|
---|
947 | <xsl:with-param name='text' select='" "'/>
|
---|
948 | <xsl:with-param name='count' select='$indent'/>
|
---|
949 | </xsl:call-template>
|
---|
950 | <xsl:value-of select='$text'/>
|
---|
951 | </xsl:otherwise>
|
---|
952 | </xsl:choose>
|
---|
953 | </xsl:template>
|
---|
954 |
|
---|
955 | <doc:template name="str:character-first" xmlns="">
|
---|
956 | <refpurpose>Find first occurring character in a string</refpurpose>
|
---|
957 |
|
---|
958 | <refdescription>
|
---|
959 | <para>Finds which of the given characters occurs first in a string.</para>
|
---|
960 | </refdescription>
|
---|
961 |
|
---|
962 | <refparameter>
|
---|
963 | <variablelist>
|
---|
964 | <varlistentry>
|
---|
965 | <term>text</term>
|
---|
966 | <listitem>
|
---|
967 | <para>The source string.</para>
|
---|
968 | </listitem>
|
---|
969 | </varlistentry>
|
---|
970 | <varlistentry>
|
---|
971 | <term>chars</term>
|
---|
972 | <listitem>
|
---|
973 | <para>The characters to search for.</para>
|
---|
974 | </listitem>
|
---|
975 | </varlistentry>
|
---|
976 | </variablelist>
|
---|
977 | </refparameter>
|
---|
978 | </doc:template>
|
---|
979 |
|
---|
980 | <xsl:template name="str:character-first">
|
---|
981 | <xsl:param name="text"/>
|
---|
982 | <xsl:param name="chars"/>
|
---|
983 |
|
---|
984 | <xsl:choose>
|
---|
985 | <xsl:when test="string-length($text) = 0 or string-length($chars) = 0"/>
|
---|
986 |
|
---|
987 | <xsl:when test="contains($text, substring($chars, 1, 1))">
|
---|
988 | <xsl:variable name="next-character">
|
---|
989 | <xsl:call-template name="str:character-first">
|
---|
990 | <xsl:with-param name="text" select="$text"/>
|
---|
991 | <xsl:with-param name="chars" select="substring($chars, 2)"/>
|
---|
992 | </xsl:call-template>
|
---|
993 | </xsl:variable>
|
---|
994 |
|
---|
995 | <xsl:choose>
|
---|
996 | <xsl:when test="string-length($next-character)">
|
---|
997 | <xsl:variable name="first-character-position" select="string-length(substring-before($text, substring($chars, 1, 1)))"/>
|
---|
998 | <xsl:variable name="next-character-position" select="string-length(substring-before($text, $next-character))"/>
|
---|
999 |
|
---|
1000 | <xsl:choose>
|
---|
1001 | <xsl:when test="$first-character-position < $next-character-position">
|
---|
1002 | <xsl:value-of select="substring($chars, 1, 1)"/>
|
---|
1003 | </xsl:when>
|
---|
1004 | <xsl:otherwise>
|
---|
1005 | <xsl:value-of select="$next-character"/>
|
---|
1006 | </xsl:otherwise>
|
---|
1007 | </xsl:choose>
|
---|
1008 | </xsl:when>
|
---|
1009 | <xsl:otherwise>
|
---|
1010 | <xsl:value-of select="substring($chars, 1, 1)"/>
|
---|
1011 | </xsl:otherwise>
|
---|
1012 | </xsl:choose>
|
---|
1013 | </xsl:when>
|
---|
1014 | <xsl:otherwise>
|
---|
1015 | <xsl:call-template name="str:character-first">
|
---|
1016 | <xsl:with-param name="text" select="$text"/>
|
---|
1017 | <xsl:with-param name="chars" select="substring($chars, 2)"/>
|
---|
1018 | </xsl:call-template>
|
---|
1019 | </xsl:otherwise>
|
---|
1020 | </xsl:choose>
|
---|
1021 | </xsl:template>
|
---|
1022 |
|
---|
1023 | <doc:template name="str:string-match" xmlns="">
|
---|
1024 | <refpurpose>Match A String To A Pattern</refpurpose>
|
---|
1025 |
|
---|
1026 | <refdescription>
|
---|
1027 | <para>Performs globbing-style pattern matching on a string.</para>
|
---|
1028 | </refdescription>
|
---|
1029 |
|
---|
1030 | <example>
|
---|
1031 | <title>Match Pattern</title>
|
---|
1032 | <programlisting><![CDATA[
|
---|
1033 | <xsl:call-template name="str:string-match">
|
---|
1034 | <xsl:with-param name="text" select="$mytext"/>
|
---|
1035 | <xsl:with-param name="pattern" select="'abc*def?g'"/>
|
---|
1036 | </xsl:call-template>
|
---|
1037 | ]]></programlisting>
|
---|
1038 | </example>
|
---|
1039 |
|
---|
1040 | <refparameter>
|
---|
1041 | <variablelist>
|
---|
1042 | <varlistentry>
|
---|
1043 | <term>text</term>
|
---|
1044 | <listitem>
|
---|
1045 | <para>The source string.</para>
|
---|
1046 | </listitem>
|
---|
1047 | </varlistentry>
|
---|
1048 | <varlistentry>
|
---|
1049 | <term>pattern</term>
|
---|
1050 | <listitem>
|
---|
1051 | <para>The pattern to match against. Certain characters have special meaning:</para>
|
---|
1052 | <variablelist>
|
---|
1053 | <varlistentry>
|
---|
1054 | <term>*</term>
|
---|
1055 | <listitem>
|
---|
1056 | <para>Matches zero or more characters.</para>
|
---|
1057 | </listitem>
|
---|
1058 | </varlistentry>
|
---|
1059 | <varlistentry>
|
---|
1060 | <term>?</term>
|
---|
1061 | <listitem>
|
---|
1062 | <para>Matches a single character.</para>
|
---|
1063 | </listitem>
|
---|
1064 | </varlistentry>
|
---|
1065 | <varlistentry>
|
---|
1066 | <term>\</term>
|
---|
1067 | <listitem>
|
---|
1068 | <para>Character escape. The next character is taken as a literal character.</para>
|
---|
1069 | </listitem>
|
---|
1070 | </varlistentry>
|
---|
1071 | </variablelist>
|
---|
1072 | </listitem>
|
---|
1073 | </varlistentry>
|
---|
1074 | </variablelist>
|
---|
1075 | </refparameter>
|
---|
1076 |
|
---|
1077 | <refreturn>
|
---|
1078 | <para>Returns "1" if the string matches the pattern, "0" otherwise.</para>
|
---|
1079 | </refreturn>
|
---|
1080 | </doc:template>
|
---|
1081 |
|
---|
1082 | <xsl:template name="str:string-match">
|
---|
1083 | <xsl:param name="text"/>
|
---|
1084 | <xsl:param name="pattern"/>
|
---|
1085 |
|
---|
1086 | <xsl:choose>
|
---|
1087 | <xsl:when test="$pattern = '*'">
|
---|
1088 | <!-- Special case: always matches -->
|
---|
1089 | <xsl:text>1</xsl:text>
|
---|
1090 | </xsl:when>
|
---|
1091 | <xsl:when test="string-length($text) = 0 and string-length($pattern) = 0">
|
---|
1092 | <xsl:text>1</xsl:text>
|
---|
1093 | </xsl:when>
|
---|
1094 | <xsl:when test="string-length($text) = 0 or string-length($pattern) = 0">
|
---|
1095 | <xsl:text>0</xsl:text>
|
---|
1096 | </xsl:when>
|
---|
1097 | <xsl:otherwise>
|
---|
1098 | <xsl:variable name='before-special'>
|
---|
1099 | <xsl:call-template name='str:substring-before-first'>
|
---|
1100 | <xsl:with-param name='text' select='$pattern'/>
|
---|
1101 | <xsl:with-param name='chars' select='"*?\"'/>
|
---|
1102 | </xsl:call-template>
|
---|
1103 | </xsl:variable>
|
---|
1104 | <xsl:variable name='special'>
|
---|
1105 | <xsl:call-template name='str:character-first'>
|
---|
1106 | <xsl:with-param name='text' select='$pattern'/>
|
---|
1107 | <xsl:with-param name='chars' select='"*?\"'/>
|
---|
1108 | </xsl:call-template>
|
---|
1109 | </xsl:variable>
|
---|
1110 |
|
---|
1111 | <xsl:variable name='new-text' select='substring($text, string-length($before-special) + 1)'/>
|
---|
1112 | <xsl:variable name='new-pattern' select='substring($pattern, string-length($before-special) + 1)'/>
|
---|
1113 |
|
---|
1114 | <xsl:choose>
|
---|
1115 | <xsl:when test="not(starts-with($text, $before-special))">
|
---|
1116 | <!-- Verbatim characters don't match -->
|
---|
1117 | <xsl:text>0</xsl:text>
|
---|
1118 | </xsl:when>
|
---|
1119 |
|
---|
1120 | <xsl:when test="$special = '*' and string-length($new-pattern) = 1">
|
---|
1121 | <xsl:text>1</xsl:text>
|
---|
1122 | </xsl:when>
|
---|
1123 | <xsl:when test="$special = '*'">
|
---|
1124 | <xsl:call-template name='str:match-postfix'>
|
---|
1125 | <xsl:with-param name='text' select='$new-text'/>
|
---|
1126 | <xsl:with-param name='pattern' select='substring($new-pattern, 2)'/>
|
---|
1127 | </xsl:call-template>
|
---|
1128 | </xsl:when>
|
---|
1129 |
|
---|
1130 | <xsl:when test="$special = '?'">
|
---|
1131 | <xsl:call-template name="str:string-match">
|
---|
1132 | <xsl:with-param name='text' select='substring($new-text, 2)'/>
|
---|
1133 | <xsl:with-param name='pattern' select='substring($new-pattern, 2)'/>
|
---|
1134 | </xsl:call-template>
|
---|
1135 | </xsl:when>
|
---|
1136 |
|
---|
1137 | <xsl:when test="$special = '\' and substring($new-text, 1, 1) = substring($new-pattern, 2, 1)">
|
---|
1138 | <xsl:call-template name="str:string-match">
|
---|
1139 | <xsl:with-param name='text' select='substring($new-text, 2)'/>
|
---|
1140 | <xsl:with-param name='pattern' select='substring($new-pattern, 3)'/>
|
---|
1141 | </xsl:call-template>
|
---|
1142 | </xsl:when>
|
---|
1143 | <xsl:when test="$special = '\' and substring($new-text, 1, 1) != substring($new-pattern, 2, 1)">
|
---|
1144 | <xsl:text>0</xsl:text>
|
---|
1145 | </xsl:when>
|
---|
1146 |
|
---|
1147 | <xsl:otherwise>
|
---|
1148 | <!-- There were no special characters in the pattern -->
|
---|
1149 | <xsl:choose>
|
---|
1150 | <xsl:when test='$text = $pattern'>
|
---|
1151 | <xsl:text>1</xsl:text>
|
---|
1152 | </xsl:when>
|
---|
1153 | <xsl:otherwise>
|
---|
1154 | <xsl:text>0</xsl:text>
|
---|
1155 | </xsl:otherwise>
|
---|
1156 | </xsl:choose>
|
---|
1157 | </xsl:otherwise>
|
---|
1158 | </xsl:choose>
|
---|
1159 | </xsl:otherwise>
|
---|
1160 | </xsl:choose>
|
---|
1161 | </xsl:template>
|
---|
1162 |
|
---|
1163 | <xsl:template name="str:match-postfix">
|
---|
1164 | <xsl:param name="text"/>
|
---|
1165 | <xsl:param name="pattern"/>
|
---|
1166 |
|
---|
1167 | <xsl:variable name='result'>
|
---|
1168 | <xsl:call-template name='str:string-match'>
|
---|
1169 | <xsl:with-param name='text' select='$text'/>
|
---|
1170 | <xsl:with-param name='pattern' select='$pattern'/>
|
---|
1171 | </xsl:call-template>
|
---|
1172 | </xsl:variable>
|
---|
1173 |
|
---|
1174 | <xsl:choose>
|
---|
1175 | <xsl:when test='$result = "1"'>
|
---|
1176 | <xsl:value-of select='$result'/>
|
---|
1177 | </xsl:when>
|
---|
1178 | <xsl:when test='string-length($text) = 0'>
|
---|
1179 | <xsl:text>0</xsl:text>
|
---|
1180 | </xsl:when>
|
---|
1181 | <xsl:otherwise>
|
---|
1182 | <xsl:call-template name='str:match-postfix'>
|
---|
1183 | <xsl:with-param name='text' select='substring($text, 2)'/>
|
---|
1184 | <xsl:with-param name='pattern' select='$pattern'/>
|
---|
1185 | </xsl:call-template>
|
---|
1186 | </xsl:otherwise>
|
---|
1187 | </xsl:choose>
|
---|
1188 |
|
---|
1189 | </xsl:template>
|
---|
1190 |
|
---|
1191 | <doc:template name="str:generate-string" xmlns="">
|
---|
1192 | <refpurpose>Create A Repeating Sequence of Characters</refpurpose>
|
---|
1193 |
|
---|
1194 | <refdescription>
|
---|
1195 | <para>Repeats a string a given number of times.</para>
|
---|
1196 | </refdescription>
|
---|
1197 |
|
---|
1198 | <refparameter>
|
---|
1199 | <variablelist>
|
---|
1200 | <varlistentry>
|
---|
1201 | <term>text</term>
|
---|
1202 | <listitem>
|
---|
1203 | <para>The string to repeat.</para>
|
---|
1204 | </listitem>
|
---|
1205 | </varlistentry>
|
---|
1206 | <varlistentry>
|
---|
1207 | <term>count</term>
|
---|
1208 | <listitem>
|
---|
1209 | <para>The number of times to repeat the string.</para>
|
---|
1210 | </listitem>
|
---|
1211 | </varlistentry>
|
---|
1212 | </variablelist>
|
---|
1213 | </refparameter>
|
---|
1214 | </doc:template>
|
---|
1215 |
|
---|
1216 | <xsl:template name="str:generate-string">
|
---|
1217 | <xsl:param name="text"/>
|
---|
1218 | <xsl:param name="count"/>
|
---|
1219 |
|
---|
1220 | <xsl:choose>
|
---|
1221 | <xsl:when test="string-length($text) = 0 or $count <= 0"/>
|
---|
1222 | <xsl:otherwise>
|
---|
1223 | <xsl:value-of select="$text"/>
|
---|
1224 | <xsl:call-template name="str:generate-string">
|
---|
1225 | <xsl:with-param name="text" select="$text"/>
|
---|
1226 | <xsl:with-param name="count" select="$count - 1"/>
|
---|
1227 | </xsl:call-template>
|
---|
1228 | </xsl:otherwise>
|
---|
1229 | </xsl:choose>
|
---|
1230 | </xsl:template>
|
---|
1231 |
|
---|
1232 | </xsl:stylesheet>
|
---|
1233 |
|
---|