Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / odds2 / odd2odd.xsl @ 187

History | View | Annotate | Download (96.2 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet 
3
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
4
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5
    xmlns:sch="http://purl.oclc.org/dsdl/schematron" 
6
    xmlns:s="http://www.ascc.net/xml/schematron" 
7
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
8
    xmlns:rng="http://relaxng.org/ns/structure/1.0"
9
    xmlns:tei="http://www.tei-c.org/ns/1.0" 
10
    xmlns:teix="http://www.tei-c.org/ns/Examples" 
11
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
12
    version="2.0" 
13
    exclude-result-prefixes="teix a s tei xs rng sch xsi">
14
  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
15
    <desc>
16
      <p> TEI stylesheet for simplifying TEI ODD markup </p>
17
      <p>This software is dual-licensed:
18

    
19
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
20
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
21

    
22
2. http://www.opensource.org/licenses/BSD-2-Clause
23
		
24
All rights reserved.
25

    
26
Redistribution and use in source and binary forms, with or without
27
modification, are permitted provided that the following conditions are
28
met:
29

    
30
* Redistributions of source code must retain the above copyright
31
notice, this list of conditions and the following disclaimer.
32

    
33
* Redistributions in binary form must reproduce the above copyright
34
notice, this list of conditions and the following disclaimer in the
35
documentation and/or other materials provided with the distribution.
36

    
37
This software is provided by the copyright holders and contributors
38
"as is" and any express or implied warranties, including, but not
39
limited to, the implied warranties of merchantability and fitness for
40
a particular purpose are disclaimed. In no event shall the copyright
41
holder or contributors be liable for any direct, indirect, incidental,
42
special, exemplary, or consequential damages (including, but not
43
limited to, procurement of substitute goods or services; loss of use,
44
data, or profits; or business interruption) however caused and on any
45
theory of liability, whether in contract, strict liability, or tort
46
(including negligence or otherwise) arising in any way out of the use
47
of this software, even if advised of the possibility of such damage.
48
</p>
49
      <p>Author: See AUTHORS</p>
50
      <p>Id: $Id: odd2odd.xsl 10148 2012-03-06 23:51:20Z rahtz $</p>
51
      <p>Copyright: 2011, TEI Consortium</p>
52
    </desc>
53
  </doc>
54
  <xsl:output encoding="utf-8" indent="no"/>
55
  <xsl:param name="autoGlobal">false</xsl:param>
56
  <xsl:param name="selectedSchema"/>
57
  <xsl:param name="verbose"/>
58
  <xsl:param name="useVersionFromTEI">true</xsl:param>
59
  <xsl:param name="stripped">false</xsl:param>
60
  <xsl:param name="configDirectory"/>
61
  <xsl:param name="currentDirectory"/>
62
  <xsl:param name="defaultSource"></xsl:param>
63
  <xsl:param name="defaultTEIVersion">current</xsl:param>
64
  <xsl:param name="defaultTEIServer">http://www.tei-c.org/Vault/P5/</xsl:param>
65
  <xsl:key name="odd2odd-CLASSREFS" match="tei:classRef" use="@key"/>
66
  <xsl:key name="odd2odd-ATTCLASSES" match="tei:classSpec[(tei:attList or @type='atts') and not(@ident='tei.TEIform')]" use="@ident"/>
67
  <xsl:key name="odd2odd-CHANGEATT" match="tei:attDef[@mode='change']" use="concat(../../@ident,'_',@ident)"/>
68
  <xsl:key name="odd2odd-CHANGECONSTRAINT" match="tei:constraintSpec[@mode='change']" use="concat(../@ident,'_',@ident)"/>
69
  <xsl:key name="odd2odd-CLASS_MEMBERED" use="tei:classes/tei:memberOf/@key" match="tei:classSpec"/>
70
  <xsl:key name="odd2odd-ATTREFS" match="tei:attRef" use="concat(@name,'_',../../@ident)"/>
71
  <xsl:key name="odd2odd-DELETEATT" match="tei:attDef[@mode='delete']" use="concat(ancestor::tei:elementSpec/@ident,'_',@ident)"/>
72
  <xsl:key name="odd2odd-DELETEATT" match="tei:attDef[@mode='delete']" use="concat(ancestor::tei:classSpec/@ident,'_',@ident)"/>
73
  <xsl:key name="odd2odd-DELETECONSTRAINT" match="tei:constraintSpec[@mode='delete']" use="concat(../@ident,'_',@ident)"/>
74

    
75
  <xsl:key name="odd2odd-ELEMENT_MEMBERED" use="tei:classes/tei:memberOf/@key" match="tei:elementSpec"/>
76
  <xsl:key name="odd2odd-IDS" match="tei:*[@xml:id]" use="@xml:id"/>
77
  <xsl:key name="odd2odd-MACROS" use="@ident" match="tei:macroSpec"/>
78
  <xsl:key name="odd2odd-MEMBEROFADD" match="tei:memberOf[not(@mode='delete')]" use="concat(../../@ident,@key)"/>
79
  <xsl:key name="odd2odd-MEMBEROFDELETE" match="tei:memberOf[@mode='delete']" use="concat(../../@ident,@key)"/>
80
  <xsl:key name="odd2odd-MODULES" match="tei:moduleRef" use="@key"/>
81
  <xsl:key name="odd2odd-NEWATTCLASSES" match="tei:classSpec[@type='atts' and @mode='add']" use="@ident"/>
82
  <xsl:key name="odd2odd-REFED" use="@name" match="rng:ref[parent::tei:datatype]"/>
83
  <xsl:key name="odd2odd-REFED" use="@name" match="rng:ref[ancestor::tei:elementSpec]"/>
84
  <xsl:key name="odd2odd-REFED" use="@name" match="rng:ref[ancestor::tei:macroSpec and not(@name=ancestor::tei:macroSpec/@ident)]"/>
85
  <xsl:key name="odd2odd-REFED" use="substring-before(@name,'.attribute')" match="tei:attRef"/>
86
  <xsl:key name="odd2odd-REFED" use="substring-before(@name,'_')" match="rng:ref[contains(@name,'_')]"/>
87
  <xsl:key name="odd2odd-REPLACEATT" match="tei:attDef[@mode='replace']" use="concat(../../@ident,'_',@ident)"/>
88
  <xsl:key name="odd2odd-REPLACECONSTRAINT" match="tei:constraintSpec[@mode='replace']" use="concat(../@ident,'_',@ident)"/>
89

    
90
  <xsl:key name="odd2odd-MODULE_MEMBERS" match="tei:macroSpec"  use="@module"/>
91
  <xsl:key name="odd2odd-MODULE_MEMBERS" match="tei:classSpec"  use="@module"/>
92
  <xsl:key name="odd2odd-MODULE_MEMBERS" match="tei:elementSpec" use="@module"/>
93

    
94
  <xsl:key name="odd2odd-MODULE_MEMBERS_MODEL" match="tei:classSpec" use="@module"/>
95

    
96
  <xsl:key name="odd2odd-SCHEMASPECS" match="tei:schemaSpec" use="@ident"/>
97

    
98
  <xsl:key name="odd2odd-IDENTS" match="tei:macroSpec" use="@ident"/>
99
  <xsl:key name="odd2odd-IDENTS" match="tei:classSpec" use="@ident"/>
100
  <xsl:key name="odd2odd-IDENTS" match="tei:elementSpec" use="@ident"/>
101

    
102
   <!-- all of these use a combination of @ident _and_ @ns (where
103
   present), in case of duplication of names across schemes -->
104

    
105
  <xsl:key name="odd2odd-CHANGE" match="tei:classSpec[@mode='change']" use="concat(@ns,@ident)"/>
106
  <xsl:key name="odd2odd-CHANGE" match="tei:elementSpec[@mode='change']" use="concat(@ns,@ident)"/>
107
  <xsl:key name="odd2odd-CHANGE" match="tei:macroSpec[@mode='change']" use="concat(@ns,@ident)"/>
108
  <xsl:key name="odd2odd-DELETE" match="tei:classSpec[@mode='delete']" use="concat(@ns,@ident)"/>
109
  <xsl:key name="odd2odd-DELETE" match="tei:elementSpec[@mode='delete']" use="concat(@ns,@ident)"/>
110
  <xsl:key name="odd2odd-DELETE" match="tei:macroSpec[@mode='delete']" use="concat(@ns,@ident)"/>
111
  <xsl:key name="odd2odd-REPLACE" match="tei:classSpec[@mode='replace']" use="concat(@ns,@ident)"/>
112
  <xsl:key name="odd2odd-REPLACE" match="tei:elementSpec[@mode='replace']" use="concat(@ns,@ident)"/>
113
  <xsl:key name="odd2odd-REPLACE" match="tei:macroSpec[@mode='replace']" use="concat(@ns,@ident)"/>
114

    
115
  <xsl:variable name="DEFAULTSOURCE">
116
    <xsl:choose>
117
      <xsl:when test="$defaultSource != ''">
118
        <xsl:value-of select="$defaultSource"/>
119
      </xsl:when>
120
      <xsl:when test="$configDirectory != ''">
121
        <xsl:value-of select="$configDirectory"/>
122
        <xsl:text>odd/p5subset.xml</xsl:text>
123
      </xsl:when>
124
      <xsl:otherwise>
125
        <xsl:value-of select="$defaultTEIServer"/>
126
        <xsl:value-of select="$defaultTEIVersion"/>
127
	<xsl:text>/xml/tei/odd/p5subset.xml</xsl:text>
128
      </xsl:otherwise>
129
    </xsl:choose>
130
  </xsl:variable>
131
  
132
  <xsl:variable name="ODD">
133
    <xsl:for-each select="/tei:TEI">
134
      <xsl:copy>
135
	<xsl:attribute name="xml:base" select="document-uri(/)"/>
136
        <xsl:copy-of select="@*"/>
137
        <xsl:if test="$useVersionFromTEI='true'">
138
          <xsl:processing-instruction name="TEIVERSION">
139
            <xsl:call-template name="odd2odd-getversion"/>
140
          </xsl:processing-instruction>
141
        </xsl:if>
142
        <xsl:apply-templates mode="odd2odd-pass0"/>
143
      </xsl:copy>
144
    </xsl:for-each>
145
  </xsl:variable>
146

    
147
  <xsl:variable name="top" select="/"/>
148

    
149

    
150
  <xsl:template match="/">
151
    <xsl:if test="$autoGlobal='true'">
152
      <xsl:message>NOTE: all TEI elements will have global attributes added automatically</xsl:message>
153
    </xsl:if>
154
    <!--
155
    <xsl:result-document href="/tmp/foo.xml">
156
      <xsl:copy-of select="$ODD"/>
157
    </xsl:result-document>
158
    -->
159
    <xsl:for-each select="$ODD">
160
      <xsl:apply-templates mode="odd2odd-pass1"/>
161
    </xsl:for-each>
162
  </xsl:template>
163

    
164
  <!-- ******************* Pass 0, follow and expand specGrp ********************************* -->
165
  <xsl:template match="tei:specGrp" mode="odd2odd-pass0">
166
    <xsl:if test="$verbose='true'">
167
      <xsl:message>Phase 0: summarize specGrp <xsl:value-of select="@xml:id"/>
168
      </xsl:message>
169
    </xsl:if>
170
    <table xmlns="http://www.tei-c.org/ns/1.0">
171
      <xsl:for-each select="*">
172
        <row>
173
          <xsl:choose>
174
            <xsl:when test="self::tei:specGrpRef">
175
              <cell>
176
                <ref target="#{@target}">reference <xsl:value-of select="@target"/></ref>
177
              </cell>
178
              <cell/>
179
            </xsl:when>
180
            <xsl:when test="self::tei:elementSpec">
181
              <cell>
182
		Element <gi><xsl:value-of select="@ident"/></gi>
183
	      </cell>
184
              <cell>
185
                <xsl:value-of select="@mode"/>
186
              </cell>
187
            </xsl:when>
188
            <xsl:when test="self::tei:classSpec">
189
              <cell>
190
		Class <ident type="class"><xsl:value-of select="@ident"/></ident>
191
	      </cell>
192
              <cell>
193
                <xsl:value-of select="@mode"/>
194
              </cell>
195
            </xsl:when>
196
            <xsl:when test="self::tei:macroSpec">
197
              <cell>
198
		Macro <ident type="macro"><xsl:value-of select="@ident"/></ident>
199
	      </cell>
200
              <cell>
201
                <xsl:value-of select="@mode"/>
202
              </cell>
203
            </xsl:when>
204
            <xsl:when test="self::tei:moduleRef">
205
              <cell>
206
		Module <xsl:value-of select="@key"/>
207
	      </cell>
208
              <cell/>
209
	    </xsl:when>
210
          </xsl:choose>
211
        </row>
212
      </xsl:for-each>
213
    </table>
214
  </xsl:template>
215

    
216
  <xsl:template match="tei:schemaSpec" mode="odd2odd-pass0">
217
    <xsl:if test="@ident=$selectedSchema or ($selectedSchema='' and not(preceding-sibling::tei:schemaSpec))">
218
      <xsl:copy>
219
	<xsl:copy-of select="@*"/>
220
	<xsl:choose>
221
	<xsl:when test="@source">
222
	<xsl:if test="$verbose='true'">
223
	  <xsl:message>Source for TEI is <xsl:value-of select="@source"/></xsl:message>
224
	</xsl:if>
225
	</xsl:when>
226
	<xsl:otherwise>
227
	  <xsl:if test="$verbose='true'">
228
	    <xsl:message>Source for TEI will be set to <xsl:value-of select="$DEFAULTSOURCE"/> </xsl:message>
229
	  </xsl:if>
230
	  <xsl:attribute name="source">
231
	    <xsl:value-of select="$DEFAULTSOURCE"/>
232
	  </xsl:attribute>
233
	</xsl:otherwise>
234
	</xsl:choose>
235
	<xsl:apply-templates select="*|text()|processing-instruction()" mode="odd2odd-pass0"/>
236
      </xsl:copy>
237
    </xsl:if>
238
  </xsl:template>
239

    
240
  <xsl:template match="tei:specGrpRef" mode="odd2odd-pass0">
241
    <xsl:if test="$verbose='true'">
242
      <xsl:message>Phase 0: expand specGrpRef <xsl:value-of
243
      select="@target"/> </xsl:message>
244
    </xsl:if>
245
    <xsl:choose>
246
      <xsl:when test="starts-with(@target,'#')">
247
	<xsl:apply-templates  mode="odd2odd-pass0"
248
			      select="id(substring(@target,2))/*"/>
249
      </xsl:when>
250
      <xsl:otherwise>
251
    <xsl:if test="$verbose='true'">
252
      <xsl:message> ... read from <xsl:value-of select="resolve-uri(@target,base-uri(/tei:TEI))"/></xsl:message>
253
    </xsl:if>
254

    
255
	<xsl:for-each 
256
	    select="doc(resolve-uri(@target,base-uri(/tei:TEI)))">
257
	  <xsl:choose>
258
	    <xsl:when test="tei:specGrp">
259
	      <xsl:apply-templates select="tei:specGrp/*" mode="odd2odd-pass0"/>
260
	    </xsl:when>
261
	    <xsl:otherwise>
262
	      <xsl:apply-templates  mode="odd2odd-pass0"/>
263
	    </xsl:otherwise>
264
	  </xsl:choose>
265
	</xsl:for-each>
266
      </xsl:otherwise>
267
    </xsl:choose>
268
  </xsl:template>
269
  
270
  <xsl:template match="text()|@*" mode="odd2odd-pass0">
271
      <xsl:copy-of select="."/>
272
  </xsl:template>
273
  
274
  <xsl:template match="processing-instruction()" mode="odd2odd-pass0">
275
      <xsl:copy-of select="."/>
276
  </xsl:template>
277

    
278
  <xsl:template match="*" mode="odd2odd-pass0">
279
    <xsl:copy>
280
      <xsl:copy-of select="@*"/>
281
      <xsl:apply-templates select="*|text()|processing-instruction()" mode="odd2odd-pass0"/>
282
    </xsl:copy>
283
  </xsl:template>
284

    
285

    
286
  <!-- ******************* Pass 1, follow schemaSpec ********************************* -->
287
  <xsl:template match="@*|processing-instruction()|text()" mode="odd2odd-pass1">
288
    <xsl:copy-of select="."/>
289
  </xsl:template>
290

    
291
  <xsl:template match="*" mode="odd2odd-pass1">
292
    <xsl:copy>
293
      <xsl:apply-templates mode="odd2odd-pass1" select="*|@*|processing-instruction()|text()"/>
294
    </xsl:copy>
295
  </xsl:template>
296

    
297
  <xsl:template match="tei:schemaSpec" mode="odd2odd-pass1">
298
    <xsl:variable name="sourceDoc" select="tei:workOutSource(.)"/>
299

    
300
    <xsl:variable name="oddsource">
301
      <xsl:copy>
302
        <xsl:copy-of select="@*"/>
303
        <xsl:if test="$verbose='true'">
304
          <xsl:message>Schema <xsl:value-of select="@ident"/></xsl:message>
305
        </xsl:if>
306
        <!-- 
307
	     it is important to process "tei" and "core" first 
308
	     because of the order of declarations
309
	-->
310
	<xsl:for-each select="tei:moduleRef[@key='tei']">
311
	  <xsl:call-template name="odd2odd-expandModule">
312
	    <xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
313
	  </xsl:call-template>
314
	</xsl:for-each>
315
	
316
	<xsl:for-each select="tei:moduleRef[@key='core']">
317
	  <xsl:call-template name="odd2odd-expandModule">
318
	    <xsl:with-param name="Source" select="$sourceDoc"
319
			tunnel="yes"/>
320
	  </xsl:call-template>
321
	</xsl:for-each>
322
	
323
	<xsl:for-each select="tei:moduleRef[@key]">
324
	  <xsl:if test="not(@key='tei' or @key='core')">
325
	    <xsl:call-template name="odd2odd-expandModule">
326
	    <xsl:with-param name="Source" select="$sourceDoc"
327
			tunnel="yes"/>
328
	  </xsl:call-template>
329
	  </xsl:if>
330
	</xsl:for-each>
331
	
332
	<xsl:for-each select="tei:macroRef|tei:classRef|tei:elementRef">
333
	    <xsl:call-template name="odd2odd-followRef">
334
	  </xsl:call-template>
335
	</xsl:for-each>
336

    
337
	<xsl:for-each select="tei:classSpec[@mode='add' or not(@mode)]">
338
	  <xsl:call-template name="odd2odd-createCopy">
339
	    <xsl:with-param name="Source" select="$sourceDoc"
340
			tunnel="yes"/>
341
	  </xsl:call-template>
342
	</xsl:for-each>
343

    
344
	<xsl:for-each select="tei:macroSpec[@mode='add' or not(@mode)]">
345
	  <xsl:call-template name="odd2odd-createCopy">
346
	    <xsl:with-param name="Source" select="$sourceDoc"
347
			tunnel="yes"/>
348
	  </xsl:call-template>
349
	</xsl:for-each>
350

    
351
	<xsl:for-each select="tei:elementSpec[@mode='add' or not(@mode)]">
352
	  <xsl:call-template name="odd2odd-createCopy">
353
	    <xsl:with-param name="Source" select="$sourceDoc"
354
			tunnel="yes"/>
355
	  </xsl:call-template>
356
	</xsl:for-each>
357

    
358
	<xsl:apply-templates mode="odd2odd-justcopy" select="tei:moduleRef[@url]"/>
359

    
360
      </xsl:copy>
361

    
362
    </xsl:variable>
363
    <xsl:for-each select="$oddsource">
364
      <xsl:apply-templates mode="odd2odd-pass2"/>
365
    </xsl:for-each>
366
    <!-- constraints -->
367
    <xsl:apply-templates mode="odd2odd-copy" select="tei:constraintSpec"/>
368
  </xsl:template>
369

    
370

    
371
  <xsl:template name="odd2odd-expandModule">
372
    <xsl:variable name="sourceDoc" select="tei:workOutSource(.)"/>
373
    <xsl:variable name="name" select="@key"/>
374
    <xsl:variable name="exc"
375
		  select="concat(' ',normalize-space(@except),' ')"/>
376
    <xsl:variable name="inc"  
377
		  select="concat('/',translate(normalize-space(@include),' ','/'),'/')"/>
378
      <xsl:choose>
379
	<xsl:when test="not(@except) and not(@include)">
380
	  <xsl:for-each select="document($sourceDoc,$top)">
381
	    <xsl:for-each select="key('odd2odd-MODULE_MEMBERS',$name)">
382
	      <xsl:call-template name="odd2odd-checkObject">
383
		<xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
384
		<xsl:with-param name="why"> module <xsl:value-of select="$name"/></xsl:with-param>
385
	      </xsl:call-template>
386
	    </xsl:for-each>
387
	  </xsl:for-each>
388
	</xsl:when>
389
	<xsl:when test="@include">
390
	  <xsl:if test="$verbose='true'">
391
	    <xsl:message>Process module reference to [<xsl:value-of
392
	    select="@key"/>] with inclusion of [<xsl:value-of
393
	    select="@include"/>]</xsl:message>
394
	  </xsl:if>
395
	  <!-- get model and attribute classes regardless -->
396
	  <xsl:for-each select="document($sourceDoc,$top)">
397
	    <xsl:for-each
398
		select="key('odd2odd-MODULE_MEMBERS_MODEL',$name)">
399
	      <xsl:call-template name="odd2odd-checkObject">
400
		<xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
401
		<xsl:with-param name="why">module (auto from include) <xsl:value-of select="$name"/></xsl:with-param>
402
	      </xsl:call-template>
403
	    </xsl:for-each>
404
	    <xsl:for-each
405
		select="key('odd2odd-MODULE_MEMBERS',$name)">
406
	      <xsl:choose>
407
		<xsl:when test="self::tei:classSpec"/>
408
		<xsl:when test="contains($inc,concat('/',@ident,'/'))">
409
		  <xsl:call-template name="odd2odd-checkObject">
410
		    <xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
411
		    <xsl:with-param name="why">(inclusion) module <xsl:value-of select="$name"/></xsl:with-param>
412
		  </xsl:call-template>
413
		</xsl:when>
414
	      </xsl:choose>
415
	    </xsl:for-each>
416
	  </xsl:for-each>
417
	</xsl:when>
418
	<xsl:otherwise>
419
	  <xsl:if test="$verbose='true'">
420
	    <xsl:message>Process module reference to [<xsl:value-of
421
	    select="@key"/>] with exclusion of [<xsl:value-of select="@except"/>]</xsl:message>
422
	  </xsl:if>
423
	  <xsl:for-each select="document($sourceDoc,$top)">
424
	    <xsl:for-each select="key('odd2odd-MODULE_MEMBERS',$name)">
425
	      <xsl:if test="not(contains($exc,concat(' ',@ident,' ')))">
426
		<xsl:call-template name="odd2odd-checkObject">
427
		  <xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
428
		  <xsl:with-param name="why">(exclusion) module <xsl:value-of select="$name"/></xsl:with-param>
429
		</xsl:call-template>
430
	      </xsl:if>
431
	    </xsl:for-each>
432
	    </xsl:for-each>
433
	</xsl:otherwise>
434
      </xsl:choose>
435
  </xsl:template>
436

    
437
  <xsl:template name="odd2odd-followRef">
438
    <xsl:variable name="sourceDoc" select="tei:workOutSource(.)"/>
439
    <xsl:variable name="name" select="@key"/>
440
    <xsl:for-each select="document($sourceDoc,$top)">
441
      <xsl:choose>
442
	<xsl:when test="key('odd2odd-IDENTS',$name)">
443
	  <xsl:for-each select="key('odd2odd-IDENTS',$name)">
444
	    <xsl:call-template name="odd2odd-checkObject">
445
		<xsl:with-param name="Source" select="$sourceDoc" tunnel="yes"/>
446
		<xsl:with-param name="why">direct reference</xsl:with-param>
447
	    </xsl:call-template>
448
	  </xsl:for-each>
449
	</xsl:when>
450
	<xsl:otherwise>
451
	<xsl:call-template name="die">
452
	  <xsl:with-param name="message">
453
	    <xsl:text>Reference to </xsl:text>
454
	    <xsl:value-of select="$name"/>
455
	    <xsl:text>: not found in source</xsl:text>
456
	  </xsl:with-param>
457
	</xsl:call-template>
458
	</xsl:otherwise>
459
      </xsl:choose>
460
    </xsl:for-each>
461
  </xsl:template>
462

    
463
<!-- pass2 -->      
464
  <xsl:template match="rng:ref" mode="odd2odd-pass2">
465
    <xsl:variable name="N">
466
      <xsl:value-of select="@name"/>
467
    </xsl:variable>
468
    <xsl:choose>
469
      <xsl:when test="starts-with($N,'macro.') and $stripped='true'">
470
        <xsl:for-each select="key('odd2odd-MACROS',$N)/tei:content/*">
471
          <xsl:call-template name="odd2odd-simplifyRelax"/>
472
        </xsl:for-each>
473
      </xsl:when>
474
      <xsl:otherwise>
475
	<ref xmlns="http://relaxng.org/ns/structure/1.0">
476
	  <xsl:apply-templates select="@*|*|text()|processing-instruction()" mode="odd2odd-pass2"/>
477
	</ref>
478
      </xsl:otherwise>
479
    </xsl:choose>
480
  </xsl:template>
481

    
482
  <xsl:template match="tei:valDesc|tei:equiv|tei:gloss|tei:desc|tei:remarks|tei:exemplum|tei:listRef" mode="odd2odd-pass2">
483
    <xsl:choose>
484
      <xsl:when test="$stripped='true'"> </xsl:when>
485
      <xsl:otherwise>
486
        <xsl:copy>
487
	  <xsl:apply-templates select="@*|*|text()|processing-instruction()" mode="odd2odd-pass2"/>
488
	</xsl:copy>
489
      </xsl:otherwise>
490
    </xsl:choose>
491
  </xsl:template>
492

    
493
  <xsl:template match="tei:ptr" mode="odd2odd-pass2">
494
    <xsl:choose>
495
      <xsl:when test="starts-with(@target,'#') and 
496
		      (ancestor::tei:remarks or ancestor::tei:listRef or ancestor::tei:valDesc) and
497
		      not(id(substring(@target,2)))">
498
	<xsl:variable name="target" select="substring(@target,2)"/>
499
	<xsl:variable name="sourceDoc" select="tei:workOutSource(.)"/>
500

    
501
	<xsl:choose>
502
	  <xsl:when test="document($sourceDoc)/id($target)">
503
	    <ref  xmlns="http://www.tei-c.org/ns/1.0"
504
		  target="http://www.tei-c.org/release/doc/tei-p5-doc/en/html/{substring($target,1,2)}.html#{$target}">
505
	      <xsl:for-each select="document($DEFAULTSOURCE)/id($target)">
506
		<xsl:number count="tei:div" format="1.1.1."
507
			    level="multiple"/>	  
508
		<xsl:text> </xsl:text>
509
		<xsl:value-of select="tei:head"/>
510
	      </xsl:for-each>
511
	    </ref>
512
	  </xsl:when>
513
	  <xsl:otherwise>
514
	    <xsl:copy>
515
	      <xsl:apply-templates select="@*|*|text()|processing-instruction()" mode="odd2odd-pass2"/>
516
	    </xsl:copy>
517
	  </xsl:otherwise>
518
	</xsl:choose>
519
      </xsl:when>
520
      <xsl:otherwise>
521
	<xsl:copy>
522
	  <xsl:apply-templates select="@*|*|text()|processing-instruction()" mode="odd2odd-pass2"/>
523
	</xsl:copy>
524
      </xsl:otherwise>
525
    </xsl:choose>
526
  </xsl:template>
527

    
528
  <xsl:template match="@*|text()|comment()" mode="odd2odd-pass2">
529
    <xsl:copy-of select="."/>
530
  </xsl:template>
531

    
532
  <xsl:template match="*" mode="odd2odd-pass2">
533
    <xsl:copy>
534
      <xsl:copy-of select="@*"/>
535
      <xsl:apply-templates mode="odd2odd-pass2"/>
536
    </xsl:copy>
537
  </xsl:template>
538

    
539
  <xsl:template match="tei:content" mode="odd2odd-pass2">
540
    <xsl:variable name="content">
541
      <xsl:copy>
542
        <xsl:copy-of select="@*"/>
543
        <xsl:apply-templates mode="odd2odd-pass2"/>
544
      </xsl:copy>
545
    </xsl:variable>
546
    <xsl:apply-templates select="$content" mode="odd2odd-pass3"/>
547
  </xsl:template>
548

    
549

    
550
  <xsl:template match="tei:classSpec" mode="odd2odd-pass2">
551
    <xsl:variable name="used">
552
      <xsl:call-template name="odd2odd-amINeeded"/>
553
    </xsl:variable>
554
    <xsl:choose>
555
      <xsl:when test="$used=''">
556
        <xsl:if test="$verbose='true'">
557
          <xsl:message>Reject unused class <xsl:value-of select="@ident"/>
558
               </xsl:message>
559
        </xsl:if>
560
      </xsl:when>
561
      <xsl:otherwise>
562
        <xsl:copy>
563
          <xsl:copy-of select="@*"/>
564
          <xsl:apply-templates mode="odd2odd-pass2"/>
565
        </xsl:copy>
566
      </xsl:otherwise>
567
    </xsl:choose>
568
  </xsl:template>
569
  <xsl:template name="odd2odd-amINeeded">
570
    <!--
571
How can a class be ok?
572
  a) if an element is a member of it
