-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.xsl
3961 lines (3523 loc) · 193 KB
/
lib.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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:lib="http://example.com/"
xmlns:rel="http://example.com/relators"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
xmlns:functx="http://www.functx.com"
xmlns:marc="http://www.loc.gov/MARC21/slim"
xmlns:eac="urn:isbn:1-931666-33-4"
xmlns:madsrdf="http://www.loc.gov/mads/rdf/v1#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:mads="http://www.loc.gov/mads/"
xmlns:snacwc="http://socialarchive.iath.virginia.edu/worldcat"
exclude-result-prefixes="eac lib xs saxon xsl madsrdf rdf mads functx marc snacwc rel"
>
<!--
Author: Tom Laudeman
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://opensource.org/licenses/ECL-2.0
http://www.osedu.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 is a library of xml templates and functions shared by several XSLT scripts.
match_record -> tpt_all_xx -> lib:edate_core -> tpt_exist_dates returns tokens
' '-> tpt_show_date takes tokens and returns exist_dates
'-> var tokens returned by lib:edate_core)
'-> var exist_dates returned by lib:edate_core)
Template match_record is in try_date_regex.xsl or oclc_marc2cpf.xsl.
Template name="match_record" calls template tpt_all_xx to create a node
set that is later used to create the .c and .r output files.
tpt_all_xx builds a node set of the .c and .r nameEntry/part. Inside
tpt_all_xx, the var temp_node includes a exist dates which it gets by
calling lib:edate_core(). lib:edate_core() calls tpt_exist_dates when
it has something it wants a date for.
I suggest changing lib: to eac: maybe so that all the code and
functions are in the same namespace. I'm not sure it makes any
difference since it seems necessary to explicitly set the namespace
since it is difficult to know the current context namespace. Using the
current context namespace broke this code when it was refactored into a
more-or-less portable set of templates and functions.
match_record -> tpt_all_xx -> edate_core -> tpt_exist_dates
'-> var exist_dates-> tpt_show_dates
-->
<xsl:import href="av.xsl"/>
<!--
Variables for the localType values. The values have changed several times, so now they go into
variables. "av_" is mnemonic for Attribute Value.
av_DatabaseName Added Sep 13 2013
-->
<xsl:variable name="cr" select="'
'"/>
<!--
When we find the tag <Corporation> we set the locn to 'CorporateBody'. Person and Family are ok.
The "cpf" versions are used in CPF cpfDescription/identity/entityType
-->
<xsl:variable name="corp_val" select="'CorporateBody'"/>
<xsl:variable name="pers_val" select="'Person'"/>
<xsl:variable name="fami_val" select="'Family'"/>
<xsl:variable name="expe_val" select="'Expedition'"/>
<xsl:variable name="cpf_corp_val" select="'corporateBody'"/>
<xsl:variable name="cpf_pers_val" select="'person'"/>
<xsl:variable name="cpf_fami_val" select="'family'"/>
<xsl:variable name="cpf_expe_val" select="'expedition'"/>
<!--
The XSLT equivalent of a key-value lookup list. Use the "normal" versions of $pers_val etc. and
*only* use $cpf_pers_val etc. in the CPF entityType in eac_cpf.xsl.
Nov 18 2013 New attribute cpf is used to select the cpf value when we know the "normal" version.
Jan 27 2015 New 'organization' for NARA.
-->
<xsl:variable name="etype" xmlns="urn:isbn:1-931666-33-4">
<value key="{$pers_val}">
<xsl:value-of select="$av_Person"/>
</value>
<value key="{$corp_val}">
<xsl:value-of select="$av_CorporateBody"/>
</value>
<value key="{$expe_val}">
<!-- Added to deal with field books -->
<xsl:value-of select="$av_CorporateBody"/>
</value>
<value key="'organization'">
<!-- Added to deal with NARA DSA orgs -->
<xsl:value-of select="$av_CorporateBody"/>
</value>
<value key="{$fami_val}">
<xsl:value-of select="$av_Family"/>
</value>
<value cpf="{$pers_val}">
<xsl:value-of select="$cpf_pers_val"/>
</value>
<value cpf="{$corp_val}">
<xsl:value-of select="$cpf_corp_val"/>
</value>
<value cpf="'organization'"> <!-- NARA orgs are corporateBody -->
<xsl:value-of select="$cpf_corp_val"/>
</value>
<value cpf="{$fami_val}">
<xsl:value-of select="$cpf_fami_val"/>
</value>
</xsl:variable>
<!-- <xsl:output method="xml" /> -->
<xsl:variable name="occupations" select="document('occupations.xml')/*"/>
<!-- <xsl:variable name="relators" select="document('vocabularyrelators.rdf')/*"/> -->
<xsl:variable name="custom_relators" select="document('relatorList.xml')/*"/>
<xsl:variable name="languages" select="document('vocabularylanguages.rdf')/*"/>
<xsl:variable name="org_codes" select="document('worldcat_code.xml')/*"/>
<!-- Too many files with "places" in the name. geonames_places is less confusing. -->
<xsl:variable name="places" select="document('geonames_places.xml')/*"/>
<xsl:variable name="this_year" select="year-from-date(current-date())"/>
<xsl:template mode="get_lang" match="*">
<xsl:choose>
<xsl:when test="madsrdf:authoritativeLabel">
<xsl:value-of select="madsrdf:authoritativeLabel"/>
</xsl:when>
<xsl:when test="madsrdf:deprecatedLabel">
<xsl:value-of select="madsrdf:deprecatedLabel"/>
<!-- <xsl:text> (deprecated) </xsl:text> -->
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="tpt_lang_decl" xmlns="urn:isbn:1-931666-33-4">
<!--
This is control/languageDeclaration. I'm pretty sure there is only a single
040$b per record, but for-each sets the context, so that is
good. When we have an 040$b, use it, otherwise default to English.
(description/languageUsed is derived from the 041$a; see tpt_language)
-->
<xsl:choose>
<xsl:when test="boolean(marc:datafield[@tag='040']/marc:subfield[@code='b'])">
<xsl:for-each select="marc:datafield[@tag='040']/marc:subfield[@code='b']">
<xsl:variable name="lang_code" select="."/>
<xsl:element name="languageDeclaration" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:element name="language" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:attribute name="languageCode" select="$lang_code"/>
<xsl:apply-templates mode="get_lang" select="$languages/madsrdf:Topic[madsrdf:code = $lang_code]"/>
</xsl:element>
<xsl:element name="script" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:attribute name="scriptCode" select="'Zyyy'"/>
<xsl:text>Unknown</xsl:text>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:element name="languageDeclaration" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:element name="language" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:attribute name="languageCode" select="'eng'"/>
<xsl:text>English</xsl:text>
</xsl:element>
<xsl:element name="script" xmlns:eac="urn:isbn:1-931666-33-4">
<xsl:attribute name="scriptCode" select="'Zyyy'"/>
<xsl:text>Unknown</xsl:text>
</xsl:element>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="tpt_geo" xmlns="urn:isbn:1-931666-33-4">
<!-- <xsl:message> -->
<!-- <xsl:text>places: </xsl:text> -->
<!-- <xsl:copy-of select="$places/place[1]/local-name()" /> -->
<!-- <xsl:text> ns (none):</xsl:text> -->
<!-- <xsl:copy-of select="$places/place[1]/namespace-uri()" /> -->
<!-- <xsl:text>
</xsl:text> -->
<!-- </xsl:message> -->
<!--
Build a node set regardless of empty placeEntry values or duplicates. Then copy removing empties
and duplicates. The elements are for output, so we create them in the eac namespace.
-->
<xsl:variable name="geo_651">
<!--
Create a temporary node set in variable $df with a prev
attribute so we can check for $a followed by ($z
repeating). Template mode do_subfield is below. Call
do_subfield with a sequence of all the subfield elements of the
current datafield.
-->
<xsl:variable name="df">
<xsl:for-each select="marc:datafield[@tag='651']">
<datafield xmlns="http://www.loc.gov/MARC21/slim">
<xsl:copy-of select="@*" />
<xsl:text>
</xsl:text>
<xsl:call-template name="do_subfield">
<xsl:with-param name="sub" select="marc:subfield"/>
<xsl:with-param name="prev" select="''"/>
</xsl:call-template>
</datafield>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:variable>
<!--
With the proper meta-data, building the geo subject is possible. Three cases. (1) $a
regardless of following. (2) $a with contiguous following $z (repeating). (3) $z single or
repeating not contiguous with $a.
$df has been forced to be namespace marc as is conventional for all input from the marc
records, so continue using the marc: prefix here. This template has been forced into the eac:
namespace.
-->
<xsl:for-each select="$df/marc:datafield">
<xsl:if test="marc:subfield[@code='a' or (@code='z' and @prev='a')]">
<place localType="{$av_associatedPlace}">
<placeEntry>
<xsl:for-each select="marc:subfield[@code='a' or (@code='z' and @prev='a')]">
<xsl:if test="not(position()=1)">
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</placeEntry>
</place>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:if test="marc:subfield[@code='z' and (@prev='' or @prev='z')]">
<place localType="{$av_associatedPlace}">
<placeEntry>
<xsl:for-each select="marc:subfield[@code='z' and (@prev='' or @prev='z')]">
<xsl:if test="not(position()=1)">
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</placeEntry>
</place>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="geo_pass1">
<xsl:call-template name="tpt_65x_geo">
<xsl:with-param name="tag" select="650"/>
</xsl:call-template>
<xsl:call-template name="tpt_65x_geo">
<xsl:with-param name="tag" select="656"/>
</xsl:call-template>
<xsl:call-template name="tpt_65x_geo">
<xsl:with-param name="tag" select="657"/>
</xsl:call-template>
<xsl:copy-of select="$geo_651"/>
</xsl:variable>
<!--
Now that we have a list of geo places, make another pass to put in authoritative names from
geonames.org.
-->
<xsl:variable name="geo1">
<xsl:for-each select="$geo_pass1/eac:place">
<!-- <xsl:variable name="pn" select="replace(eac:placeEntry, '\-\-', ' ')"/> -->
<xsl:variable name="pn" select="eac:placeEntry"/>
<!-- <xsl:message> -->
<!-- <xsl:text>pn: </xsl:text> -->
<!-- <xsl:value-of select="$pn"/> -->
<!-- <xsl:text> orig: </xsl:text> -->
<!-- <xsl:copy-of select="$places/place[cpfName/text() = 'Pennsylvania Ambridge']"/> -->
<!-- <xsl:text>
</xsl:text> -->
<!-- </xsl:message> -->
<place localType="{@localType}">
<placeEntry>
<xsl:choose>
<xsl:when test="$places/place[cpfName/text() = $pn]">
<!--
Use xsl:for-each to loop and set context but we must have only one record. We
choose the record with the max score. In the tests below, note that
element/text() will only be true if there is a non-empty value, AFAIK. As opposed
to testing the element which is true if the element exists, empty or not.
Note that continentCode aka countryCode is occasionally blank, so we deal with it.
-->
<xsl:variable name="ms" select="max($places/place[cpfName/text() = $pn]/score)"/>
<xsl:for-each select="$places/place[cpfName/text() = $pn and score = $ms]">
<xsl:attribute name="latitude" select="lat"/>
<xsl:attribute name="longitude" select="lng"/>
<xsl:attribute name="vocabularySource" select="concat('http://www.geonames.org/', geonameId)"/>
<xsl:if test="continentCode/text()">
<xsl:attribute name="countryCode" select="continentCode"/>
</xsl:if>
<xsl:if test="adminName1/text() and not (name/text() = adminName1/text())">
<xsl:value-of select="adminName1"/>
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:if test="adminName2/text() and not (name/text() = adminName2/text())">
<xsl:value-of select="adminName2"/>
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:value-of select="name"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="eac:placeEntry"/>
</xsl:otherwise>
</xsl:choose>
</placeEntry>
</place>
</xsl:for-each>
</xsl:variable>
<!--
Deduping a node set in 6 lines of code, but subtle. We need to
compare each node to every other node in the node set. That is
impossible in a single conditional because pname-match() takes
string args, not node set args. Looking at it another way, we have
two separate contexts to compare, so one of them will have to be
saved to a variable. somepath[] will implicitly for-each over nodes
in the []. We must use a variable for the outer context (an explicit
for-each) since the context inside the [] is different. The xsl:if
will map all preceding placeEntry values through pname-match()
against the current $geo1 node in $curr. To state the problem
another way, we can only have one implicit loop in [], so that loop
must be the preceding nodes. We need to loop through $geo1 comparing
the current node to all (by looping over) the preceding nodes.
We are deduping a list against itself. We loop over each node while
implicitly looping over all preceding nodes. The outer loop is an
(explicit) for-each. The inner loop is an implicit mapping of values
inside preceding::placeEntry[].
The final confusing part is that the preceding::placeEntry will
return 0 or more matching nodes, as opposed to returing true or
false. If there are any matching nodes then we have a
duplicate. Thus we put not() around the whole preceding::placeEntry expression.
This short method (the 6 line deduping algorighm) requires an outer container element. In this
case I suppose the outer element is <place> and we are deduping <placeEntry>. (Or it could be
that I didn't understand how to use /* to for-each over nodes in a variable without referring to
a root node.)
-->
<xsl:for-each select="$geo1/eac:place[string-length(eac:placeEntry) > 0]">
<xsl:variable name="curr" select="."/>
<xsl:if test="not(preceding::eac:placeEntry[lib:pname-match(text(), $curr/eac:placeEntry)])">
<xsl:copy-of select="$curr" />
</xsl:if>
</xsl:for-each>
</xsl:template> <!-- end tpt_geo -->
<xsl:template name="do_subfield" xmlns="http://www.loc.gov/MARC21/slim">
<xsl:param name="sub"/>
<xsl:param name="prev"/>
<!--
This recursively builds a node set that tells us when 'a' or 'z'
immediately precedes 'z'. Each node in the node set has the code
and $prev as well as the value of the subfield.
-->
<xsl:variable name="code" select="$sub[1]/@code"/>
<subfield code="{$code}" prev="{$prev}">
<xsl:value-of select="$sub[1]" />
</subfield>
<!--
$ancestral_a is the state of the current node that we're passing to
the next recursive iteration. The variable name fits since it could
be restated as "do I have a direct 'a' ancestral lineage?".
-->
<xsl:variable name="ancestral_a">
<xsl:choose>
<xsl:when test="$code = 'a' or ($code = 'z' and $prev = 'a')">
<xsl:value-of select="'a'"/>
</xsl:when>
<xsl:when test="$code = 'z' or $prev = 'z'">
<xsl:value-of select="'z'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--
Recurse on the remaining sibling subfields.
-->
<xsl:if test="boolean($sub/following-sibling::marc:subfield)">
<xsl:call-template name="do_subfield">
<xsl:with-param name="sub" select="$sub/following-sibling::marc:subfield"/>
<xsl:with-param name="prev" select="$ancestral_a"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!--
Nov 18 2013. This should only be used for 6xx, per new email from Daniel.
template tpt_is_cp replaced by improved code.
This is resourceRelation/@xlink:arcrole.
See function lib:cpf_arc_role for cpfRelation/@xlink:arcrole.
-->
<xsl:template name="tpt_arc_role">
<xsl:param name="is_c_flag" as="xs:boolean"/>
<xsl:param name="is_r_flag"/>
<!--
I think this probably ends up in /eac-cpf/cpfDescription/relations/resourceRelation in the output
template eac_cpf.xsl.
-->
<xsl:choose>
<xsl:when test="$is_c_flag or (eac:e_name/@is_creator=true())">
<xsl:value-of select="$av_creatorOf"/>
</xsl:when>
<xsl:when test="$is_r_flag">
<xsl:value-of select="$av_referencedIn"/>
</xsl:when>
<xsl:otherwise>
<!--
Newer comment: Can a person be associatedWith an archival resource?
Old comment: This can't happen given current logic.
New comment: Really? Maybe not for WorldCat because all those are either is_c_flag or
is_r_flag, but the SIA data might get here.
-->
<xsl:value-of select="concat('rr:',$av_associatedWith)"/>
<xsl:message>
<xsl:value-of select="concat('warning: resourceRelation xlink:arcrole associatedWith ', $av_associatedWith)"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="tpt_rules">
<xsl:choose>
<xsl:when test="substring(marc:controlfield[@tag='008'],11,1)='b'">
<xsl:text>aacr1</xsl:text>
</xsl:when>
<xsl:when test="substring(marc:controlfield[@tag='008'],11,1)='c'">
<xsl:text>aacr2</xsl:text>
</xsl:when>
<xsl:when test="substring(marc:controlfield[@tag='008'],11,1)='d'">
<xsl:text>aacr2compatible</xsl:text>
</xsl:when>
<xsl:otherwise>
<!--
Ideally this would be set specially for each input data set. See the param auth_form in
the main calling code that includes lib.xsl, for example oclc_marc2cpf.xsl.
-->
<xsl:value-of select="$auth_form"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="tpt_65x_geo" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="tag"/>
<!--
Deal with mutiple elements with the same tag value. For example,
multiple 650$z elements. Simply join all $z, contiguous or not.
-->
<xsl:for-each select="marc:datafield[@tag=$tag]" >
<!-- removed xmlns="urn:isbn:1-931666-33-4" and moved to enclosing template element. -->
<place localType="{$av_associatedPlace}">
<placeEntry>
<xsl:for-each select="marc:subfield[@code='z']">
<xsl:if test="not(position() = 1)">
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</placeEntry>
</place>
</xsl:for-each>
</xsl:template>
<xsl:template name="tpt_topical_subject" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="record_position"/>
<xsl:param name="record_id"/>
<!--
Removing the xmlns above may break this, and the name of the namespace must be "eac" as required
by the deduping loop at the end of this template.
Originally, some subfields were captured based on non-repeating or
repeating, but the data is bad for a few records, so now we capture
all the desired subfields as though they are repeating. Assume that
XSLT XPath "or" clauses are processed in order, and have short
circuit optimization.
Must use xsl:element for localDescription with xmlns. Also need to use xsl:element for term, but
it inherits eac: which is a tiny bit odd because xsl:element is not supposed to inherit, but I
guess it doesn't inherit the current namespace, but an explicit namespace is subtly different
from "current namespace". Current namespace in the for-each is marc:. As a shortcut, the marc:
can be turned off in the output by putting marc into the exclude-result-prefixes, but that
masquerades potential problems. While it is not necessary to put an explicit namespace on
xsl:element term, I did anyway because namespace inheritance is clearly not clear.
-->
<xsl:variable name="all_ts">
<xsl:for-each select="marc:datafield[@tag='650']">
<localDescription localType="{$av_associatedSubject}">
<term>
<xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c' or @code='d' or @code='v' or @code='x' or @code='y']">
<xsl:if test="not(position() = 1)">
<xsl:text>--</xsl:text>
</xsl:if>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</term>
</localDescription>
</xsl:for-each>
</xsl:variable>
<!--
The node-set dedup idiom. See extensive comments with tpt_geo.
Context is localDescription so paths below are term being relative to the context node.
-->
<xsl:for-each select="$all_ts/eac:localDescription[string-length(eac:term) > 0]">
<xsl:variable name="curr" select="."/>
<xsl:if test="not(preceding::eac:term[lib:pname-match(text(), $curr/eac:term)])">
<xsl:copy-of select="$curr"/>
</xsl:if>
</xsl:for-each>
</xsl:template> <!-- end tpt_topical_subject -->
<xsl:template name="tpt_language"
xmlns="urn:isbn:1-931666-33-4">
<!--
This is description/languageUsed. First put each 041$a lang into var $all, then dedup $all and
wrap each lang with languageUsed to go into description.
(control/languageDeclaration is derived 040$b; see tpt_lang_decl)
<subfield code="a">gereng</subfield>
This is in the <description> element in the cpf template.
<languageUsed>
<language languageCode="ger">German</language>
<language languageCode="eng">English</language>
<script scriptCode="Zyyy">Unknown</script>
</languageUsed>
An empty sequence is not allowed as the @select attribute of xsl:analyze-string.
Use for-each to make sure the context is something non-empty.
-->
<xsl:variable name="all">
<xsl:for-each select="marc:datafield[@tag='041']/marc:subfield[@code='a']">
<xsl:analyze-string select="." regex="...">
<xsl:matching-substring>
<xsl:variable name="lang_code" select="regex-group(0)"/>
<language languageCode="{$lang_code}">
<xsl:apply-templates mode="get_lang" select="$languages/madsrdf:Topic[madsrdf:code = $lang_code]"/>
</language>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</xsl:variable>
<!--
The node-set dedup idiom. See extensive comments with tpt_geo. This is a little different since
$all has no container element, so we for-each on $all/* rather than $all/container. Also, our
test is = instead of lib:pname-match() or some other function. Apparently /* is perfectly legal
and robust. http://www.dpawson.co.uk/xsl/sect2/root.html
-->
<xsl:for-each select="$all/*[string-length() > 0]">
<xsl:variable name="curr" select="."/>
<xsl:if test="not(preceding::eac:language[text() = $curr/text()])">
<languageUsed>
<xsl:copy-of select="$curr"/>
<script scriptCode="Zyyy">
<xsl:text>Unknown</xsl:text>
</script>
</languageUsed>
</xsl:if>
</xsl:for-each>
</xsl:template> <!-- end tpt_language -->
<xsl:template name="tpt_occupation" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="df"/>
<!--
If we are a person/family with a 656 occupation then use it, else try to use [167]00$e or [167]00$4 via some lookups in the
relator xml authority files. (Remember, $e and $4 are relator fields.) Across x00, x10, and x11,
relator fields are somewhat inconsistent ($e and $j).
-->
<xsl:variable name="all">
<xsl:if test="$df/marc:datafield[@tag ='100' or @tag ='600' or @tag ='700'] and
(boolean($df/marc:datafield/marc:subfield[@code='e']) or
boolean($df/marc:datafield/marc:subfield[@code='4']))">
<xsl:for-each select="$df/marc:datafield/marc:subfield[@code='e']">
<xsl:call-template name="tpt_ocfu_e">
<xsl:with-param name="sfield" select="."/>
<xsl:with-param name="cname" select="'occupation'"/>
</xsl:call-template>
</xsl:for-each>
<xsl:for-each select="$df/marc:datafield/marc:subfield[@code='4']">
<xsl:call-template name="tpt_ocfu_4">
<xsl:with-param name="sfield" select="."/>
<xsl:with-param name="cname" select="'occupation'"/>
</xsl:call-template>
</xsl:for-each>
<!-- <xsl:apply-templates mode="tpt_occ_e" select="$df/marc:datafield/marc:subfield[@code='e']"/> -->
<!-- <xsl:apply-templates mode="tpt_occ_4" select="$df/marc:datafield/marc:subfield[@code='4']"/> -->
</xsl:if>
<!-- do 657 for 100 as occupation per email Mar 14 2013 1:49 pm -->
<xsl:if test="$df/marc:datafield[@tag ='100'] and boolean(marc:datafield[@tag='656' or @tag='657'])">
<xsl:for-each select="marc:datafield[@tag='656' or @tag='657']">
<xsl:call-template name="tpt_occ_65x">
<xsl:with-param name="ocfu" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
</xsl:variable>
<!--
The node-set dedup idiom. See extensive comments with tpt_geo.
-->
<xsl:for-each select="$all/eac:occupation[string-length(eac:term) > 0]">
<xsl:variable name="curr" select="."/>
<xsl:if test="not(preceding::eac:term[lib:pname-match(text(), $curr/eac:term)])">
<xsl:copy-of select="$curr"/>
</xsl:if>
</xsl:for-each>
</xsl:template> <!-- end tpt_occupation -->
<xsl:template name="tpt_function" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="df"/>
<!--
Must use for-each in case there a multiple $e or $4. Or in the x11 $j.
-->
<xsl:variable name="all">
<xsl:if test="$df/marc:datafield[matches(@tag, '110|610|710')] and
(boolean($df/marc:datafield/marc:subfield[@code='e']) or
boolean($df/marc:datafield/marc:subfield[@code='4']))">
<xsl:for-each select="$df/marc:datafield/marc:subfield[@code='e']">
<xsl:call-template name="tpt_ocfu_e">
<xsl:with-param name="sfield" select="."/>
<xsl:with-param name="cname" select="'function'"/>
</xsl:call-template>
</xsl:for-each>
<xsl:for-each select="$df/marc:datafield/marc:subfield[@code='4']">
<xsl:call-template name="tpt_ocfu_4">
<xsl:with-param name="sfield" select="."/>
<xsl:with-param name="cname" select="'function'"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
<xsl:if test="$df/marc:datafield[matches(@tag, '111|611|711')] and
boolean($df/marc:datafield/marc:subfield[@code='j'])">
<xsl:for-each select="$df/marc:datafield/marc:subfield[@code='j']">
<xsl:call-template name="tpt_ocfu_e">
<xsl:with-param name="sfield" select="."/>
<xsl:with-param name="cname" select="'function'"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
<xsl:if test="$df/marc:datafield[@tag ='110' or @tag ='111']">
<xsl:for-each select="marc:datafield[@tag='657']">
<function>
<term>
<xsl:if test="marc:subfield[@code='a']">
<xsl:value-of select="lib:norm-punct(marc:subfield[@code='a'])"/>
</xsl:if>
<xsl:for-each select="marc:subfield[@code='x' or @code='y' or @code='z']">
<xsl:text>--</xsl:text>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</term>
</function>
</xsl:for-each>
<!-- do 656 for 110 and 111 as function per email Mar 14 2013 1:49 pm -->
<xsl:for-each select="marc:datafield[@tag='656']">
<function>
<term>
<xsl:if test="marc:subfield[@code='a']">
<xsl:value-of select="lib:norm-punct(marc:subfield[@code='a'])"/>
</xsl:if>
<xsl:for-each select="marc:subfield[@code='x' or @code='y' or @code='z']">
<xsl:text>--</xsl:text>
<xsl:value-of select="lib:norm-punct(.)"/>
</xsl:for-each>
</term>
</function>
</xsl:for-each>
</xsl:if>
</xsl:variable>
<!--
The node-set dedup idiom. See extensive comments with tpt_geo. Context is function so paths
below are term being relative to the context node.
-->
<xsl:for-each select="$all/eac:function[string-length(eac:term) > 0]">
<xsl:variable name="curr" select="."/>
<xsl:if test="not(preceding::eac:term[lib:pname-match(text(), $curr/eac:term)])">
<xsl:copy-of select="$curr"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="tpt_ocfu_4" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="sfield"/>
<xsl:param name="cname"/>
<!--
Named "_4" because $sfield is (usually) $df/marc:datafield/marc:subfield[@code='4']
This is similar to tpt_ocfu_e, with the addition that it first checks against the relators file.
Use a double test [[]] on the for-each to find the single matching <mads> element. We're using
the for-each to set the context of a record where the singular variant matches our unpunctuated
value. Once we have the context, just get the authority/occupation name as usual.
-->
<xsl:variable name="test_code" select="lib:unpunct($sfield)"/>
<xsl:variable name="relator_value" select="lib:relator-lookup($test_code)"/>
<!-- <xsl:message> -->
<!-- <xsl:text>test_code: </xsl:text> -->
<!-- <xsl:copy-of select="$test_code"/> -->
<!-- <xsl:text> sf: </xsl:text> -->
<!-- <xsl:copy-of select="$sfield"/> -->
<!-- <xsl:text> rv: </xsl:text> -->
<!-- <xsl:copy-of select="$relator_value"/> -->
<!-- <xsl:text>
</xsl:text> -->
<!-- </xsl:message> -->
<xsl:element name="{$cname}">
<xsl:attribute name="localType" select="$av_derivedFromRole"/>
<xsl:for-each select="$occupations/mads:mads[mads:variant[@otherType='singular']/mads:occupation[text() = $relator_value]]">
<term>
<xsl:value-of select="mads:authority/mads:occupation"/>
</term>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name="tpt_ocfu_e" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="sfield"/>
<xsl:param name="cname"/>
<!--
Named "_e" because $sfield is (usually) $df/marc:datafield/marc:subfield[@code='e'].
Normally, the $sfield is the $e, thus this historical comment: Test the lowercase, un-punctuated
text of the $e (context) against the occupation list.
Use a variable to hold the current x00$e, since the context changes once we are inside the
for-each. It was a mistake to do lib:unpunct(text()) below in the for-each where $unp is, because
at that point the context is the occupations record, not our marc x00$e value.
Use a double test [[]] on the for-each to find the single matching <mads> element. We're using
the for-each to set the context of a record where the singular variant matches our unpunctuated
value. Once we have the context, just get the authority/occupation name as usual.
-->
<xsl:variable name="unp" select="lower-case(lib:unpunct($sfield))"/>
<xsl:for-each select="$occupations/mads:mads[mads:variant[@otherType='singular']/mads:occupation[text() = $unp]]">
<xsl:element name="{$cname}">
<xsl:attribute name="localType" select="$av_derivedFromRole"/>
<term>
<xsl:value-of select="mads:authority/mads:occupation"/>
</term>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template name="tpt_occ_65x" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="ocfu"/>
<!-- 656 and 657 work the same as occupation, and I suspect they are mutually exclusive. -->
<occupation>
<term>
<xsl:if test="$ocfu/marc:subfield[@code='a']">
<xsl:value-of select="marc:subfield[@code='a']"/>
</xsl:if>
<xsl:for-each select="$ocfu/marc:subfield[@code='x']">
<xsl:text>--</xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select="$ocfu/marc:subfield[@code='y']">
<xsl:text>--</xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select="$ocfu/marc:subfield[@code='z']">
<xsl:text>--</xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
</term>
</occupation>
</xsl:template>
<xsl:template name="tpt_show_date" xmlns="urn:isbn:1-931666-33-4">
<xsl:param name="tokens"/>
<xsl:param name="is_family"/> <!-- means "active" not entity type family -->
<xsl:param name="rec_pos"/>
<xsl:param name="entity_type"/>
<!--
Date determination logic is here, using the tokenized date. Some of this code is a bit
dense. existDates must have same namespace as its parent element eac-cpf.
We start of with several tests to determine dates that we cannot parse, and put them in a variable
to neaten up the code and allow us to only have a single place where the unparsed date node set is
constructed. Anything that makes it through these tests should be parse-able, although there is a
catch-all otherwise at the end of the parsing choose.
Forcing the data type of an attribute to be xs:boolean is probably not easy, so I didn't even
try. Strong typing fails again.
-->
<xsl:variable name="is_active" as="xs:boolean">
<xsl:choose>
<xsl:when test="$is_family or ($tokens/tok[1])/@is_active = true()">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$debug">
<xsl:message>
<xsl:text>tokens: </xsl:text>
<xsl:copy-of select="$tokens" />
<xsl:text> isa: </xsl:text>
<xsl:value-of select="($tokens/tok[1])/@is_active"/>
<xsl:text> isf: </xsl:text>
<xsl:value-of select="$is_active"/>
<xsl:text>
</xsl:text>
</xsl:message>
</xsl:if>
<xsl:variable name="is_unparsed" as="xs:boolean">
<xsl:choose>
<!--
If there are no numeric tokens, the date is unparsed. Note
that since we only support integer values for years, we are
not checking all valid XML formats for numbers, only
[0-9]. Century numbers are a special case, so we have to
check them too.
-->
<!-- <xsl:when test="count($tokens/tok[matches(text(), '^\d+$|\d+.*century')]) = 0"> -->
<xsl:when test="count($tokens/tok[text() = 'num']) = 0">
<xsl:value-of select="true()"/>
</xsl:when>
<!--
If there are two adjacent number tokens, then unparsed. Due to
the declarative nature of xpath, position() will be relative to
the context which will change as xpath iterates through the
first half of the xpath.
-->
<xsl:when test="$tokens/tok[text() = 'num']/following-sibling::tok[position()=1 and text() = 'num']">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:when test="$tokens/tok[text() = 'num' and string-length(text()) > 4]">
<xsl:value-of select="true()"/>
</xsl:when>
<!--
Alphabetic tokens that aren't our known list must be unparsed dates. Note that the second
regex matches against the full length of the token.
-->
<xsl:when test="$tokens/tok[matches(text(), '[A-Za-z]+') and
not(matches(text(), '^(num|approximately|or|active|century|b|d|st|nd|rd|th)$'))]">
<xsl:value-of select="true()"/>
</xsl:when>
<!--
Unrecognized tokens should only be year numbers. Any
supposed-year-numbers that are NAN or numbers > 2013 are errors
are unparsed dates. Also, notBefore and notAfter should never be
NaN.
-->
<xsl:when test="$tokens/tok[@notBefore='NaN' or
@notBefore='0' or
@notAfter='NaN' or
@notAfter='0' or
(not(matches(text(), '^(approximately|or|active|century|b|d|st|nd|rd|th|-)$')) and string(@std) = 'NaN') or
@std > $this_year or
@std = 0 ]">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable> <!-- end name="is_unparsed" -->
<xsl:variable name="show_one">
<xsl:choose>
<!-- There is another unparsed "suspicious" date below in the otherwise element of this choose. -->
<xsl:when test="$is_unparsed">
<existDates>
<date localType="{$av_suspiciousDate}">
<xsl:value-of select="$tokens/odate"/>
</date>
</existDates>
</xsl:when>
<!--
This test may be meaningless now that numeric tokens have the value 'num'. Except for century,
we don't like any token that mixes digits and non-digits. We also to not like strings of 5 or
more digits.
-->
<xsl:when test="$tokens/tok[(matches(@std, '\d+[^\d]+|[^\d]+\d+') or matches(@std, '\d{5}')) and not(matches(@val, 'century'))]">
<existDates>
<date localType="{$av_suspiciousDate}">
<xsl:value-of select="$tokens/odate"/>
</date>
</existDates>
</xsl:when>
<!-- Real parsing begins here. -->
<!-- born -->
<xsl:when test="$tokens/tok = 'b' or (($tokens/tok)[last()] = '-')">
<!-- No active since that is illogical with 'born' for persons. -->
<xsl:variable name="loc_type">
<xsl:if test="$is_active">
<xsl:value-of select="$av_active"/>
</xsl:if>
<xsl:if test="not($is_active)">
<xsl:value-of select="$av_born"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="curr_tok" select="$tokens/tok[text() = 'num'][1]"/>
<existDates>
<dateRange>
<fromDate>
<xsl:attribute name="standardDate" select="$curr_tok/@std"/>
<xsl:attribute name="localType" select="$loc_type"/>
<!-- Add attributes notBefore, notAfter. -->
<xsl:for-each select="$curr_tok[text() = 'num'][1]/@*[matches(name(), '^not')]">
<xsl:choose>
<xsl:when test="string-length(.)>0">
<xsl:attribute name="{name()}">
<xsl:value-of select="format-number(., '0000')"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:copy-of select="$rec_pos" />
<xsl:text> line 395 fromDate empty attr: </xsl:text>
<xsl:copy-of select="." />
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--
new: Used to follow the for-each.
old: is_family/is_active doesn't have an active token so we need
to add it here. Maybe it should have been added
earlier.
-->
<xsl:if test="$is_active">
<xsl:text>active </xsl:text>
</xsl:if>
<!--
We know this is a birth date so make that clear in the human readable part of