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