573
  b) if its referred to in a content model
574
  c) if some other class is a member of it, and that class is OK
575
-->
576
    <xsl:variable name="k" select="@ident"/>
577
    <xsl:choose>
578
      <xsl:when test="$autoGlobal='true' and starts-with(@ident,'att.global')">y</xsl:when>
579
      <xsl:when test="self::tei:classSpec and $stripped='true'">y</xsl:when>
580
      <xsl:when test="key('odd2odd-ELEMENT_MEMBERED',$k)">y</xsl:when>
581
      <xsl:when test="key('odd2odd-REFED',$k)">y</xsl:when>
582
      <xsl:when test="key('odd2odd-CLASS_MEMBERED',$k)">
583
        <xsl:for-each select="key('odd2odd-CLASS_MEMBERED',$k)">
584
          <xsl:call-template name="odd2odd-amINeeded"/>
585
        </xsl:for-each>
586
      </xsl:when>
587
    </xsl:choose>
588
  </xsl:template>
589
  <xsl:template match="tei:macroSpec" mode="odd2odd-pass2">
590
    <xsl:variable name="k">
591
      <xsl:value-of select="@prefix"/>
592
      <xsl:value-of select="@ident"/>
593
    </xsl:variable>
594
    <xsl:choose>
595
      <xsl:when test="$stripped='true' and starts-with($k,'macro.')"/>
