-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.xsl
1442 lines (1286 loc) · 64.2 KB
/
functions.xsl
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="#all"
version="2.0"
xmlns:functx="http://www.functx.com"
xmlns:snac="http://socialarchive.iath.virginia.edu/"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Author: Tom Laudeman, Daniel Pitti
The Institute for Advanced Technology in the Humanities
Copyright 2013 University of Virginia. Licensed under the Educational Community License, Version 2.0
(the "License"); you may not use this file except in compliance with the License. You may obtain a
copy of the License at
http://www.osedu.org/licenses/ECL-2.0
http://opensource.org/licenses/ECL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and limitations under the
License.
This file contains functions used by eadToCpf.xsl (and related XSL code.)
-->
<!--
snac:css-style('underline') returns a string compatible with a style="" attribute.
-->
<xsl:function name="snac:css-style">
<xsl:param name="arg"/>
<xsl:choose>
<xsl:when test="$arg ='underline'">
<xsl:text>text-decoration: underline;</xsl:text>
</xsl:when>
<xsl:when test="$arg = 'italic'">
<xsl:text>font-style: italic;</xsl:text>
</xsl:when>
<xsl:when test="$arg = 'super'">
<xsl:text>vertical-align: super;</xsl:text>
</xsl:when>
<xsl:when test="$arg = 'bold'">
<xsl:text>font-weight: bold;</xsl:text>
</xsl:when>
<xsl:when test="$arg = 'bolditalic'">
<!--
Yes, it is real. render="bolditalic"
/data/source/findingAids/cjh/ajhs-ead-snac/109173_NoahBenevolentSociety.xml
-->
<xsl:text>font-weight: bold;</xsl:text>
<xsl:text> font-style: italic;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('/* unknown ', $arg , '*/')"/>
<xsl:message>
<xsl:value-of select="concat('Warning: unknown style: ', $arg, $cr)"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!--
Parse a name and return the name portion in direct order.
I'm guessing that \p{L} is match Unicode letter
http://www.regular-expressions.info/unicode.html
http://stackoverflow.com/questions/14891129/regular-expression-pl-and-pn
-->
<xsl:function name="snac:directPersnameOne">
<xsl:param as="xs:string" name="tempString"/>
<xsl:choose>
<xsl:when test="(contains($tempString,',')) and (matches($tempString,'^[\p{L}]'))">
<xsl:analyze-string flags="x"
regex="
^
( ([\p{{L}}]+\.?[\-'\s]?)+ )
(,\s)?
(([\p{{L}}]+\.?[\-'\s]?)*)
(\( (([\p{{L}}]+\.?[\-'\s]?)+) \))?
(.*?)
"
select="normalize-space($tempString)">
<xsl:matching-substring>
<xsl:variable name="buildString">
<xsl:value-of select="regex-group(4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="regex-group(1)"/>
<!--xsl:text>::1::</xsl:text>
<xsl:value-of select="regex-group(1)"/>
<xsl:text>::2::</xsl:text>
<xsl:value-of select="regex-group(2)"/>
<xsl:text>::3::</xsl:text>
<xsl:value-of select="regex-group(3)"/>
<xsl:text>::4::</xsl:text>
<xsl:value-of select="regex-group(4)"/>
<xsl:text>::5::</xsl:text>
<xsl:value-of select="regex-group(5)"/>
<xsl:text>::6::</xsl:text>
<xsl:value-of select="regex-group(6)"/>
<xsl:text>::7::</xsl:text>
<xsl:value-of select="regex-group(7)"/>
<xsl:text>::8::</xsl:text>
<xsl:value-of select="regex-group(8)"/>
<xsl:text>::9::</xsl:text>
<xsl:value-of select="regex-group(9)"/-->
</xsl:variable>
<xsl:choose>
<!--
I do not think this first when test actually works.
-->
<xsl:when test="normalize-space($buildString)=''">
<xsl:value-of select="normalize-space($tempString)"/>
<xsl:message>
<xsl:text>Look in the snac:directPersnameOne function.</xsl:text>
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($buildString)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring> </xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"/>
</xsl:otherwise>
<!-- if no , then what? -->
</xsl:choose>
</xsl:function>
<!--
Parse a name and return the name portion in direct order. Seems a tiny bit more robust than "one" above.
I'm guessing that \p{L} is match Unicode letter
http://www.regular-expressions.info/unicode.html
http://stackoverflow.com/questions/14891129/regular-expression-pl-and-pn
-->
<xsl:function name="snac:directPersnameTwo">
<xsl:param as="xs:string" name="tempString"/>
<xsl:choose>
<xsl:when test="(contains($tempString,',')) and (matches($tempString,'^[\p{L}]'))">
<xsl:analyze-string flags="x"
regex="
^
( ([\p{{L}}]+\.?[\-'\s]?)+ )
(,\s)?
(([\p{{L}}]+\.?[\-'\s]?)*)
(\( (([\p{{L}}]+\.?[\-'\s]?)+) \))?
(.*?)
"
select="normalize-space($tempString)">
<xsl:matching-substring>
<xsl:variable name="buildString">
<xsl:choose>
<xsl:when test="regex-group(7)">
<xsl:value-of select="regex-group(7)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="regex-group(4)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text>
<xsl:value-of select="regex-group(1)"/>
<!--xsl:text>::1::</xsl:text>
<xsl:value-of select="regex-group(1)"/>
<xsl:text>::2::</xsl:text>
<xsl:value-of select="regex-group(2)"/>
<xsl:text>::3::</xsl:text>
<xsl:value-of select="regex-group(3)"/>
<xsl:text>::4::</xsl:text>
<xsl:value-of select="regex-group(4)"/>
<xsl:text>::5::</xsl:text>
<xsl:value-of select="regex-group(5)"/>
<xsl:text>::6::</xsl:text>
<xsl:value-of select="regex-group(6)"/>
<xsl:text>::7::</xsl:text>
<xsl:value-of select="regex-group(7)"/>
<xsl:text>::8::</xsl:text>
<xsl:value-of select="regex-group(8)"/>
<xsl:text>::9::</xsl:text>
<xsl:value-of select="regex-group(9)"/-->
</xsl:variable>
<xsl:choose>
<!-- I do not think this first when test actually works. -->
<xsl:when test="normalize-space($buildString)=''">
<xsl:value-of select="normalize-space($tempString)"/>
<xsl:message>
<xsl:text>Look in the snac:directPersnameTwo function.</xsl:text>
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($buildString)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"/>
</xsl:otherwise>
<!-- if no, then what? -->
</xsl:choose>
</xsl:function>
<xsl:function name="snac:getDateFromUnitdate">
<!-- This function dates a string from a unitdate and returns a string with just numbers in it;
When called, it is tokenized an only the four digital numbers (years) are used. -->
<xsl:param as="xs:string" name="tempString"/>
<xsl:variable name="dateNumbersOne">
<xsl:value-of select="normalize-space(replace($tempString,'[^\d\-]',' '))"/>
</xsl:variable>
<xsl:variable name="dateNumbersTwo">
<!-- Complete dates that are of the following pattern: 1848-51; change to 1848-1851 -->
<xsl:choose>
<xsl:when test="matches($dateNumbersOne,'^[\d]{4}-[\d]{2}$')">
<xsl:variable name="century">
<xsl:value-of select="substring($dateNumbersOne,1,2)"/>
</xsl:variable>
<xsl:value-of select="normalize-space(substring-before($dateNumbersOne,'-'))"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$century"/>
<xsl:value-of select="normalize-space(substring-after($dateNumbersOne,'-'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(replace($dateNumbersOne,'-',' '))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$dateNumbersTwo"/>
</xsl:function>
<xsl:function name="snac:testDate">
<xsl:param name="tempString" as="xs:string"/>
<xsl:choose>
<xsl:when test="number($tempString) = number($tempString)">
<xsl:choose>
<xsl:when test="number($tempString) > 2099">
<xsl:value-of select="boolean(0)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean(1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean(0)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!--
Perhaps specialized. Replace ' . ' with ' '
<relationEntry>Pierre-Jules Hetzel, . Papiers.. NAF 16932-17152., XIXe s.</relationEntry>
-->
<xsl:function name="snac:removeFloatingDot">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, '(\s+\.\s+)', ' ')"/>
</xsl:function>
<!--
Perhaps specialized. Replace ., with '. '
<relationEntry>Pierre-Jules Hetzel, . Papiers.. NAF 16932-17152., XIXe s.</relationEntry>
-->
<xsl:function name="snac:dotComma2Dot">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, '(\.,\s*)', '. ')"/>
</xsl:function>
<!--
Clean . with zero or more whitespace and as many of those as possible, replace with a single '. '
<relationEntry>Pierre-Jules Hetzel, . Papiers.. NAF 16932-17152., XIXe s.</relationEntry>
-->
<xsl:function name="snac:removeDoubleDot">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, '(\.\s*)+', '. ')"/>
</xsl:function>
<!--
For cleaning up "Corporation, , 1901-2001" as well as "Corporation, ," and "Bowen,, Richard, "
-->
<xsl:function name="snac:removeDoubleComma">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, '(,\s*)+', ', ')"/>
</xsl:function>
<!--
Trim off leading whitespace and comma, any number in any combination.
-->
<xsl:function name="snac:removeLeadingComma">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, '^[\s,]+', '')"/>
</xsl:function>
<xsl:function name="snac:removeFinalComma">
<xsl:param name="tempString" as="xs:string"/>
<xsl:choose>
<xsl:when test="ends-with(normalize-space($tempString),',') or ends-with(normalize-space($tempString),';')">
<xsl:value-of select="substring(normalize-space($tempString),1,(string-length(normalize-space($tempString))-1))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($tempString)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="snac:countTokens">
<!-- This function merely counts the number of tokens in a string. -->
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="count(tokenize(normalize-space(snac:removePunctuation($tempString)),'\s'))"/>
</xsl:function>
<xsl:function name="snac:escape-for-regex" as="xs:string">
<xsl:param name="arg" as="xs:string?"/>
<xsl:variable name="var">
<xsl:sequence
select="
replace($arg,
'(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
"
/>
</xsl:variable>
<xsl:value-of select="replace($var, '/', '/')"/>
</xsl:function>
<xsl:function name="snac:path-minus-base">
<xsl:param name="base" as="xs:string"/>
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString, concat('^.*', $base, '/(.*)/.*?$'),'$1','i')"/>
</xsl:function>
<xsl:function name="snac:getBaseIdName">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString,'(.*)(\.xml)','$1','i')"/>
</xsl:function>
<xsl:function name="snac:getFileName">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="replace($tempString,'(.*/)(.*\.xml)','$2','i')"/>
</xsl:function>
<xsl:function name="snac:testYearDate">
<xsl:param name="tempString" as="xs:string"/>
<xsl:choose>
<xsl:when test="number($tempString) = number($tempString)">
<xsl:choose>
<xsl:when test="number($tempString) > 2099">
<xsl:value-of select="boolean(0)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean(1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean(0)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="snac:getDateFromPersname">
<xsl:param as="xs:string" name="tempString"/>
<!--
Jan 5 2015 Switch this over to bfl/dfl, etc. as in other date code, and other character classes
for flourished and circa.
May 12 2015 This won't parse (nnnn-nnnn) apparently the parens cause problems. So we clean them up
here because cleaning in the calling code could break something. Cleaning is safer than trying to
upgrade the regex.
-->
<xsl:variable name="clean_string" select="replace($tempString, '\(|\)', '')"/>
<xsl:choose>
<xsl:when
test="matches($clean_string, '(^.+?\s)
(
(([fl\.]*\s*) ([ca\.]*\s*)[\d]{3,4}\??\- ([fl\.]*\s*) ([ca\.]*\s*) [\d]{3,4}\??)
|
(([fl\.]*\s*) ([ca\.]*\s) [\d]{3,4}\??\-)
|
(([bdfl\.]*\s*) ([ca\.]*\s*)[\d]{3,4}\??)
)
($|([\D].*$))'
,
'x')">
<xsl:choose>
<xsl:when test="matches($clean_string,
'(^.+?\s) ((fl\.?\s*)? (ca?\.?\s*)?[\d]{3,4}\??\- (fl\.?\s*)? (ca?\.?\s*)? [\d]{3,4}\??)($|([\D].*$))', 'x')">
<xsl:value-of
select="replace($clean_string,
'(^.+?\s) ((fl\.?\s*)? (ca?\.?\s*)?[\d]{3,4}\??\- (fl\.?\s*)? (ca?\.?\s*)? [\d]{3,4}\??)($|([\D].*$))'
,'$2', 'x')"/>
</xsl:when>
<xsl:when test="matches($clean_string,
'(^.+?\s) ((fl\.?\s*)? (ca?\.?\s*)? [\d]{3,4}\??\-)($|([\D].*$))', 'x')">
<xsl:value-of
select="replace($clean_string,
'(^.+?\s) ((fl\.?\s*)? (ca?\.?\s*)? [\d]{3,4}\??\-)($|([\D].*$))'
,'$2', 'x')"/>
</xsl:when>
<xsl:when test="matches($clean_string,
'(^.+?\s) (([bdfl\.]*\s*) (ca?\.?\s*)?[\d]{3,4}\??) ($|([\D].*$))', 'x')">
<xsl:value-of
select="replace($clean_string,
'(^.+?\s) (([bdfl\.]*\s*) (ca?\.?\s*)?[\d]{3,4}\??) ($|([\D].*$))', '$2', 'x')"/>
</xsl:when>
<xsl:otherwise>
<!--
It is unclear how this code will ever execute. The top matches() has to hit first, and
if so, then one of the inner matches() must hit as well, thus this code never runs. If
you want to keep this, maybe a log message would be good.
-->
<xsl:value-of select="$clean_string"/>
</xsl:otherwise>
<!-- The original regex worked fine until a bug in Saxon stopped it from parsing. -->
<!-- <xsl:value-of -->
<!-- select="replace($clean_string,' -->
<!-- (^.+?\s) -->
<!-- ( -->
<!-- ((fl\.\s)? (ca\.\s)?[\d]{3,4}\??\- (fl\.\s)? (ca\.\s)? [\d]{3,4}\??) -->
<!-- | -->
<!-- ((fl\.\s)? (ca\.\s)? [\d]{3,4}\??\-) -->
<!-- | -->
<!-- ((([b|d]\.\s)|(fl\.\s))(ca\.\s)?[\d]{3,4}\??) -->
<!-- ) -->
<!-- ( -->
<!-- $ -->
<!-- | -->
<!-- ([\D].*$) -->
<!-- ) -->
<!-- ','$2','x')" -->
<!-- /> -->
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text>0</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:function> <!-- snac:getDateFromPersname -->
<xsl:function name="snac:stripStringAfterDateInPersname">
<xsl:param as="xs:string" name="tempString"/>
<!--
This is not code that strips "\-\-Correspondence." from the ends of names. See snac:removeBeforeHyphen2().
Note that this uses simplfied character classes [bdfl\.?] and [ca\.] which seems to work well. It
isn't as specific as some other uses, but is much simplier. Matching too much has not (yet) been a
bug in this code. And it fixed several other bugs where the old code failed to match common date
formats.
Dec 18 2014 Don't do anything if there are parens around any part of the date. In this case other
code was unable to recognize a date, so we should not do any cleaning.
Dec 17 2014 Mod 3rd when to match "fl" and not just "fl.".
Problem with strings that have internal newline:
<persname encodinganalog="700">Grigsby, Hugh Blair, 1806-
1881.</persname>
Break the replace() into 3 passes because the single large regexp (below) crashes saxon
with a java exception.
java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:686)
at net.sf.saxon.regex.BMPString.charAt(BMPString.java:33)
at net.sf.saxon.regex.REMatcher.subst(REMatcher.java:755)
at net.sf.saxon.regex.ARegularExpression.replace(ARegularExpression.java:123)
at net.sf.saxon.functions.Replace.eval(Replace.java:209)
at net.sf.saxon.functions.Replace.evaluateItem(Replace.java:123)
at net.sf.saxon.functions.Replace.evaluateItem(Replace.java:27)
...
-->
<xsl:variable name="result">
<xsl:choose>
<xsl:when test="matches($tempString, '\(.*\d+.*\)')">
<!--
Dec 18 2014 New sanity check.
We found a number inside parens. Don't modify $tempString. Might be like this:
Kipling, Charlotte. ( b.1919 d.1992)
/data/source/findingAids/ahub/f_6946.xml
-->
<xsl:value-of select="$tempString"/>
</xsl:when>
<!--
Change "\- " to "\s*\-\s*" because we have cases with multiple whitespace around the hyphen.
Use flag 'm' so that $ means end of line and not newline.
This used to say: "[\D].*$" which says match a non-digit then zero or more of any
character. That means lines with a non-digit followed by a date can be
truncated. Fixed. Original code at the end of the function.
fn: /data/source/findingAids/ahub/f_20516.xml
orig:Bell, (Arthur) Clive (Heward), 1881-1964; Bell, Vanessa, 1879-1961; Jackson, Maria, d. 1891?
stripStringAfterDateInPersname: result:Bell, (Arthur) Clive (Heward), 1881-1964
-->
<xsl:when test="matches($tempString,
'(^.+?\s) ([bfl\.\?]*\s*[ca\.]*\s*\d{3,4}[\s\?]*\-[\s\?]*[dfl\.]*\s* [ca\.]*\s*\d{3,4}\?*)($|([\D]+$))', 'xm')">
<!--
Dec 15 2014 Add alternation c\.*\s* with each ca.\s in this block so we match dates c
nnnnn which were not being matched, and resulted in the death date being lost.
/data/source/findingAids/ahub/f_4188.xml
fixDatesRemoveParens result: Robert Valentine, 1674-c. 1735, composer
normalFinal: Valentine, Robert., 1647- type: regExed cpf: persname
In pass1 we clean up an whitespace following the hyphen, but conservatively only for the
simply case of 3 or 4 digits.
In pass2, $2 is the whole date: '1806- 1881' (yes it can be hyphen space)
Use flag 'm' so that $ means end of line and not newline.
-->
<xsl:variable name="pass1">
<xsl:value-of
select="replace($tempString, '([\d]{3,4})\s*-\s*([\d]{3,4})', '$1-$2', 'm')"/>
</xsl:variable>
<xsl:variable name="pass2">
<!--
The Java regex engine throws an exception if the regex gets any longer, so we have
to break up our cleaning into two expressions. The first is b nnnn - d|fl nnn and
the second is fl nnnn - d|fl nnnn. The difference being is the first date born or
flourished. Doing an alternation pushed Java's regex engine over the edge.
-->
<xsl:value-of
select="replace($pass1,
'(^.+?\s) ([flb\.\?]*\s*([ca\.]*\s*)*[\d]{3,4}\??\s* \- \s*[fld\.\?]*\s*[ca\.]*\s*[\d]{3,4}\??)($|([\D]+$))',
'$1$2',
'xm')"/>
</xsl:variable>
<xsl:value-of select="$pass2"/>
</xsl:when>
<xsl:when test="matches($tempString,
'(^.+?\s)([bfl\.\s\?]* (ca\.\s)? [\d]{3,4}\??\-)($|([\D]+$))', 'x')">
<!--
Dec 19 2014 Modify to [bfl\.\s\?]* which matches other code and seems to work better.
Note: this has a trailing - hyphen, therefore only born/flourished date is valid.
At the end of $tempString (the name string) the old "[\D].*$" matches "d. 1842" which
is part of a date, thus the old code truncated good dates of that format. The
modification above (matches()) and below (replace()) allows no digits following the -
hyphen.
The original regex is below in a test="false()" code block.
-->
<xsl:value-of
select="replace($tempString,
'(^.+?\s)([bfl\.\s\?]* (ca\.\s)? [\d]{3,4}\??\-)($|([\D]+$))','$1$2', 'x')"/>
</xsl:when>
<xsl:when test="matches($tempString,
'(^.+?\s)([bdfl\.\s]*[ca\.\s]*?[\d]{3,4}\??)($|([\D]+$))', 'x')">
<!--
Dec 18 2014 Change the end of line test to not match or truncate anything with digits. Be conservative
Note: There is no trailing - hyphen, so this could be born/died/flourished date, thus
the character class is [bdfl] and not [bfl] or [dfl].
This is what used to happen:
orig: Brady, Mathew B., 1823 (ca.)-1896.
final: Brady, Mathew B., 1823
-->
<xsl:value-of
select="replace($tempString,
'(^.+?\s)([bdfl\.\s]*[ca\.\s]*?[\d]{3,4}\??)($|([\D]+$))','$1$2', 'x')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:message>
<xsl:text>stripStringAfterDateInPersname: orig:</xsl:text>
<xsl:value-of select="concat($tempString, $cr)"/>
<xsl:text>stripStringAfterDateInPersname: result:</xsl:text>
<xsl:value-of select="$result"/>
</xsl:message>
<xsl:value-of select="$result"/>
<xsl:if test="false()">
<!--
This is the old code (disabled by false()) which worked just fine, but ran afoul of a bug in
newer versions Saxon that threw the exception noted above.
-->
<xsl:choose>
<xsl:when
test="matches($tempString,'
(^.+?\s)
(
((fl\.\s)? (ca\.\s)?[\d]{3,4}\??\- (fl\.\s)? (ca\.\s)? [\d]{3,4}\??)
|
((fl\.\s)? (ca\.\s)? [\d]{3,4}\??\-)
|
((([b|d]\.\s)|(fl\.\s))(ca\.\s)?[\d]{3,4}\??)
)
(
$
|
([\D].*$)
)
','x')">
<xsl:value-of
select="replace($tempString,'
(^.+?\s)
(
((fl\.\s)? (ca\.\s)?[\d]{3,4}\??\- (fl\.\s)? (ca\.\s)? [\d]{3,4}\??)
|
((fl\.\s)? (ca\.\s)? [\d]{3,4}\??\-)
|
((([b|d]\.\s)|(fl\.\s))(ca\.\s)?[\d]{3,4}\??)
)
(
$
|
([\D].*$)
)
','$1$2','x')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:function>
<xsl:function name="snac:removeInitialNonWord">
<xsl:param name="tempString"/>
<xsl:choose>
<xsl:when test="matches($tempString,'
^
([\W]+)
(.*)
$
','x')">
<xsl:value-of select="normalize-space(replace($tempString,'
^
([\W]+)
(.*)
$
','$2','x'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!--
<xsl:function name="snac:stripStringAfterDateInPersname">
<xsl:param as="xs:string" name="tempString"/>
<xsl:choose>
<xsl:when test="matches($tempString,'(,?\s?([b|d]\.\s)?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})\??\-?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})?\??)')">
<xsl:value-of
select="replace($tempString,'^(.*?)(([b|d]\.\s)?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})\??\-?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})?\??\d+)(.*$)','$1$2.')"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
xsl:function name="snac:stripStringAfterDateInPersname">
<xsl:param as="xs:string" name="tempString"/>
<xsl:choose>
<xsl:when test="matches($tempString,'(,?\s?([b|d]\.\s)?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})\??\-?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})?\??)')">
<xsl:value-of
select="replace($tempString,'^(.*?)(([b|d]\.\s)?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})\??\-?(active\s)?(fl\.\s)?(ca\.\s)?([\d]{3,4})?\??)(.*$)','$1$2.')"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tempString"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
-->
<!--
Replace any amount of space plus comma followed by space with comma space.
-->
<xsl:function name="snac:fixSpaceComma">
<xsl:param name="tempString" as="xs:string"/>
<xsl:value-of select="normalize-space(replace($tempString,'\s+,',', '))"/>
</xsl:function>
<xsl:function name="snac:removeApostropheLowercaseSSpace">
<xsl:param as="xs:string" name="nameString"/>
<xsl:variable name="regEx" as="xs:string">
<xsl:text>'s\s?</xsl:text>
</xsl:variable>
<xsl:choose>
<xsl:when test="matches($nameString,$regEx)">
<xsl:value-of select="normalize-space(replace($nameString,$regEx,''))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$nameString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="snac:changeWordToProperCase">
<xsl:param name="tempString"/>
<xsl:value-of select="upper-case(substring($tempString,1,1))"/>
<xsl:value-of select="substring($tempString,2)"/>
</xsl:function>
<xsl:template name="extractOccupationOrFunction">
<xsl:param name="entry" as="node()"/>
<!-- this template looks for an occupation or function and if it finds one, outputs occupation or function with name entry. -->
<xsl:for-each select="$relatorList/relator">
<xsl:variable name="relator">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="relatorCode">
<xsl:value-of select="./@code"/>
</xsl:variable>
<xsl:variable name="relatorRegex">
<xsl:text>(,\s</xsl:text>
<xsl:value-of select="lower-case(.)"/>
<xsl:text>.*)|(and\s</xsl:text>
<xsl:value-of select="lower-case(.)"/>
<xsl:text>)</xsl:text>
</xsl:variable>
<xsl:choose>
<!-- first it looks to see if there is a @role and then uses it. -->
<xsl:when test="lower-case($entry/*/@role)=lower-case($relator) or lower-case($entry/*/@role)=$relatorCode">
<xsl:choose>
<xsl:when test="name($entry/*)='persname' or name($entry/*)='famame'">
<occupation source="roleAttribute">
<xsl:value-of select="$relator"/>
</occupation>
</xsl:when>
<xsl:otherwise>
<function source="roleAttribute">
<xsl:value-of select="$relator"/>
</function>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- second it looks to see if there an occupation or function like term in the name string. -->
<xsl:analyze-string select="$entry" regex="{$relatorRegex}">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="name($entry/*)='persname' or name($entry/*)='famame'">
<occupation source="nameString">
<xsl:value-of select="$relator"/>
</occupation>
</xsl:when>
<xsl:otherwise>
<function source="nameString">
<xsl:value-of select="$relator"/>
</function>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:function name="snac:removeBeforeHyphen2">
<xsl:param as="xs:string" name="nameString"/>
<xsl:choose>
<xsl:when test="contains($nameString,'--')">
<!--
This keeps everything before hyphen (do you mean "double hyphen"?) and removes everything
after. Also, if there is a weird paragraph character followed by -*, replace it with a
single hyphen.
If the name has [\-\-\-] or other cases of multi-hyphens, this code is not indended to fix
that. Those names have deeper issues and should be fixed elsewhere.
When debugging, keep in mind that this is called from xsl:if and xsl:when tests, and not
necessarily only when we are normalizing a name for output.
-->
<!-- After name-out: is always empty, even though the output is fine. Unexpected. Unclear why. -->
<!-- <xsl:message> -->
<!-- <xsl:value-of select="concat(' name-in: ', -->
<!-- $nameString, -->
<!-- ' name-out: ', -->
<!-- replace(normalize-space(substring-before(snac:dateHyphen2($nameString),'\-\-')),'¶(-*)','-'))"/> -->
<!-- </xsl:message> -->
<xsl:value-of select="replace(normalize-space(substring-before(snac:dateHyphen2($nameString),'--')),'¶(-*)','-')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$nameString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="snac:dateHyphen2">
<xsl:param as="xs:string" name="nameString"/>
<!-- Commented out in Daniel's original code. -->
<!--xsl:value-of select="replace($nameString,'[^-](\s[\d]{4}) 2','$1¶ 2')"/-->
<xsl:value-of select="replace($nameString,'(\s[\d]{4})[-]{2,3}','$1¶--')"/>
</xsl:function>
<xsl:function name="snac:containsFamily" as="xs:boolean">
<xsl:param name="tempString" as="xs:string"/>
<!--
This is similar to creating a normal-for-match. Lowercase, no punctuation. The original string
"Pease family, of Frosterley, Co. Durham." becomes "pease family of frosterley co durham".
-->
<xsl:variable name="normalizedString">
<xsl:choose>
<xsl:when test="snac:removeBeforeHyphen2($tempString)=''">
<xsl:value-of select="snac:normalizeString($tempString)"/>
<!-- test for cases of multiple hyhens used to imply a name -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="snac:normalizeString(snac:removeBeforeHyphen2($tempString))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--
Sep 22 2014 Name becomes zero after normalization above.
<persname xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">[\-\-\-] Boos; </persname>
If that happens, return false. Before adding the choose/when/otherwise below we could get this Saxon error:
Error on line 705 of functions.xsl:
XTTE0780: An empty sequence is not allowed as the result of function snac:containsFamily()
at snac:containsFamily()
Basically, analyze-string didn't run, thus nothing was returned. The w3c definition of
analyze-string says: "The function returns the zero-length string if there is no captured substring
with the relevant number."
When working on the regex remember that "Pease family, of Frosterley, Co. Durham." becomes "pease family of frosterley co
durham".
Dec 15 2014 In addition to not(regex-group(3) allow group 3 to begin with 'of' so it will work for
the majority of AHUB families that are tagged as <persname>
xlf /data/source/findingAids/ahub/f_5178.xml
normalFinal: Pease family, of Frosterley, Co. Durham. type: regExed
<persname source="aacr2" encodinganalog="ukmarc600.30" authfilenumber="134">Pease family, of Frosterley, Co. Durham.</persname>
-->
<xsl:choose>
<xsl:when test="string-length($normalizedString) > 0">
<xsl:analyze-string select="$normalizedString"
regex="
^
([\p{{L}}]+\s)
(family\s?)
(([\p{{L}}]+\s?)*)
(.*)
$
" flags="x">
<xsl:matching-substring>
<!-- <xsl:message> -->
<!-- <xsl:value-of select="$normalizedString"/> -->
<!-- <xsl:value-of select="$cr"/> -->
<!-- <xsl:value-of select="concat('re1:x', regex-group(1), 'xre2:x', regex-group(2), 'xre3:x', regex-group(3), 'x')"/> -->
<!-- </xsl:message> -->
<xsl:choose>
<xsl:when test="regex-group(2) and
(not(regex-group(3)) or matches(regex-group(3), '^of'))">
<xsl:value-of select="boolean(1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean(0)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="boolean(0)"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="snac:containsCorporateWord" as="xs:boolean">
<!--
This function examines a string to see if it contains any "corporate" words, and
returns boolean yes if it finds one.
Unfortunately, ahub often has an epithet at the end of the name, and many of those names trigger
this code.
-->
<xsl:param name="tempString" as="xs:string"/>
<xsl:variable name="tokenList" select="tokenize(snac:normalizeString($tempString), '\s+')"/>
<!--
Maybe we can try detecting corporate words only when they occur before (to the left of) any
date-like digits. Or even before any digits at all.
-->
<xsl:message>
<xsl:text>cword: </xsl:text>
<xsl:value-of select="replace(snac:normalizeString($tempString), '^(.*?)\d{4,}.*$', '$1')"/>
</xsl:message>
<!-- <xsl:variable name="tokenList" select="tokenize(replace(snac:normalizeString($tempString), '^(.*)\d{4,}.*$', '$1'), '\s+')"/> -->
<xsl:value-of
select="
exists(index-of($tokenList,'&'))
or exists(index-of($tokenList,'agency'))
or exists(index-of($tokenList,'assoc'))
or exists(index-of($tokenList,'associates'))
or exists(index-of($tokenList,'association'))
or exists(index-of($tokenList,'board'))
or exists(index-of($tokenList,'bro'))
or exists(index-of($tokenList,'bros'))
or exists(index-of($tokenList,'brother'))
or exists(index-of($tokenList,'brothers'))
or exists(index-of($tokenList,'center'))
or exists(index-of($tokenList,'central'))
or exists(index-of($tokenList,'chorus'))
or exists(index-of($tokenList,'cia'))
or exists(index-of($tokenList,'cie'))
or exists(index-of($tokenList,'citizens'))
or exists(index-of($tokenList,'city'))
or exists(index-of($tokenList,'club'))
or exists(index-of($tokenList,'cnty'))
or exists(index-of($tokenList,'co'))
or exists(index-of($tokenList,'coalition'))
or exists(index-of($tokenList,'college'))
or exists(index-of($tokenList,'commercial'))
or exists(index-of($tokenList,'commission'))
or exists(index-of($tokenList,'committee'))
or exists(index-of($tokenList,'company'))
or exists(index-of($tokenList,'conference'))
or exists(index-of($tokenList,'congregational'))
or exists(index-of($tokenList,'congress'))
or exists(index-of($tokenList,'consulate'))
or exists(index-of($tokenList,'corp'))
or exists(index-of($tokenList,'corporation'))
or exists(index-of($tokenList,'council'))
or exists(index-of($tokenList,'county'))
or exists(index-of($tokenList,'court'))
or exists(index-of($tokenList,'daughter'))
or exists(index-of($tokenList,'daughters'))
or exists(index-of($tokenList,'delegation'))
or exists(index-of($tokenList,'department'))
or exists(index-of($tokenList,'dept'))
or exists(index-of($tokenList,'dept.'))
or exists(index-of($tokenList,'district'))
or exists(index-of($tokenList,'division'))
or exists(index-of($tokenList,'federation'))
or exists(index-of($tokenList,'festival'))
or exists(index-of($tokenList,'firm'))
or exists(index-of($tokenList,'foundation'))
or exists(index-of($tokenList,'gallery'))
or exists(index-of($tokenList,'gazette'))
or exists(index-of($tokenList,'gesellschaft'))
or exists(index-of($tokenList,'governor'))
or exists(index-of($tokenList,'group'))
or exists(index-of($tokenList,'headquarters'))
or exists(index-of($tokenList,'herr'))
or exists(index-of($tokenList,'hospital'))
or exists(index-of($tokenList,'hotel'))