596
      <xsl:when test="key('odd2odd-REFED',$k)">
597
	<macroSpec xmlns="http://www.tei-c.org/ns/1.0" >
598
          <xsl:copy-of select="@*"/>
599
          <xsl:apply-templates mode="odd2odd-pass2"/>
600
	</macroSpec>
601
      </xsl:when>
602
      <xsl:otherwise>
603
        <xsl:if test="$verbose='true'">
604
          <xsl:message>Reject unused macro <xsl:value-of select="$k"/></xsl:message>
605
        </xsl:if>
606
      </xsl:otherwise>
607
    </xsl:choose>
608
  </xsl:template>
609

    
610
  <xsl:template name="odd2odd-checkObject">
611
    <xsl:param name="why"/>
612
    <xsl:param name="Source" tunnel="yes"/>
613
    <!--
614
        for every object
615
         - if its in DELETE list, ignore
616
         - if its in REPLACE list, use that
617
         - if its in CHANGE list
618
           (do the hard merge bit)
619
         - otherwise copy 
620
        done
621
  -->
622
      <xsl:variable name="Current" select="."/>
623
      <xsl:variable name="specName">
624
	<xsl:choose>
625
	  <xsl:when test="@ns">
626
	    <xsl:value-of select="@ns"/>
627
	  </xsl:when>
628
	  <xsl:when test="ancestor::tei:schemaSpec/@ns">
629
	    <xsl:value-of select="ancestor::tei:schemaSpec/@ns"/>
630
	  </xsl:when>
631
	</xsl:choose>
632
	<xsl:value-of select="@ident"/>
633
      </xsl:variable>
634
      <xsl:variable name="N" select="local-name(.)"/>
635
      <xsl:for-each select="$ODD">
636
        <xsl:choose>
637
          <xsl:when test="key('odd2odd-DELETE',$specName)">
638
            <xsl:if test="$verbose='true'">
639
              <xsl:message>Phase 1: remove <xsl:value-of select="$specName"/></xsl:message>
640
            </xsl:if>
641
          </xsl:when>
642
	  <xsl:when test="key('odd2odd-REPLACE',$specName)">
643
            <xsl:if test="$verbose='true'">
644
              <xsl:message>Phase 1: replace <xsl:value-of select="$specName"/></xsl:message>
645
            </xsl:if>
646
            <xsl:apply-templates mode="odd2odd-copy" select="key('odd2odd-REPLACE',$specName)"/>
647
          </xsl:when>
648
          <xsl:when test="key('odd2odd-CHANGE',$specName)">
649
            <xsl:if test="$verbose='true'">
650
              <xsl:message>Phase 1: change <xsl:value-of select="$specName"/></xsl:message>
651
            </xsl:if>
652
            <xsl:apply-templates mode="odd2odd-change" select="$Current"/>
653
          </xsl:when>
654
          <xsl:otherwise>
655
            <xsl:if test="$verbose='true'">
656
	      <xsl:message>Phase 1: include <xsl:value-of
657
	      select="$specName"/> from <xsl:value-of
658
	      select="$Source"/> (<xsl:value-of select="$why"/>)</xsl:message>
659
            </xsl:if>
660
            <xsl:apply-templates mode="odd2odd-copy" select="$Current"/>
661
          </xsl:otherwise>
662
        </xsl:choose>
663
      </xsl:for-each>
664
  </xsl:template>
665

    
666
  <xsl:template match="@*|processing-instruction()|text()" mode="odd2odd-change">
667
    <xsl:copy-of select="."/>
668
  </xsl:template>
669

    
670
  <xsl:template match="*" mode="odd2odd-change">
671
    <xsl:copy>
672
      <xsl:apply-templates mode="odd2odd-change" select="*|@*|processing-instruction()|text()"/>
673
    </xsl:copy>
674
  </xsl:template>
675

    
676
  <xsl:template match="@*|processing-instruction()|text()" mode="odd2odd-copy">
677
    <xsl:copy-of select="."/>
678
  </xsl:template>
679

    
680
  <xsl:template match="tei:memberOf" mode="odd2odd-copy">
681
    <xsl:variable name="k" select="@key"/>
682
    <xsl:choose>
683
      <xsl:when test="$ODD/key('odd2odd-DELETE',$k)"/>      
684
      <xsl:otherwise>
685
	<memberOf xmlns="http://www.tei-c.org/ns/1.0" key="{$k}">
686
	  <xsl:copy-of select="@min|@max"/>
687
	</memberOf>
688
      </xsl:otherwise>
689
    </xsl:choose>
690
  </xsl:template>
691

    
692
  <xsl:template match="*" mode="odd2odd-copy">
693
    <xsl:copy>
694
      <xsl:apply-templates mode="odd2odd-copy" select="*|@*|processing-instruction()|text()"/>
695
    </xsl:copy>
696
  </xsl:template>
697

    
698
  <xsl:template match="tei:elementSpec/@mode" mode="odd2odd-change">
699
    <xsl:copy/>
700
  </xsl:template>
701

    
702
  <xsl:template match="tei:listRef" mode="odd2odd-copy"/>
703

    
704
  <xsl:template match="tei:elementSpec" mode="odd2odd-copy">
705
    <xsl:copy>
706
      <xsl:choose>
707
	<xsl:when test="@module"/>
708
	<xsl:when test="ancestor::tei:schemaSpec/@module">
709
	  <xsl:copy-of select="ancestor::tei:schemaSpec/@module"/>
710
	</xsl:when>
711
	<xsl:otherwise>
712
	  <xsl:attribute name="module">
713
	    <xsl:text>derived-module-</xsl:text>
714
	    <xsl:value-of select="ancestor::tei:schemaSpec/@ident"/>
715
	  </xsl:attribute>
716
	</xsl:otherwise>
717
      </xsl:choose>
718
      <xsl:call-template name="odd2odd-copyElementSpec">
719
	<xsl:with-param name="n" select="'1'"/>
720
      </xsl:call-template>
721
    </xsl:copy>
722
  </xsl:template>
723

    
724
  <xsl:template name="odd2odd-copyElementSpec">
725
    <xsl:param name="n"/>
726
    <xsl:param name="Source" tunnel="yes"/>
727
      <xsl:variable name="orig" select="."/>
728
      <xsl:apply-templates mode="odd2odd-copy" select="@*"/>
729
      <xsl:apply-templates mode="odd2odd-justcopy" select="tei:altIdent"/>
730
      <xsl:if test="$stripped='false'">
731
        <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
732
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
733
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
734
      </xsl:if>
735
      <xsl:apply-templates mode="odd2odd-justcopy" select="tei:classes"/>
736
      <xsl:apply-templates mode="odd2odd-copy" select="tei:content"/>
737
      <xsl:apply-templates mode="odd2odd-copy" select="tei:constraintSpec"/>
738
      <attList xmlns="http://www.tei-c.org/ns/1.0">
739
        <xsl:call-template name="odd2odd-addClassAttsToCopy"/>
740
        <xsl:choose>
741
          <xsl:when test="tei:attList[@org='choice']">
742
            <xsl:for-each select="tei:attList">
743
              <xsl:copy>
744
                <xsl:copy-of select="@*"/>
745
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attDef[@mode='add' or not(@mode)]"/>
746
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attRef"/>
747
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList"/>
748
              </xsl:copy>
749
            </xsl:for-each>
750
          </xsl:when>
751
          <xsl:otherwise>
752
            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attDef[@mode='add' or not(@mode)]"/>
753
            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attRef"/>
754
            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attList"/>
755
          </xsl:otherwise>
756
        </xsl:choose>
757
      </attList>
758
      <xsl:if test="$stripped='false'">
759
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
760
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
761
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
762
      </xsl:if>
763
  </xsl:template>
764

    
765
  <xsl:template name="odd2odd-addClassAttsToCopy">
766
    <xsl:if test="$autoGlobal='true' and (not(@ns) or @ns='http://www.tei-c.org/ns/1.0' or @ns='http://www.tei-c.org/ns/Examples')">
767
      <xsl:call-template name="odd2odd-classAttributes">
768
        <xsl:with-param name="whence">1</xsl:with-param>
769
        <xsl:with-param name="elementName" select="@ident"/>
770
        <xsl:with-param name="className" select="'att.global'"/>
771
      </xsl:call-template>
772
    </xsl:if>
773
    <xsl:for-each select="tei:classes/tei:memberOf">
774
      <xsl:call-template name="odd2odd-classAttributes">
775
        <xsl:with-param name="whence">2</xsl:with-param>
776
        <xsl:with-param name="elementName" select="../../@ident"/>
777
        <xsl:with-param name="className" select="@key"/>
778
      </xsl:call-template>
779
    </xsl:for-each>
780
  </xsl:template>
781
  <xsl:template match="tei:elementSpec" mode="odd2odd-change">
782
    <xsl:variable name="elementName">
783
      <xsl:value-of select="concat(@ns,@ident)"/>
784
    </xsl:variable>
785

    
786
    <xsl:variable name="ORIGINAL" select="."/>
787
    <xsl:copy>
788
      <xsl:attribute name="rend">change</xsl:attribute>
789
      <xsl:apply-templates mode="odd2odd-change" select="@*"/>
790
      <!-- 
791
For each element, go through most of the sections one by one
792
and see if they are present in the change mode version.
793
If so, use them as is. The constraints and attributes are identifiable
794
for change individually.
795
 -->
796
      <xsl:for-each select="$ODD">
797
        <xsl:for-each select="key('odd2odd-CHANGE',$elementName)">
798
	  <xsl:if test="$verbose='true'">
799
	    <xsl:message>Change <xsl:value-of select="$elementName"/></xsl:message>
800
	  </xsl:if>
801
          <xsl:copy-of select="@ns"/>
802
          <!-- if there is an altIdent, use it -->
803
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:altIdent"/>
804
          <!-- equiv, gloss, desc trio -->
805
          <xsl:choose>
806
            <xsl:when test="tei:equiv">
807
              <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
808
            </xsl:when>
809
            <xsl:otherwise>
810
              <xsl:for-each select="$ORIGINAL">
811
                <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
812
              </xsl:for-each>
813
            </xsl:otherwise>
814
          </xsl:choose>
815
          <xsl:choose>
816
            <xsl:when test="tei:gloss">
817
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
818
            </xsl:when>
819
            <xsl:otherwise>
820
              <xsl:for-each select="$ORIGINAL">
821
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
822
              </xsl:for-each>
823
            </xsl:otherwise>
824
          </xsl:choose>
825
          <xsl:choose>
826
            <xsl:when test="$stripped='true'"/>
827
            <xsl:when test="tei:desc">
828
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
829
            </xsl:when>
830
            <xsl:otherwise>
831
              <xsl:for-each select="$ORIGINAL">
832
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
833
              </xsl:for-each>
834
            </xsl:otherwise>
835
          </xsl:choose>
836
          <!-- classes -->
837
          <classes xmlns="http://www.tei-c.org/ns/1.0">
838
            <xsl:choose>
839
              <xsl:when test="tei:classes[@mode='change']">
840
                <xsl:for-each select="tei:classes/tei:memberOf">
841
                  <xsl:choose>
842
                    <xsl:when test="@mode='delete'"/>
843
                    <xsl:when test="@mode='add' or not (@mode)">
844
                      <memberOf key="{@key}">
845
			<xsl:copy-of select="@min|@max"/>
846
		      </memberOf>
847
                    </xsl:when>
848
                  </xsl:choose>
849
                </xsl:for-each>
850
                <xsl:for-each select="$ORIGINAL">
851
                  <xsl:for-each select="tei:classes/tei:memberOf">
852
                    <xsl:variable name="me">
853
                      <xsl:value-of select="@key"/>
854
                    </xsl:variable>
855
                    <xsl:variable name="metoo">
856
                      <xsl:value-of select="concat(../../@ident,@key)"/>
857
                    </xsl:variable>
858
                    <xsl:for-each select="$ODD">
859
                      <xsl:choose>
860
                        <xsl:when test="key('odd2odd-DELETE',$me)"> </xsl:when>
861
                        <xsl:when test="key('odd2odd-MEMBEROFDELETE',$metoo)"> </xsl:when>
862
                        <xsl:when test="key('odd2odd-MEMBEROFADD',$metoo)"> </xsl:when>
863
                        <xsl:otherwise>
864
                          <memberOf key="{$me}"/>
865
                        </xsl:otherwise>
866
                      </xsl:choose>
867
                    </xsl:for-each>
868
                  </xsl:for-each>
869
                </xsl:for-each>
870
              </xsl:when>
871
              <xsl:when test="tei:classes">
872
                <xsl:for-each select="tei:classes/tei:memberOf">
873
                  <xsl:copy-of select="."/>
874
                </xsl:for-each>
875
              </xsl:when>
876
              <xsl:otherwise>
877
                <xsl:for-each select="$ORIGINAL">
878
                  <xsl:for-each select="tei:classes/tei:memberOf">
879
                    <xsl:variable name="me">
880
                      <xsl:value-of select="@key"/>
881
                    </xsl:variable>
882
                    <xsl:for-each select="$ODD">
883
                      <xsl:if test="not(key('odd2odd-DELETE',$me))">
884
                        <memberOf key="{$me}"/>
885
                      </xsl:if>
886
                    </xsl:for-each>
887
                  </xsl:for-each>
888
                </xsl:for-each>
889
              </xsl:otherwise>
890
            </xsl:choose>
891
          </classes>
892
          <!-- valList -->
893
          <xsl:choose>
894
            <xsl:when test="tei:valList[@mode='delete']"/>
895
            <xsl:when test="tei:valList">
896
              <xsl:apply-templates mode="odd2odd-copy" select="tei:valList[1]"/>
897
            </xsl:when>
898
            <xsl:otherwise>
899
              <xsl:for-each select="$ORIGINAL">
900
                <xsl:apply-templates mode="odd2odd-copy" select="tei:valList"/>
901
              </xsl:for-each>
902
            </xsl:otherwise>
903
          </xsl:choose>
904
          <!-- element content -->
905
          <content xmlns="http://www.tei-c.org/ns/1.0">
906
            <xsl:choose>
907
              <xsl:when test="tei:content/rng:*">
908
                <xsl:apply-templates mode="odd2odd-copy" select="tei:content/*"/>
909
              </xsl:when>
910
              <xsl:when test="tei:content/tei:*">
911
                <xsl:apply-templates mode="odd2odd-copy" select="tei:content/*"/>
912
              </xsl:when>
913
              <xsl:otherwise>
914
                <xsl:for-each select="$ORIGINAL">
915
                  <xsl:apply-templates mode="odd2odd-copy" select="tei:content/*"/>
916
                </xsl:for-each>
917
              </xsl:otherwise>
918
            </xsl:choose>
919
          </content>
920
          <!-- element constraints -->
921
          <xsl:call-template name="odd2odd-processConstraints">
922
            <xsl:with-param name="ORIGINAL" select="$ORIGINAL"/>
923
            <xsl:with-param name="elementName" select="$elementName"/>
924
          </xsl:call-template>
925
          <!-- attList -->
926
          <attList xmlns="http://www.tei-c.org/ns/1.0">
927
            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/@org"/>
928
            <xsl:call-template name="odd2odd-processAttributes">
929
              <xsl:with-param name="ORIGINAL" select="$ORIGINAL"/>
930
              <xsl:with-param name="elementName" select="$elementName"/>
931
            </xsl:call-template>
932
          </attList>
933
          <!-- exemplum, remarks and listRef are either replacements or not -->
934
          <xsl:choose>
935
            <xsl:when test="$stripped='true'"/>
936
            <xsl:when test="tei:exemplum">
937
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
938
            </xsl:when>
939
            <xsl:otherwise>
940
              <xsl:for-each select="$ORIGINAL">
941
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
942
              </xsl:for-each>
943
            </xsl:otherwise>
944
          </xsl:choose>
945
          <xsl:choose>
946
            <xsl:when test="$stripped='true'"/>
947
            <xsl:when test="tei:remarks">
948
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
949
            </xsl:when>
950
            <xsl:otherwise>
951
              <xsl:for-each select="$ORIGINAL">
952
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
953
              </xsl:for-each>
954
            </xsl:otherwise>
955
          </xsl:choose>
956
          <xsl:choose>
957
            <xsl:when test="tei:listRef">
958
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
959
            </xsl:when>
960
            <xsl:otherwise>
961
              <xsl:for-each select="$ORIGINAL">
962
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
963
              </xsl:for-each>
964
            </xsl:otherwise>
965
          </xsl:choose>
966
        </xsl:for-each>
967
      </xsl:for-each>
968
    </xsl:copy>
969
  </xsl:template>
970
  <xsl:template match="tei:macroSpec" mode="odd2odd-change">
971
    <xsl:variable name="elementName">
972
      <xsl:value-of select="concat(@ns,@ident)"/>
973
    </xsl:variable>
974
    <xsl:variable name="ORIGINAL" select="."/>
975
    <xsl:copy>
976
      <xsl:attribute name="rend">change</xsl:attribute>
977
      <xsl:apply-templates mode="odd2odd-change" select="@*"/>
978
      <!-- 
979
	   For each macro, go through most of the sections one by one
980
	   and see if they are present in the change mode version.
981
	   If so, use them as is. 
982
      -->
983
      <xsl:for-each select="$ODD">
984
        <xsl:for-each select="key('odd2odd-CHANGE',$elementName)">
985
          <!-- if there is an altIdent, use it -->
986
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:altIdent"/>
987
          <!-- equiv, gloss, desc trio -->
988
          <xsl:choose>
989
            <xsl:when test="$stripped='true'"/>
990
            <xsl:when test="tei:equiv">
991
              <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
992
            </xsl:when>
993
            <xsl:otherwise>
994
              <xsl:for-each select="$ORIGINAL">
995
                <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
996
              </xsl:for-each>
997
            </xsl:otherwise>
998
          </xsl:choose>
999
          <xsl:choose>
1000
            <xsl:when test="tei:gloss">
1001
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
1002
            </xsl:when>
1003
            <xsl:otherwise>
1004
              <xsl:for-each select="$ORIGINAL">
1005
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
1006
              </xsl:for-each>
1007
            </xsl:otherwise>
1008
          </xsl:choose>
1009
          <xsl:choose>
1010
            <xsl:when test="$stripped='true'"/>
1011
            <xsl:when test="tei:desc">
1012
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1013
            </xsl:when>
1014
            <xsl:otherwise>
1015
              <xsl:for-each select="$ORIGINAL">
1016
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1017
              </xsl:for-each>
1018
            </xsl:otherwise>
1019
          </xsl:choose>
1020
          <!-- content -->
1021
          <xsl:choose>
1022
            <xsl:when test="tei:content">
1023
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:content"/>
1024
            </xsl:when>
1025
            <xsl:otherwise>
1026
              <xsl:for-each select="$ORIGINAL">
1027
                <xsl:apply-templates mode="odd2odd-copy" select="tei:content"/>
1028
              </xsl:for-each>
1029
            </xsl:otherwise>
1030
          </xsl:choose>
1031
          <xsl:choose>
1032
            <xsl:when test="tei:valList">
1033
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:valList[1]"/>
1034
            </xsl:when>
1035
            <xsl:when test="tei:stringVal">
1036
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:stringVal"/>
1037
            </xsl:when>
1038
            <xsl:otherwise>
1039
              <xsl:for-each select="$ORIGINAL">
1040
                <xsl:apply-templates mode="odd2odd-copy" select="tei:stringVal"/>
1041
              </xsl:for-each>
1042
            </xsl:otherwise>
1043
          </xsl:choose>
1044
          <!-- exemplum, remarks and listRef are either replacements or not -->
1045
          <xsl:choose>
1046
            <xsl:when test="$stripped='true'"/>
1047
            <xsl:when test="tei:exemplum">
1048
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
1049
            </xsl:when>
1050
            <xsl:otherwise>
1051
              <xsl:for-each select="$ORIGINAL">
1052
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
1053
              </xsl:for-each>
1054
            </xsl:otherwise>
1055
          </xsl:choose>
1056
          <xsl:choose>
1057
            <xsl:when test="tei:remarks">
1058
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
1059
            </xsl:when>
1060
            <xsl:otherwise>
1061
              <xsl:for-each select="$ORIGINAL">
1062
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
1063
              </xsl:for-each>
1064
            </xsl:otherwise>
1065
          </xsl:choose>
1066
          <xsl:choose>
1067
            <xsl:when test="$stripped='true'"/>
1068
            <xsl:when test="tei:listRef">
1069
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
1070
            </xsl:when>
1071
            <xsl:otherwise>
1072
              <xsl:for-each select="$ORIGINAL">
1073
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
1074
              </xsl:for-each>
1075
            </xsl:otherwise>
1076
          </xsl:choose>
1077
        </xsl:for-each>
1078
      </xsl:for-each>
1079
    </xsl:copy>
1080
  </xsl:template>
1081
  <xsl:template match="tei:classSpec" mode="odd2odd-change">
1082
    <xsl:variable name="className">
1083
	<xsl:value-of select="concat(@ns,@ident)"/>
1084
    </xsl:variable>
1085
    <xsl:variable name="ORIGINAL" select="."/>
1086
    <xsl:copy>
1087
      <xsl:attribute name="rend">change</xsl:attribute>
1088
      <xsl:apply-templates mode="odd2odd-change" select="@*"/>
1089
      <!-- for each section of the class spec, 
1090
     go through the sections one by one
1091
     and see if they are present in the change mode version -->
1092
      <xsl:for-each select="$ODD">
1093
        <xsl:for-each select="key('odd2odd-CHANGE',$className)">
1094
          <!-- context is now a classSpec in change mode in the ODD spec -->
1095
          <!-- description -->
1096
          <xsl:choose>
1097
            <xsl:when test="$stripped='true'"/>
1098
            <xsl:when test="tei:desc">
1099
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1100
            </xsl:when>
1101
            <xsl:otherwise>
1102
              <xsl:for-each select="$ORIGINAL">
1103
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1104
              </xsl:for-each>
1105
            </xsl:otherwise>
1106
          </xsl:choose>
1107
          <!-- classes -->
1108
          <classes xmlns="http://www.tei-c.org/ns/1.0">
1109
            <xsl:choose>
1110
              <xsl:when test="tei:classes[@mode='change']">
1111
                <xsl:for-each select="tei:classes/tei:memberOf">
1112
                  <xsl:choose>
1113
                    <xsl:when test="@mode='delete'"/>
1114
                    <xsl:when test="@mode='add' or not (@mode)">
1115
                      <memberOf key="{@key}"/>
1116
                    </xsl:when>
1117
                  </xsl:choose>
1118
                </xsl:for-each>
1119
                <xsl:for-each select="$ORIGINAL">
1120
                  <xsl:for-each select="tei:classes/tei:memberOf">
1121
                    <xsl:variable name="me">
1122
                      <xsl:value-of select="@key"/>
1123
                    </xsl:variable>
1124
                    <xsl:variable name="metoo">
1125
                      <xsl:value-of select="concat(../../@ident,@key)"/>
1126
                    </xsl:variable>
1127
                    <xsl:for-each select="$ODD">
1128
                      <xsl:choose>
1129
                        <xsl:when test="key('odd2odd-DELETE',$me)"> </xsl:when>
1130
                        <xsl:when test="key('odd2odd-MEMBEROFDELETE',$metoo)"> </xsl:when>
1131
                        <xsl:when test="key('odd2odd-MEMBEROFADD',$metoo)"> </xsl:when>
1132
                        <xsl:otherwise>
1133
                          <memberOf key="{$me}"/>
1134
                        </xsl:otherwise>
1135
                      </xsl:choose>
1136
                    </xsl:for-each>
1137
                  </xsl:for-each>
1138
                </xsl:for-each>
1139
              </xsl:when>
1140
              <xsl:when test="tei:classes">
1141
                <xsl:for-each select="tei:classes/tei:memberOf">
1142
                  <xsl:copy-of select="."/>
1143
                </xsl:for-each>
1144
              </xsl:when>
1145
              <xsl:otherwise>
1146
                <xsl:for-each select="$ORIGINAL">
1147
                  <xsl:for-each select="tei:classes/tei:memberOf">
1148
                    <xsl:variable name="me">
1149
                      <xsl:value-of select="@key"/>
1150
                    </xsl:variable>
1151
                    <xsl:for-each select="$ODD">
1152
                      <xsl:if test="not(key('odd2odd-DELETE',$me))">
1153
                        <memberOf key="{$me}"/>
1154
                      </xsl:if>
1155
                    </xsl:for-each>
1156
                  </xsl:for-each>
1157
                </xsl:for-each>
1158
              </xsl:otherwise>
1159
            </xsl:choose>
1160
          </classes>
1161
          <!-- attList -->
1162
          <attList xmlns="http://www.tei-c.org/ns/1.0">
1163
            <xsl:call-template name="odd2odd-processAttributes">
1164
              <xsl:with-param name="ORIGINAL" select="$ORIGINAL"/>
1165
              <xsl:with-param name="elementName" select="''"/>
1166
            </xsl:call-template>
1167
            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attRef"/>
1168
          </attList>
1169
        </xsl:for-each>
1170
      </xsl:for-each>
1171
    </xsl:copy>
1172
  </xsl:template>
1173
  <xsl:template match="rng:choice|rng:list|rng:group|rng:optional|rng:oneOrMore|rng:zeroOrMore" mode="odd2odd-copy">
1174
    <xsl:call-template name="odd2odd-simplifyRelax"/>
1175
  </xsl:template>
1176
  <xsl:template name="odd2odd-simplifyRelax">
1177
    <xsl:variable name="element">
1178
      <xsl:value-of select="local-name(.)"/>
1179
    </xsl:variable>
1180
    <!-- 
1181
for each RELAX NG content model,
1182
remove reference to any elements which have been
1183
deleted, or to classes which are empty.
1184
This may make the container empty,
1185
so that is only put back in if there is some content
1186
-->
1187
    <xsl:variable name="contents">
1188
      <WHAT>
1189
        <xsl:for-each select="a:*|rng:*|processing-instruction()">
1190
          <xsl:choose>
1191
            <xsl:when test="self::a:*">
1192
              <xsl:copy-of select="."/>
1193
            </xsl:when>
1194
            <xsl:when test="self::processing-instruction()">
1195
              <xsl:copy-of select="."/>
1196
            </xsl:when>
1197
            <xsl:when test="self::rng:element">
1198
              <element xmlns="http://relaxng.org/ns/structure/1.0">
1199
                <xsl:copy-of select="@*"/>
1200
                <xsl:apply-templates mode="odd2odd-copy"/>
1201
              </element>
1202
            </xsl:when>
1203
            <xsl:when test="self::rng:name">
1204
              <name xmlns="http://relaxng.org/ns/structure/1.0">
1205
                <xsl:copy-of select="@*"/>
1206
                <xsl:apply-templates mode="odd2odd-copy"/>
1207
              </name>
1208
            </xsl:when>
1209
            <xsl:when test="self::rng:attribute">
1210
              <attribute xmlns="http://relaxng.org/ns/structure/1.0">
1211
                <xsl:copy-of select="@*"/>
1212
                <xsl:apply-templates mode="odd2odd-copy"/>
1213
              </attribute>
1214
            </xsl:when>
1215
            <xsl:when test="self::rng:data">
1216
              <data xmlns="http://relaxng.org/ns/structure/1.0">
1217
                <xsl:copy-of select="@*"/>
1218
                <xsl:apply-templates mode="odd2odd-copy"/>
1219
              </data>
1220
            </xsl:when>
1221
            <xsl:when test="self::rng:text">
1222
              <text xmlns="http://relaxng.org/ns/structure/1.0"/>
1223
            </xsl:when>
1224
            <xsl:when test="self::rng:value">
1225
              <value xmlns="http://relaxng.org/ns/structure/1.0">
1226
                <xsl:apply-templates/>
1227
              </value>
1228
            </xsl:when>
1229
            <xsl:when test="self::rng:ref">
1230
              <xsl:variable name="N" select="@name"/>
1231
              <xsl:for-each select="$ODD">
1232
                <xsl:choose>
1233
                  <xsl:when test="$stripped='true'">
1234
                    <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$N}"/>
1235
                  </xsl:when>
1236
                  <xsl:when test="key('odd2odd-DELETE',$N)"/>
1237
                  <xsl:otherwise>
1238
                    <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$N}"/>
1239
                  </xsl:otherwise>
1240
                </xsl:choose>
1241
              </xsl:for-each>
1242
            </xsl:when>
1243
            <xsl:otherwise>
1244
              <xsl:call-template name="odd2odd-simplifyRelax"/>
1245
            </xsl:otherwise>
1246
          </xsl:choose>
1247
        </xsl:for-each>
1248
      </WHAT>
1249
    </xsl:variable>
1250
    <xsl:variable name="entCount">
1251
      <xsl:for-each select="$contents/WHAT">
1252
        <xsl:value-of select="count(*)"/>
1253
      </xsl:for-each>
1254
    </xsl:variable>
1255
    <xsl:for-each select="$contents/WHAT">
1256
      <xsl:choose>
1257
        <xsl:when test="$entCount=1     and local-name(*)=$element">
1258
          <xsl:copy-of select="*|@*|text()|processing-instruction()"/>
1259
        </xsl:when>
1260
        <xsl:when test="$element='optional'     and $entCount=1     and rng:zeroOrMore">
1261
          <xsl:copy-of select="*|@*|text()|processing-instruction()"/>
1262
        </xsl:when>
1263
        <xsl:when test="$element='optional'     and $entCount=1     and rng:oneOrMore">
1264
          <xsl:copy-of select="*|@*|text()|processing-instruction()"/>
1265
        </xsl:when>
1266
        <xsl:when test="$element='oneOrMore'     and $entCount=1     and rng:zeroOrMore">
1267
          <oneOrMore xmlns="http://relaxng.org/ns/structure/1.0">
1268
            <xsl:copy-of select="rng:zeroOrMore/*"/>
1269
          </oneOrMore>
1270
        </xsl:when>
1271
        <xsl:when test="self::rng:zeroOrMore/rng:ref/@name='model.global'        and preceding-sibling::rng:*[1][self::rng:zeroOrMore/rng:ref/@name='model.global']"/>
1272
        <xsl:when test="$entCount&gt;0 or $stripped='true'">
1273
          <xsl:element xmlns="http://relaxng.org/ns/structure/1.0" name="{$element}">
1274
            <xsl:copy-of select="*|@*|text()|processing-instruction()"/>
1275
          </xsl:element>
1276
        </xsl:when>
1277
      </xsl:choose>
1278
    </xsl:for-each>
1279
  </xsl:template>
1280

    
1281
  <xsl:template name="odd2odd-classAttributes">
1282
    <xsl:param name="elementName"/>
1283
    <xsl:param name="className"/>
1284
    <xsl:param name="whence"/>
1285
    <xsl:param name="Source" tunnel="yes"/>
1286

    
1287
    <!-- 
1288
    On entry, we are sitting on an <elementSpec> or <classSpec> 
1289
    and seeing if we can pick up some attributes for 
1290
    $elementName. We travel to the ODD first
1291
    to see if it has some overrides
1292
    -->
1293
    <xsl:for-each select="$ODD">
1294
      <xsl:choose>
1295
        <xsl:when test="key('odd2odd-MEMBEROFDELETE',concat($elementName,$className))"> </xsl:when>
1296
        <!-- the class is referenced in the ODD and has redefined <classes>-->
1297
        <xsl:when test="key('odd2odd-ATTCLASSES',$className)/tei:classes">
1298
          <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1299
            <xsl:call-template name="odd2odd-processClassAttributes">
1300
              <xsl:with-param name="elementName" select="$elementName"/>
1301
              <xsl:with-param name="className" select="$className"/>
1302
              <xsl:with-param name="whence" select="$whence"/>
1303
              <xsl:with-param name="fromODD">true</xsl:with-param>
1304
            </xsl:call-template>
1305
          </xsl:for-each>
1306
        </xsl:when>
1307
        <!-- the class is referenced in the ODD and has redefined <attList>-->
1308
        <xsl:when test="key('odd2odd-ATTCLASSES',$className)/tei:attList">
1309
          <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1310
            <xsl:call-template name="odd2odd-processClassAttributes">
1311
              <xsl:with-param name="elementName" select="$elementName"/>
1312
              <xsl:with-param name="className" select="$className"/>
1313
              <xsl:with-param name="whence" select="$whence"/>
1314
              <xsl:with-param name="fromODD">true</xsl:with-param>
1315
            </xsl:call-template>
1316
          </xsl:for-each>
1317
        </xsl:when>
1318
        <!-- otherwise, we'll revert to source
1319
	     (assuming the class is of type 'atts')
1320
	-->
1321
        <xsl:otherwise>
1322
          <xsl:for-each select="document($Source)/tei:TEI">
1323
            <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1324
              <xsl:call-template name="odd2odd-processClassAttributes">
1325
                <xsl:with-param name="elementName" select="$elementName"/>
1326
                <xsl:with-param name="className" select="$className"/>
1327
                <xsl:with-param name="whence" select="$whence"/>
1328
                <xsl:with-param name="fromODD">false</xsl:with-param>
1329
              </xsl:call-template>
1330
            </xsl:for-each>
1331
          </xsl:for-each>
1332
	</xsl:otherwise>
1333
      </xsl:choose>
1334
    </xsl:for-each>
1335
  </xsl:template>
1336

    
1337
  <xsl:template name="odd2odd-processClassAttributes">
1338
    <xsl:param name="elementName"/>
1339
    <xsl:param name="className"/>
1340
    <xsl:param name="fromODD">false</xsl:param>
1341
    <xsl:param name="whence"/>
1342
    <xsl:param name="Source" tunnel="yes"/>
1343

    
1344
    <!-- we are sitting on a attribute classSpec, could be in the ODD
1345
	 or could be in the source -->
1346
    <xsl:variable name="M" select="@module"/>
1347
    <!-- decide whether to proceed with this class at all. if we don't
1348
    include this class specifically, don't have this module, or we
1349
    have deleted the class, 
1350
    we can bypass it now.-->
1351
    <xsl:variable name="use">
1352
      <xsl:choose>
1353
        <xsl:when test="$fromODD='true' and (not(@mode) or @mode='add')">
1354
          <xsl:text>true</xsl:text>
1355
        </xsl:when>
1356
        <xsl:when test="not(@module)">
1357
          <xsl:text>true</xsl:text>
1358
        </xsl:when>
1359
        <xsl:otherwise>
1360
          <xsl:for-each select="$ODD">
1361
            <xsl:choose>
1362
              <xsl:when test="key('odd2odd-DELETE',$className)">false</xsl:when>
1363
              <xsl:when test="key('odd2odd-CLASSREFS',$className)">true</xsl:when>
1364
              <xsl:when test="key('odd2odd-MODULES',$M)">
1365
                <xsl:text>true</xsl:text>
1366
              </xsl:when>
1367
	      <xsl:otherwise>false</xsl:otherwise>
1368
            </xsl:choose>
1369
          </xsl:for-each>
1370
        </xsl:otherwise>
1371
      </xsl:choose>
1372
    </xsl:variable>
1373
    <!-- DEBUG-->
1374
    <!--
1375
    <xsl:message>START <xsl:value-of select="$whence"/>: <xsl:value-of select="$elementName"/> + <xsl:value-of
1376
    select="$className"/> [<xsl:value-of select="$M"/>] + <xsl:value-of
1377
    select="$fromODD"/>, so use=<xsl:value-of select="$use"/></xsl:message>
1378
    -->
1379
    <xsl:if test="$use='true'">
1380
      <!-- 
1381
	   We need to put in the class attributes. We'll 
1382
	   use the value of $fromODD to see whether this is in the
1383
	   current ODD or the source.
1384
	   
1385
	   a) the class is new in this customization, add all attributes regardless
1386
	   b) the class is marked for deletion. do nothing
1387
	   c) the class is marked for replacement. reference attributes from the replacement
1388
	   d) the class is marked for change. compare attributes (tedious)
1389
	   e) the class has no replacement; reference its attributes
1390
	   
1391
	   In each case, once we have a potential attribute, we have to check
1392
	   back to see if it is changed in the element (mergeClassAttribute)
1393
	   
1394
	   First, establish whether any attributes in the class,
1395
	   inherited or otherwise, are changed in the ODD. We will
1396
	   use this information later
1397
      -->
1398
      <xsl:variable name="anyChanged">
1399
        <xsl:choose>
1400
          <xsl:when test="$fromODD='false'">
1401
            <xsl:call-template name="odd2odd-checkClassAttribute">
1402
              <xsl:with-param name="element" select="$elementName"/>
1403
            </xsl:call-template>
1404
          </xsl:when>
1405
          <xsl:otherwise>
1406
            <xsl:for-each select="document($Source)/tei:TEI">
1407
              <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1408
                <xsl:call-template name="odd2odd-checkClassAttribute">
1409
                  <xsl:with-param name="element" select="$elementName"/>
1410
                </xsl:call-template>
1411
              </xsl:for-each>
1412
            </xsl:for-each>
1413
	  </xsl:otherwise>
1414
        </xsl:choose>
1415
      </xsl:variable>
1416
      <!--
1417
	  <xsl:message>Class <xsl:value-of select="$className"/>, element
1418
	  <xsl:value-of select="$elementName"/>: has changes: <xsl:value-of
1419
	  select="$anyChanged"/></xsl:message>
1420
      -->
1421
      <xsl:choose>
1422
        <!-- a) new class in ODD -->
1423
        <xsl:when test="$fromODD='true' and (not(@mode) or @mode='add')">
1424
          <attRef xmlns="http://www.tei-c.org/ns/1.0" n="1" name="{$className}.attributes"/>
1425
        </xsl:when>
1426
        <!-- b) its deleted -->
1427
        <xsl:when test="@mode='delete'"/>
1428
        <!-- c) its a replacement -->
1429
        <xsl:when test="@mode='replace'">
1430
          <xsl:for-each select="tei:attList/tei:attDef">
1431
            <xsl:call-template name="odd2odd-mergeClassAttribute">
1432
              <xsl:with-param name="source">1</xsl:with-param>
1433
              <xsl:with-param name="element" select="$elementName"/>
1434
              <xsl:with-param name="class" select="$className"/>
1435
              <xsl:with-param name="fromODD">
1436
                <xsl:value-of select="$fromODD"/>
1437
              </xsl:with-param>
1438
            </xsl:call-template>
1439
          </xsl:for-each>
1440
          <xsl:for-each select="tei:attList/tei:attList">
1441
            <attList xmlns="http://www.tei-c.org/ns/1.0">
1442
              <xsl:copy-of select="@org"/>
1443
              <xsl:for-each select="tei:attDef">
1444
                <xsl:call-template name="odd2odd-mergeClassAttribute">
1445
                  <xsl:with-param name="source">2</xsl:with-param>
1446
                  <xsl:with-param name="element" select="$elementName"/>
1447
                  <xsl:with-param name="class" select="$className"/>
1448
                  <xsl:with-param name="fromODD">
1449
                    <xsl:value-of select="$fromODD"/>
1450
                  </xsl:with-param>
1451
                </xsl:call-template>
1452
              </xsl:for-each>
1453
            </attList>
1454
          </xsl:for-each>
1455
        </xsl:when>
1456
        <!-- d) there are changes to attributes in the class spec itself,
1457
	but the element makes no override -->
1458
        <xsl:when test="@mode='change' and tei:attList and not (contains($anyChanged,':element-'))">
1459
          <xsl:if test="$verbose='true'">
1460
            <xsl:message>Phase 1 check d) Class <xsl:value-of select="$className"/> for <xsl:value-of select="$elementName"/> has no changes in element, refer by name</xsl:message>
1461
          </xsl:if>
1462
          <attRef xmlns="http://www.tei-c.org/ns/1.0" n="5" name="{$className}.attributes"/>
1463
        </xsl:when>
1464
        <!-- e) there are changes to attributes in the class spec itself -->
1465
        <xsl:when test="@mode='change' and tei:attList">
1466
          <!-- always references attributes in add mode -->
1467
          <xsl:for-each select="tei:attList/tei:attDef[@mode='add']">
1468
            <attRef xmlns="http://www.tei-c.org/ns/1.0" n="2" name="{$className}.attribute.{translate(@ident,':','')}"/>
1469
          </xsl:for-each>
1470
          <!-- go back to original and proceed from there -->
1471
	  <xsl:for-each select="document($Source)/tei:TEI">
1472
	    <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1473
	      <xsl:call-template name="odd2odd-tryAttributes">
1474
		<xsl:with-param name="elementName" select="$elementName"/>
1475
		<xsl:with-param name="className" select="$className"/>
1476
		<xsl:with-param name="fromODD">
1477
		  <xsl:value-of select="$fromODD"/>
1478
		</xsl:with-param>
1479
	      </xsl:call-template>
1480
	    </xsl:for-each>
1481
	  </xsl:for-each>
1482
          <xsl:for-each select="tei:attList/tei:attRef">
1483
            <xsl:copy-of select="."/>
1484
          </xsl:for-each>
1485
        </xsl:when>
1486
        <!-- there are no changes to the attributes in the odd-->
1487
        <xsl:when test="$anyChanged=''">
1488
          <xsl:if test="$verbose='true'">
1489
            <xsl:message>Phase 1 check f) Class <xsl:value-of select="$className"/> for <xsl:value-of select="$elementName"/> has no changes, refer by name</xsl:message>
1490
          </xsl:if>
1491
          <attRef xmlns="http://www.tei-c.org/ns/1.0" n="4" name="{$className}.attributes"/>
1492
        </xsl:when>
1493
        <xsl:otherwise>
1494
          <xsl:if test="$verbose='true'">
1495
            <xsl:message>Phase 1 check g) Class <xsl:value-of select="$className"/> for <xsl:value-of select="$elementName"/> has changes in odd, refer by values</xsl:message>
1496
          </xsl:if>
1497
          <!-- attributes here -->
1498
          <xsl:for-each select="tei:attList/tei:attDef">
1499
            <xsl:call-template name="odd2odd-mergeClassAttribute">
1500
              <xsl:with-param name="source">7</xsl:with-param>
1501
              <xsl:with-param name="element" select="$elementName"/>
1502
              <xsl:with-param name="class" select="$className"/>
1503
              <xsl:with-param name="fromODD">
1504
                <xsl:value-of select="$fromODD"/>
1505
              </xsl:with-param>
1506
            </xsl:call-template>
1507
          </xsl:for-each>
1508
          <!-- embedded attribute lists  -->
1509
          <xsl:for-each select="tei:attList/tei:attList">
1510
            <attList xmlns="http://www.tei-c.org/ns/1.0">
1511
              <xsl:copy-of select="@org"/>
1512
              <xsl:for-each select="tei:attDef">
1513
                <xsl:call-template name="odd2odd-mergeClassAttribute">
1514
                  <xsl:with-param name="source">8</xsl:with-param>
1515
                  <xsl:with-param name="element" select="$elementName"/>
1516
                  <xsl:with-param name="class" select="$className"/>
1517
                  <xsl:with-param name="fromODD">
1518
                    <xsl:value-of select="$fromODD"/>
1519
                  </xsl:with-param>
1520
                </xsl:call-template>
1521
              </xsl:for-each>
1522
            </attList>
1523
          </xsl:for-each>
1524
        </xsl:otherwise>
1525
      </xsl:choose>
1526
      <!-- Now attributes referenced from classes we are a member
1527
     of. Again, check whether we are in ODD or not. If
1528
     we have not tinkered with the superclass, this is not
1529
     needed, obviously.
1530
      -->
1531
      <!-- DEBUG
1532
      <xsl:message>Now time to look at subclasses of <xsl:value-of
1533
      select="@ident"/> whose changes status was <xsl:value-of
1534
      select="$anyChanged"/>; we are in fromOdd <xsl:value-of
1535
      select="$fromODD"/></xsl:message>
1536
-->
1537
      <xsl:choose>
1538
        <xsl:when test="@mode='change' and tei:attList and not (contains($anyChanged,':element-'))"/>
1539
        <xsl:when test="$fromODD='false' and $anyChanged =''"/>
1540
        <xsl:when test="$fromODD='true' and not(.//tei:attDef)"/>
1541
        <xsl:when test="$fromODD='true' and tei:classes[@mode='replace']">
1542
          <xsl:for-each select="tei:classes/tei:memberOf">
1543
            <xsl:call-template name="odd2odd-classAttributes">
1544
              <xsl:with-param name="whence">3</xsl:with-param>
1545
              <xsl:with-param name="elementName" select="$elementName"/>
1546
              <xsl:with-param name="className" select="@key"/>
1547
            </xsl:call-template>
1548
          </xsl:for-each>
1549
        </xsl:when>
1550
        <xsl:otherwise>
1551
          <xsl:if test="$fromODD='true' and tei:classes[@mode='change']">
1552
            <xsl:for-each select="tei:classes/tei:memberOf[@mode='add']">
1553
              <xsl:call-template name="odd2odd-classAttributes">
1554
                <xsl:with-param name="whence">4</xsl:with-param>
1555
                <xsl:with-param name="elementName" select="$elementName"/>
1556
                <xsl:with-param name="className" select="@key"/>
1557
              </xsl:call-template>
1558
            </xsl:for-each>
1559
          </xsl:if>
1560
	  <xsl:for-each select="document($Source)/tei:TEI">
1561
	    <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
1562
	      <xsl:for-each select="tei:classes/tei:memberOf">
1563
		<xsl:call-template name="odd2odd-classAttributes">
1564
		  <xsl:with-param name="whence">5</xsl:with-param>
1565
		  <xsl:with-param name="elementName" select="$elementName"/>
1566
		  <xsl:with-param name="className" select="@key"/>
1567
		</xsl:call-template>
1568
	      </xsl:for-each>
1569
	    </xsl:for-each>
1570
	  </xsl:for-each>
1571
        </xsl:otherwise>
1572
      </xsl:choose>
1573
    </xsl:if>
1574
  </xsl:template>
1575
  <xsl:template name="odd2odd-tryAttributes">
1576
    <xsl:param name="elementName"/>
1577
    <xsl:param name="className"/>
1578
    <xsl:param name="fromODD"/>
1579
    <xsl:for-each select="tei:attList/tei:attDef">
1580
      <xsl:call-template name="odd2odd-mergeClassAttribute">
1581
        <xsl:with-param name="source">3</xsl:with-param>
1582
        <xsl:with-param name="element" select="$elementName"/>
1583
        <xsl:with-param name="class" select="$className"/>
1584
        <xsl:with-param name="fromODD">
1585
          <xsl:value-of select="$fromODD"/>
1586
        </xsl:with-param>
1587
      </xsl:call-template>
1588
    </xsl:for-each>
1589
    <xsl:for-each select="tei:attList/tei:attList">
1590
      <attList xmlns="http://www.tei-c.org/ns/1.0">
1591
        <xsl:copy-of select="@org"/>
1592
        <xsl:for-each select="tei:attDef">
1593
          <xsl:call-template name="odd2odd-mergeClassAttribute">
1594
            <xsl:with-param name="source">4</xsl:with-param>
1595
            <xsl:with-param name="element" select="$elementName"/>
1596
            <xsl:with-param name="class" select="$className"/>
1597
            <xsl:with-param name="fromODD">
1598
              <xsl:value-of select="$fromODD"/>
1599
            </xsl:with-param>
1600
          </xsl:call-template>
1601
        </xsl:for-each>
1602
      </attList>
1603
    </xsl:for-each>
1604
  </xsl:template>
1605
  <xsl:template name="odd2odd-mergeClassAttribute">
1606
    <!-- 
1607
	 sitting on a source class. look at the 
1608
	 attribute and see whether it has changed
1609
	 in the customization
1610
    -->
1611
    <xsl:param name="source"/>
1612
    <xsl:param name="element"/>
1613
    <xsl:param name="class"/>
1614
    <xsl:param name="fromODD"/>
1615
    <xsl:variable name="att" select="@ident"/>
1616
    <xsl:variable name="wherefrom" select="."/>
1617
    <xsl:variable name="attRef">
1618
      <xsl:value-of select="concat($class,'.attribute.' ,translate($att,':',''),'_',$element)"/>
1619
    </xsl:variable>
1620
    <xsl:variable name="lookingAt">
1621
      <xsl:value-of select="concat($element,'_',@ident)"/>
1622
    </xsl:variable>
1623
    <xsl:for-each select="$ODD">
1624
      <xsl:choose>
1625
        <!-- deleted in the customization at the class level -->
1626
        <xsl:when test="key('odd2odd-DELETEATT',concat($class,'_',$att))"/>
1627
        <!-- deleted in the customization at the element level -->
1628
        <xsl:when test="key('odd2odd-DELETEATT',$lookingAt)"/>
1629
        <!-- replaced in the customization at the element level -->
1630
        <xsl:when test="key('odd2odd-REPLACEATT',$lookingAt)"/>
1631
        <!-- changed in the customization by the element -->
1632
        <xsl:when test="key('odd2odd-CHANGEATT',$lookingAt)">
1633
          <xsl:call-template name="odd2odd-mergeAttribute">
1634
            <xsl:with-param name="New" select="key('odd2odd-CHANGEATT',$lookingAt)"/>
1635
            <xsl:with-param name="Old" select="$wherefrom"/>
1636
          </xsl:call-template>
1637
        </xsl:when>
1638
        <xsl:otherwise>
1639
          <xsl:choose>
1640
            <xsl:when test="$fromODD='false'">
1641
              <xsl:for-each select="$wherefrom">
1642
                <xsl:call-template name="odd2odd-unChangedAtt">
1643
                  <xsl:with-param name="debug">1</xsl:with-param>
1644
                  <xsl:with-param name="lookingAt">
1645
                    <xsl:value-of select="$lookingAt"/>
1646
                  </xsl:with-param>
1647
                  <xsl:with-param name="att">
1648
                    <xsl:value-of select="$att"/>
1649
                  </xsl:with-param>
1650
                  <xsl:with-param name="attRef">
1651
                    <xsl:value-of select="$attRef"/>
1652
                  </xsl:with-param>
1653
                  <xsl:with-param name="class">
1654
                    <xsl:value-of select="$class"/>
1655
                  </xsl:with-param>
1656
                  <xsl:with-param name="orig">
1657
                    <xsl:value-of select="$wherefrom"/>
1658
                  </xsl:with-param>
1659
                </xsl:call-template>
1660
              </xsl:for-each>
1661
            </xsl:when>
1662
            <xsl:otherwise>
1663
              <xsl:call-template name="odd2odd-unChangedAtt">
1664
                <xsl:with-param name="debug">2</xsl:with-param>
1665
                <xsl:with-param name="lookingAt">
1666
                  <xsl:value-of select="$lookingAt"/>
1667
                </xsl:with-param>
1668
                <xsl:with-param name="att">
1669
                  <xsl:value-of select="$att"/>
1670
                </xsl:with-param>
1671
                <xsl:with-param name="attRef">
1672
                  <xsl:value-of select="$attRef"/>
1673
                </xsl:with-param>
1674
                <xsl:with-param name="class">
1675
                  <xsl:value-of select="$class"/>
1676
                </xsl:with-param>
1677
                <xsl:with-param name="orig">
1678
                  <xsl:value-of select="$wherefrom"/>
1679
                </xsl:with-param>
1680
              </xsl:call-template>
1681
            </xsl:otherwise>
1682
          </xsl:choose>
1683
        </xsl:otherwise>
1684
      </xsl:choose>
1685
    </xsl:for-each>
1686
  </xsl:template>
1687
  <xsl:template name="odd2odd-unChangedAtt">
1688
    <xsl:param name="lookingAt"/>
1689
    <xsl:param name="att"/>
1690
    <xsl:param name="class"/>
1691
    <xsl:param name="orig"/>
1692
    <xsl:param name="attRef"/>
1693
    <xsl:param name="debug"/>
1694
    <xsl:choose>
1695
      <!-- don't make another reference to a class attribute 
1696
	 if we already have an attRef -->
1697
      <xsl:when test="key('odd2odd-ATTREFS',$attRef)"/>
1698
      <xsl:otherwise>
1699
        <attRef xmlns="http://www.tei-c.org/ns/1.0" n="3-{$debug}" name="{$class}.attribute.{translate($att,':','')}"/>
1700
      </xsl:otherwise>
1701
    </xsl:choose>
1702
  </xsl:template>
1703
  <xsl:template name="odd2odd-checkClassAttribute">
1704
    <xsl:param name="element"/>
1705
    <!-- look at each attribute in turn, and then repeat for any
1706
	 inherited classes -->
1707
    <xsl:variable name="class" select="@ident"/>
1708
    <xsl:variable name="mode" select="@mode"/>
1709
    <xsl:for-each select="tei:attList//tei:attDef">
1710
      <xsl:variable name="att" select="@ident"/>
1711
      <xsl:for-each select="$ODD">
1712
        <xsl:choose>
1713
          <!-- deleted in the customization at the class level -->
1714
          <xsl:when test="key('odd2odd-DELETEATT',concat($class,'_',$att))">
1715
            <xsl:text>:class-del-</xsl:text>
1716
            <xsl:value-of select="$att"/>
1717
          </xsl:when>
1718
          <!-- deleted in the customization at the element level -->
1719
          <xsl:when test="key('odd2odd-DELETEATT',concat($element,'_',$att))">
1720
            <xsl:text>:element-del-</xsl:text>
1721
            <xsl:value-of select="$att"/>
1722
          </xsl:when>
1723
          <!-- replaced in the customization at the element level -->
1724
          <xsl:when test="key('odd2odd-REPLACEATT',concat($element,'_',$att))">
1725
            <xsl:text>:element-replace-</xsl:text>
1726
            <xsl:value-of select="$att"/>
1727
          </xsl:when>
1728
          <!-- changed in the customization by the element -->
1729
          <xsl:when test="key('odd2odd-CHANGEATT',concat($element,'_',$att))">
1730
            <xsl:text>:element-change-</xsl:text>
1731
            <xsl:value-of select="$att"/>
1732
          </xsl:when>
1733
        </xsl:choose>
1734
      </xsl:for-each>
1735
    </xsl:for-each>
1736
    <xsl:for-each select="tei:classes/tei:memberOf">
1737
      <xsl:for-each select="key('odd2odd-ATTCLASSES',@key)">
1738
        <xsl:call-template name="odd2odd-checkClassAttribute">
1739
          <xsl:with-param name="element" select="$element"/>
1740
        </xsl:call-template>
1741
      </xsl:for-each>
1742
    </xsl:for-each>
1743
  </xsl:template>
1744
  <xsl:template name="odd2odd-processAttributes">
1745
    <xsl:param name="ORIGINAL"/>
1746
    <xsl:param name="elementName"/>
1747
    <!-- first put in the ones we know take precedence -->
1748
    <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attDef[@mode='add' or not(@mode)]"/>
1749
    <xsl:apply-templates mode="odd2odd-justcopy" select="tei:attList/tei:attDef[@mode='replace']"/>
1750
    <xsl:for-each select="$ORIGINAL/tei:attList">
1751
      <!-- original source  context -->
1752
      <xsl:for-each select="tei:attList">
1753
        <attList xmlns="http://www.tei-c.org/ns/1.0">
1754
          <xsl:copy-of select="@org"/>
1755
          <xsl:for-each select="tei:attDef">
1756
            <xsl:variable name="ATT" select="."/>
1757
            <xsl:variable name="lookingAt">
1758
              <xsl:value-of select="concat(../../../@ident,'_',@ident)"/>
1759
            </xsl:variable>
1760
            <xsl:for-each select="$ODD">
1761
              <xsl:choose>
1762
                <xsl:when test="key('odd2odd-DELETEATT',$lookingAt)"/>
1763
                <xsl:when test="key('odd2odd-REPLACEATT',$lookingAt)"/>
1764
                <xsl:when test="key('odd2odd-CHANGEATT',$lookingAt)">
1765
                  <xsl:call-template name="odd2odd-mergeAttribute">
1766
                    <xsl:with-param name="New" select="key('odd2odd-CHANGEATT',$lookingAt)"/>
1767
                    <xsl:with-param name="Old" select="$ATT"/>
1768
                  </xsl:call-template>
1769
                </xsl:when>
1770
                <xsl:otherwise>
1771
                  <xsl:copy-of select="$ATT"/>
1772
                </xsl:otherwise>
1773
              </xsl:choose>
1774
            </xsl:for-each>
1775
          </xsl:for-each>
1776
        </attList>
1777
      </xsl:for-each>
1778
      <xsl:variable name="atts">
1779
        <xsl:for-each select="tei:attDef">
1780
          <xsl:variable name="ATT" select="."/>
1781
          <xsl:variable name="lookingAt">
1782
            <xsl:value-of select="concat(../../@ident,'_',@ident)"/>
1783
          </xsl:variable>
1784
          <xsl:for-each select="$ODD">
1785
            <xsl:choose>
1786
              <xsl:when test="key('odd2odd-DELETEATT',$lookingAt)"/>
1787
              <xsl:when test="key('odd2odd-REPLACEATT',$lookingAt)"/>
1788
              <xsl:when test="key('odd2odd-CHANGEATT',$lookingAt)">
1789
                <xsl:call-template name="odd2odd-mergeAttribute">
1790
                  <xsl:with-param name="New" select="key('odd2odd-CHANGEATT',$lookingAt)"/>
1791
                  <xsl:with-param name="Old" select="$ATT"/>
1792
                </xsl:call-template>
1793
              </xsl:when>
1794
              <xsl:otherwise>
1795
                <xsl:copy-of select="$ATT"/>
1796
              </xsl:otherwise>
1797
            </xsl:choose>
1798
          </xsl:for-each>
1799
        </xsl:for-each>
1800
      </xsl:variable>
1801
      <xsl:choose>
1802
        <xsl:when test="@org">
1803
          <attList xmlns="http://www.tei-c.org/ns/1.0">
1804
            <xsl:copy-of select="@org"/>
1805
            <xsl:copy-of select="$atts"/>
1806
          </attList>
1807
        </xsl:when>
1808
        <xsl:otherwise>
1809
          <xsl:copy-of select="$atts"/>
1810
        </xsl:otherwise>
1811
      </xsl:choose>
1812
    </xsl:for-each>
1813
    <!-- now we need to go back to the classes of which this 
1814
       element is a member and reference their untouched attributes -->
1815
    <xsl:choose>
1816
      <xsl:when test="$elementName=''"/>
1817
      <xsl:otherwise>
1818
	<xsl:if test="$autoGlobal='true'">
1819
	  <xsl:call-template name="odd2odd-classAttributes">
1820
	    <xsl:with-param name="whence">7</xsl:with-param>
1821
	    <xsl:with-param name="elementName" select="$elementName"/>
1822
	    <xsl:with-param name="className" select="'att.global'"/>
1823
	  </xsl:call-template>
1824
	</xsl:if>
1825
        <xsl:variable name="classMembership">
1826
          <x>
1827
            <xsl:choose>
1828
              <xsl:when test="tei:classes[@mode='change']">
1829
                <xsl:for-each select="tei:classes/tei:memberOf[not(@mode='delete')]">
1830
                  <xsl:copy-of select="."/>
1831
                </xsl:for-each>
1832
                <xsl:for-each select="$ORIGINAL">
1833
                  <xsl:for-each select="tei:classes/tei:memberOf">
1834
                    <xsl:copy-of select="."/>
1835
                  </xsl:for-each>
1836
                </xsl:for-each>
1837
              </xsl:when>
1838
              <xsl:when test="tei:classes">
1839
                <xsl:for-each select="tei:classes/tei:memberOf">
1840
                  <xsl:copy-of select="."/>
1841
                </xsl:for-each>
1842
              </xsl:when>
1843
              <xsl:otherwise>
1844
                <xsl:for-each select="$ORIGINAL">
1845
                  <xsl:for-each select="tei:classes/tei:memberOf">
1846
                    <xsl:copy-of select="."/>
1847
                  </xsl:for-each>
1848
                </xsl:for-each>
1849
              </xsl:otherwise>
1850
            </xsl:choose>
1851
          </x>
1852
        </xsl:variable>
1853
        <xsl:for-each select="$classMembership/x/tei:memberOf">
1854
          <xsl:if test="not(preceding-sibling::tei:memberOf[@key=current()/@key])">
1855
            <xsl:call-template name="odd2odd-classAttributes">
1856
              <xsl:with-param name="whence">8</xsl:with-param>
1857
              <xsl:with-param name="elementName" select="$elementName"/>
1858
              <xsl:with-param name="className" select="@key"/>
1859
            </xsl:call-template>
1860
          </xsl:if>
1861
        </xsl:for-each>
1862
      </xsl:otherwise>
1863
    </xsl:choose>
1864
  </xsl:template>
1865
  <xsl:template name="odd2odd-mergeAttribute">
1866
    <xsl:param name="New"/>
1867
    <xsl:param name="Old"/>
1868
    <attDef xmlns="http://www.tei-c.org/ns/1.0">
1869
      <xsl:attribute name="ident" select="$Old/@ident"/>
1870
       <xsl:attribute name="usage">
1871
          <xsl:choose>
1872
            <xsl:when test="$New/@usage">
1873
              <xsl:value-of select="$New/@usage"/>
1874
            </xsl:when>
1875
            <xsl:otherwise>
1876
              <xsl:value-of select="$Old/@usage"/>
1877
            </xsl:otherwise>
1878
          </xsl:choose>
1879
        </xsl:attribute>
1880
        <xsl:if test="$New/tei:altIdent">
1881
          <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:altIdent"/>
1882
        </xsl:if>
1883
        <!-- equiv, gloss, desc trio -->
1884
        <xsl:choose>
1885
          <xsl:when test="$stripped='true'"/>
1886
          <xsl:when test="$New/tei:equiv">
1887
            <xsl:apply-templates mode="odd2odd-copy" select="$New/tei:equiv"/>
1888
          </xsl:when>
1889
          <xsl:otherwise>
1890
            <xsl:apply-templates mode="odd2odd-copy" select="$Old/tei:equiv"/>
1891
          </xsl:otherwise>
1892
        </xsl:choose>
1893
        <xsl:choose>
1894
          <xsl:when test="$New/tei:gloss">
1895
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:gloss"/>
1896
          </xsl:when>
1897
          <xsl:otherwise>
1898
              <xsl:apply-templates mode="odd2odd-justcopy" select="$Old/tei:gloss"/>
1899
          </xsl:otherwise>
1900
        </xsl:choose>
1901
        <xsl:choose>
1902
          <xsl:when test="$stripped='true'"/>
1903
          <xsl:when test="$New/tei:desc">
1904
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:desc"/>
1905
          </xsl:when>
1906
          <xsl:otherwise>
1907
              <xsl:apply-templates mode="odd2odd-justcopy" select="$Old/tei:desc"/>
1908
          </xsl:otherwise>
1909
        </xsl:choose>
1910
        <xsl:choose>
1911
          <xsl:when test="$New/tei:constraintSpec">
1912
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:constraintSpec"/>
1913
          </xsl:when>
1914
          <xsl:when test="$Old/tei:constraintSpec">
1915
            <xsl:copy-of select="$Old/tei:constraintSpec"/>
1916
          </xsl:when>
1917
        </xsl:choose>
1918
        <xsl:choose>
1919
          <xsl:when test="$New/tei:datatype">
1920
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:datatype"/>
1921
          </xsl:when>
1922
          <xsl:when test="$Old/tei:datatype">
1923
            <xsl:copy-of select="$Old/tei:datatype"/>
1924
          </xsl:when>
1925
        </xsl:choose>
1926
        <xsl:choose>
1927
          <xsl:when test="$New/tei:defaultVal">
1928
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:defaultVal"/>
1929
          </xsl:when>
1930
          <xsl:when test="$Old/tei:defaultVal">
1931
            <xsl:copy-of select="$Old/tei:defaultVal"/>
1932
          </xsl:when>
1933
        </xsl:choose>
1934
        <xsl:choose>
1935
          <xsl:when test="$New/tei:valDesc">
1936
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:valDesc"/>
1937
          </xsl:when>
1938
          <xsl:when test="$Old/tei:valDesc">
1939
            <xsl:copy-of select="$Old/tei:valDesc"/>
1940
          </xsl:when>
1941
        </xsl:choose>
1942
        <xsl:choose>
1943
	  <xsl:when test="$New/tei:valList[@mode='delete']"/>
1944
          <xsl:when test="$New/tei:valList[@mode='add' or @mode='replace']">
1945
            <xsl:for-each select="$New/tei:valList[1]">
1946
              <xsl:copy>
1947
                <xsl:copy-of select="@type"/>
1948
                <xsl:copy-of select="@repeatable"/>
1949
                <xsl:copy-of select="*"/>
1950
              </xsl:copy>
1951
            </xsl:for-each>
1952
          </xsl:when>
1953
          <xsl:when test="$New/tei:valList[@mode='change']">
1954
            <xsl:for-each select="$New/tei:valList[1]">
1955
              <xsl:copy>
1956
                <xsl:copy-of select="@*"/>
1957
                <xsl:for-each select="$Old/tei:valList/tei:valItem">
1958
                  <xsl:variable name="thisme" select="@ident"/>
1959
                  <xsl:if test="not($New/tei:valList[1]/tei:valItem[@ident=$thisme and (@mode='delete' or @mode='replace')])">
1960
                    <xsl:copy>
1961
                      <xsl:copy-of select="@*"/>
1962
                      <xsl:for-each select="$New/tei:valList[1]/tei:valItem[@ident=$thisme]">
1963
                        <xsl:choose>
1964
                          <xsl:when test="tei:equiv">
1965
                            <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
1966
                          </xsl:when>
1967
                          <xsl:otherwise>
1968
                            <xsl:for-each select="$Old/tei:valList/tei:valItem[@ident=$thisme]">
1969
                              <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
1970
                            </xsl:for-each>
1971
                          </xsl:otherwise>
1972
                        </xsl:choose>
1973
                        <xsl:choose>
1974
                          <xsl:when test="tei:gloss">
1975
                            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
1976
                          </xsl:when>
1977
                          <xsl:otherwise>
1978
                            <xsl:for-each select="$Old/tei:valList/tei:valItem[@ident=$thisme]">
1979
                              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
1980
                            </xsl:for-each>
1981
                          </xsl:otherwise>
1982
                        </xsl:choose>
1983
                        <xsl:choose>
1984
                          <xsl:when test="$stripped='true'"/>
1985
                          <xsl:when test="tei:desc">
1986
                            <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1987
                          </xsl:when>
1988
                          <xsl:otherwise>
1989
                            <xsl:for-each select="$Old/tei:valList/tei:valItem[@ident=$thisme]">
1990
                              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
1991
                            </xsl:for-each>
1992
                          </xsl:otherwise>
1993
                        </xsl:choose>
1994
                      </xsl:for-each>
1995
                    </xsl:copy>
1996
                  </xsl:if>
1997
                </xsl:for-each>
1998
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:valItem[@mode='add']"/>
1999
                <xsl:apply-templates mode="odd2odd-justcopy" select="tei:valItem[@mode='replace']"/>
2000
              </xsl:copy>
2001
            </xsl:for-each>
2002
          </xsl:when>
2003
          <xsl:when test="$Old/tei:valList">
2004
            <xsl:copy-of select="$Old/tei:valList"/>
2005
          </xsl:when>
2006
        </xsl:choose>
2007
        <xsl:choose>
2008
          <xsl:when test="$stripped='true'"/>
2009
          <xsl:when test="$New/tei:exemplum">
2010
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:exemplum"/>
2011
          </xsl:when>
2012
          <xsl:otherwise>
2013
            <xsl:for-each select="$Old">
2014
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
2015
            </xsl:for-each>
2016
          </xsl:otherwise>
2017
        </xsl:choose>
2018
        <xsl:choose>
2019
          <xsl:when test="$stripped='true'"/>
2020
          <xsl:when test="$New/tei:remarks">
2021
            <xsl:apply-templates mode="odd2odd-justcopy" select="$New/tei:remarks"/>
2022
          </xsl:when>
2023
          <xsl:otherwise>
2024
            <xsl:for-each select="$Old">
2025
              <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
2026
            </xsl:for-each>
2027
          </xsl:otherwise>
2028
        </xsl:choose>
2029
    </attDef>
2030
  </xsl:template>
2031
  <xsl:template match="tei:specGrp">
2032
    <xsl:choose>
2033
      <xsl:when test="ancestor::tei:schemaSpec"> </xsl:when>
2034
      <xsl:otherwise>
2035
        <xsl:copy>
2036
          <xsl:apply-templates/>
2037
        </xsl:copy>
2038
      </xsl:otherwise>
2039
    </xsl:choose>
2040
  </xsl:template>
2041
  <xsl:template match="tei:specGrpRef"/>
2042
  <xsl:template match="tei:macroSpec|tei:classSpec">
2043
    <xsl:if test="not(ancestor::tei:schemaSpec)">
2044
      <xsl:copy-of select="."/>
2045
    </xsl:if>
2046
  </xsl:template>
2047
  <xsl:template match="tei:attDef[@mode]"/>
2048
  <xsl:template match="tei:elementSpec">
2049
    <xsl:if test="not(//tei:schemaSpec)">
2050
      <xsl:variable name="elementName">
2051
        <xsl:value-of select="@ident"/>
2052
      </xsl:variable>
2053
      <xsl:copy>
2054
        <xsl:apply-templates mode="odd2odd-copy" select="@*"/>
2055
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:altIdent"/>
2056
        <xsl:if test="$stripped='false'">
2057
          <xsl:apply-templates mode="odd2odd-copy" select="tei:equiv"/>
2058
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:gloss"/>
2059
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:desc"/>
2060
        </xsl:if>
2061
        <xsl:apply-templates mode="odd2odd-justcopy" select="tei:classes"/>
2062
        <xsl:apply-templates mode="odd2odd-copy" select="tei:content"/>
2063
        <xsl:apply-templates mode="odd2odd-copy" select="tei:constraintSpec"/>
2064
        <attList xmlns="http://www.tei-c.org/ns/1.0">
2065
	  <xsl:if test="$autoGlobal='true' and not(tei:classes/tei:memberOf[@key='att.global'])">
2066
	    <xsl:comment>1.</xsl:comment>
2067
	    <xsl:call-template name="odd2odd-classAttributesSimple">
2068
	      <xsl:with-param name="whence">9</xsl:with-param>
2069
	      <xsl:with-param name="elementName" select="$elementName"/>
2070
	      <xsl:with-param name="className" select="'att.global'"/>
2071
	    </xsl:call-template>
2072
	  </xsl:if>
2073
          <xsl:comment>2.</xsl:comment>
2074
          <xsl:for-each select="tei:classes/tei:memberOf">
2075
            <xsl:comment>3: <xsl:value-of select="@key"/>
2076
                  </xsl:comment>
2077
            <xsl:call-template name="odd2odd-classAttributesSimple">
2078
              <xsl:with-param name="whence">10</xsl:with-param>
2079
              <xsl:with-param name="elementName" select="$elementName"/>
2080
              <xsl:with-param name="className" select="@key"/>
2081
            </xsl:call-template>
2082
          </xsl:for-each>
2083
          <xsl:comment>4.</xsl:comment>
2084
          <xsl:apply-templates select="tei:attList"/>
2085
          <xsl:comment>5.</xsl:comment>
2086
        </attList>
2087
        <xsl:if test="$stripped='false'">
2088
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:exemplum"/>
2089
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:remarks"/>
2090
          <xsl:apply-templates mode="odd2odd-justcopy" select="tei:listRef"/>
2091
        </xsl:if>
2092
      </xsl:copy>
2093
    </xsl:if>
2094
  </xsl:template>
2095
  <xsl:template match="tei:moduleRef[@url]">
2096
    <p>Include external module <xsl:value-of select="@url"/>.</p>
2097
  </xsl:template>
2098
  <xsl:template match="tei:moduleRef[@key]">
2099
    <p>Internal module <xsl:value-of select="@key"/> was located and expanded.</p>
2100
  </xsl:template>
2101
  <xsl:template match="@*|processing-instruction()|text()">
2102
    <xsl:copy-of select="."/>
2103
  </xsl:template>
2104
  <xsl:template match="*">
2105
    <xsl:copy>
2106
      <xsl:apply-templates select="*|@*|processing-instruction()|text()"/>
2107
    </xsl:copy>
2108
  </xsl:template>
2109
  <xsl:template name="odd2odd-classAttributesSimple">
2110
    <xsl:param name="elementName"/>
2111
    <xsl:param name="className"/>
2112
    <xsl:param name="whence"/>
2113
    <xsl:comment>START on <xsl:value-of select="$className"/>
2114
      </xsl:comment>
2115
    <xsl:for-each select="key('odd2odd-ATTCLASSES',$className)">
2116
      <xsl:variable name="CURRENTCLASS" select="."/>
2117
      <xsl:for-each select="tei:attList/tei:attDef">
2118
        <xsl:call-template name="odd2odd-mergeClassAttribute">
2119
          <xsl:with-param name="source">9</xsl:with-param>
2120
          <xsl:with-param name="element" select="$elementName"/>
2121
          <xsl:with-param name="class" select="$className"/>
2122
        </xsl:call-template>
2123
      </xsl:for-each>
2124
      <xsl:if test="tei:classes/tei:memberOf">
2125
        <xsl:for-each select="tei:classes/tei:memberOf">
2126
          <xsl:variable name="cName" select="@key"/>
2127
          <xsl:call-template name="odd2odd-classAttributesSimple">
2128
            <xsl:with-param name="whence">11</xsl:with-param>
2129
            <xsl:with-param name="elementName" select="$elementName"/>
2130
            <xsl:with-param name="className" select="$cName"/>
2131
          </xsl:call-template>
2132
        </xsl:for-each>
2133
      </xsl:if>
2134
    </xsl:for-each>
2135
    <xsl:comment>FINISH <xsl:value-of select="$className"/>
2136
      </xsl:comment>
2137
  </xsl:template>
2138

    
2139
  <xsl:template name="odd2odd-createCopy">
2140
    <xsl:param name="Source"  tunnel="yes"/>
2141
    <xsl:if test="$verbose='true'">
2142
      <xsl:message>Create <xsl:value-of select="local-name()"/> named <xsl:value-of select="@ident"/>            </xsl:message>
2143
    </xsl:if>
2144
    <xsl:element xmlns="http://www.tei-c.org/ns/1.0" name="{local-name()}">
2145
      <xsl:choose>
2146
	<xsl:when test="@module"/>
2147
	<xsl:when test="ancestor::tei:schemaSpec/@module">
2148
	  <xsl:copy-of select="ancestor::tei:schemaSpec/@module"/>
2149
	</xsl:when>
2150
	<xsl:otherwise>
2151
	  <xsl:attribute name="module">
2152
	    <xsl:text>derived-module-</xsl:text>
2153
	    <xsl:value-of select="ancestor::tei:schemaSpec/@ident"/>
2154
	  </xsl:attribute>
2155
	</xsl:otherwise>
2156
      </xsl:choose>
2157
      <xsl:choose>
2158
	<xsl:when test="local-name()='classSpec'">
2159
	  <xsl:if test="@type='model' and not(@predeclare)">
2160
	    <xsl:attribute name="predeclare">true</xsl:attribute>
2161
	  </xsl:if>
2162
	  <xsl:apply-templates mode="odd2odd-copy" select="@*|*|processing-instruction()|text()"/>
2163
	</xsl:when>
2164
	<xsl:when test="local-name()='macroSpec'">
2165
	  <xsl:apply-templates mode="odd2odd-copy" select="@*|*|processing-instruction()|text()"/>
2166
	</xsl:when>
2167
	<xsl:otherwise>
2168
	  <xsl:call-template name="odd2odd-copyElementSpec">
2169
	    <xsl:with-param name="n" select="'2'"/>
2170
	  </xsl:call-template>
2171
	</xsl:otherwise>
2172
      </xsl:choose>
2173
    </xsl:element>
2174
  </xsl:template>
2175

    
2176
  <xsl:template name="odd2odd-getversion">
2177
    <xsl:choose>
2178
      <xsl:when test="key('odd2odd-SCHEMASPECS',$selectedSchema)">
2179
	<xsl:for-each
2180
	    select="key('odd2odd-SCHEMASPECS',$selectedSchema)">
2181
	  <xsl:variable name="source" select="tei:workOutSource(.)"/>
2182
	  <xsl:for-each select="document($source)/tei:TEI/tei:teiHeader/tei:fileDesc/tei:editionStmt/tei:edition">
2183
	    <xsl:value-of select="."/>
2184
	  </xsl:for-each>
2185
	</xsl:for-each>
2186
      </xsl:when>
2187
      <xsl:otherwise>
2188
	<xsl:for-each select="document($DEFAULTSOURCE)/tei:TEI/tei:teiHeader/tei:fileDesc/tei:editionStmt/tei:edition">
2189
	  <xsl:value-of select="."/>
2190
	</xsl:for-each>
2191
      </xsl:otherwise>
2192
    </xsl:choose>
2193
  </xsl:template>
2194

    
2195
  <xsl:template name="odd2odd-processConstraints">
2196
    <xsl:param name="ORIGINAL"/>
2197
    <xsl:param name="elementName"/>
2198
    <!-- first put in the ones we know take precedence -->
2199
    <xsl:apply-templates mode="odd2odd-justcopy" select="tei:constraintSpec[@mode='add' or not(@mode)]"/>
2200
    <xsl:apply-templates mode="odd2odd-justcopy" select="tei:constraintSpec[@mode='replace']"/>
2201
    <xsl:apply-templates mode="odd2odd-justcopy" select="tei:constraintSpec[@mode='change']"/>
2202
    <xsl:for-each select="$ORIGINAL">
2203
      <!-- original source  context -->
2204
      <xsl:for-each select="tei:constraintSpec">
2205
        <xsl:variable name="CONSTRAINT" select="."/>
2206
        <xsl:variable name="lookingAt">
2207
          <xsl:value-of select="concat(../@ident,'_',@ident)"/>
2208
        </xsl:variable>
2209
        <xsl:for-each select="$ODD">
2210
          <xsl:choose>
2211
            <xsl:when test="key('odd2odd-DELETECONSTRAINT',$lookingAt)"/>
2212
            <xsl:when test="key('odd2odd-REPLACECONSTRAINT',$lookingAt)"/>
2213
            <xsl:when test="key('odd2odd-CHANGECONSTRAINT',$lookingAt)"/>
2214
            <xsl:otherwise>
2215
              <xsl:copy-of select="$CONSTRAINT"/>
2216
            </xsl:otherwise>
2217
          </xsl:choose>
2218
        </xsl:for-each>
2219
      </xsl:for-each>
2220
    </xsl:for-each>
2221
  </xsl:template>
2222

    
2223

    
2224
   <xsl:template match="@*|text()|processing-instruction()" mode="odd2odd-justcopy">
2225
      <xsl:copy-of select="."/>
2226
   </xsl:template>
2227

    
2228
   <xsl:template match="*" mode="odd2odd-justcopy">
2229
      <xsl:copy>
2230
         <xsl:apply-templates
2231
	     select="*|@*|processing-instruction()|text()" mode="odd2odd-justcopy"/>
2232
      </xsl:copy>
2233
   </xsl:template>
2234

    
2235

    
2236
  <xsl:template match="processing-instruction()" mode="odd2odd-pass2">
2237
    <xsl:copy-of select="."/>
2238
  </xsl:template>
2239

    
2240

    
2241
  <xsl:template match="processing-instruction()" mode="odd2odd-pass3">
2242
    <xsl:copy-of select="."/>
2243
  </xsl:template>
2244

    
2245
  <xsl:template match="@*|text()" mode="odd2odd-pass3">
2246
    <xsl:copy-of select="."/>
2247
  </xsl:template>
2248

    
2249
  <xsl:template match="*" mode="odd2odd-pass3">
2250
    <xsl:choose>
2251
      <xsl:when test="self::rng:optional     and count(rng:zeroOrMore)=2    and count(*)=2">
2252
        <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2253
      </xsl:when>
2254
      <xsl:when test="count(*)=1">
2255
        <xsl:variable name="element" select="local-name()"/>
2256
        <xsl:choose>
2257
          <xsl:when test="*[local-name()=$element]">
2258
            <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2259
          </xsl:when>
2260
          <xsl:when test="$element='optional'         and rng:zeroOrMore">
2261
            <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2262
          </xsl:when>
2263
          <xsl:when test="$element='optional'         and rng:oneOrMore">
2264
            <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2265
          </xsl:when>
2266
          <xsl:otherwise>
2267
            <xsl:copy>
2268
              <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2269
            </xsl:copy>
2270
          </xsl:otherwise>
2271
        </xsl:choose>
2272
      </xsl:when>
2273
      <xsl:otherwise>
2274
        <xsl:copy>
2275
          <xsl:apply-templates select="*|@*|text()|processing-instruction()" mode="odd2odd-pass3"/>
2276
        </xsl:copy>
2277
      </xsl:otherwise>
2278
    </xsl:choose>
2279
  </xsl:template>
2280

    
2281
  <xsl:function name="tei:workOutSource" as="xs:string">
2282
    <xsl:param name="e"/>
2283
    <xsl:variable name="loc">
2284
      <xsl:choose>
2285
	<xsl:when test="$e/@source">
2286
	  <xsl:value-of select="$e/@source"/>
2287
	</xsl:when>
2288
	<xsl:when test="$e/ancestor::tei:schemaSpec/@source">
2289
	  <xsl:value-of select="$e/ancestor::tei:schemaSpec/@source"/>
2290
	</xsl:when>
2291
	<xsl:otherwise>
2292
	  <xsl:value-of select="$DEFAULTSOURCE"/>
2293
	</xsl:otherwise>
2294
      </xsl:choose>
2295
    </xsl:variable>
2296
    <xsl:variable name="source">
2297
      <xsl:choose>
2298
	<xsl:when test="starts-with($loc,'/')">
2299
	  <xsl:value-of select="$loc"/>
2300
	</xsl:when>
2301
	<xsl:when test="starts-with($loc,'file:')">
2302
	  <xsl:value-of select="$loc"/>
2303
	</xsl:when>
2304
	<xsl:when test="starts-with($loc,'http:')">
2305
	  <xsl:value-of select="$loc"/>
2306
	</xsl:when>
2307
	<xsl:when test="starts-with($loc,'https:')">
2308
	  <xsl:value-of select="$loc"/>
2309
	</xsl:when>
2310
	<xsl:when test="starts-with($loc,'tei:')">
2311
	  <xsl:value-of
2312
	      select="replace($loc,'tei:',$defaultTEIServer)"/>
2313
	  <xsl:text>/xml/tei/odd/p5subset.xml</xsl:text>
2314
	</xsl:when>
2315
	<xsl:otherwise>
2316
	  <xsl:value-of select="$currentDirectory"/>
2317
	  <xsl:value-of select="$loc"/>
2318
	</xsl:otherwise>
2319
      </xsl:choose>
2320
    </xsl:variable>
2321
    <xsl:choose>
2322
      <xsl:when test="not(doc-available($source))">
2323
	<xsl:call-template name="die">
2324
	  <xsl:with-param name="message">
2325
	    <xsl:text>Source </xsl:text>
2326
	   <xsl:value-of select='$source'/>
2327
	   <xsl:text> not readable</xsl:text>
2328
	  </xsl:with-param>
2329
	</xsl:call-template>
2330
      </xsl:when>
2331
      <xsl:otherwise>
2332
	<xsl:if test="$verbose='true'">
2333
	  <xsl:message>
2334
	    <xsl:text>Setting source document to </xsl:text>
2335
	    <xsl:value-of  select="$source"/>
2336
	  </xsl:message>
2337
	</xsl:if>
2338
	<xsl:value-of select="$source"/>
2339
      </xsl:otherwise>
2340
    </xsl:choose>
2341
  </xsl:function>
2342

    
2343
  <xsl:template name="die">
2344
    <xsl:param name="message"/>
2345
    <xsl:message terminate="yes">
2346
      <xsl:text>Error: odd2odd.xsl: </xsl:text> 
2347
      <xsl:value-of select="$message"/>
2348
    </xsl:message>
2349
  </xsl:template>
2350

    
2351
</xsl:stylesheet>