Statistics
| Revision:

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

History | View | Annotate | Download (74.1 kB)

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

    
23
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
24
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
25

    
26
2. http://www.opensource.org/licenses/BSD-2-Clause
27
		
28
All rights reserved.
29

    
30
Redistribution and use in source and binary forms, with or without
31
modification, are permitted provided that the following conditions are
32
met:
33

    
34
* Redistributions of source code must retain the above copyright
35
notice, this list of conditions and the following disclaimer.
36

    
37
* Redistributions in binary form must reproduce the above copyright
38
notice, this list of conditions and the following disclaimer in the
39
documentation and/or other materials provided with the distribution.
40

    
41
This software is provided by the copyright holders and contributors
42
"as is" and any express or implied warranties, including, but not
43
limited to, the implied warranties of merchantability and fitness for
44
a particular purpose are disclaimed. In no event shall the copyright
45
holder or contributors be liable for any direct, indirect, incidental,
46
special, exemplary, or consequential damages (including, but not
47
limited to, procurement of substitute goods or services; loss of use,
48
data, or profits; or business interruption) however caused and on any
49
theory of liability, whether in contract, strict liability, or tort
50
(including negligence or otherwise) arising in any way out of the use
51
of this software, even if advised of the possibility of such damage.
52
</p>
53
      <p>Author: See AUTHORS</p>
54
      <p>Id: $Id: teiodds.xsl 9861 2011-12-02 00:05:30Z rahtz $</p>
55
      <p>Copyright: 2011, TEI Consortium</p>
56
    </desc>
57
  </doc>
58

    
59
  <xsl:include href="RngToRnc.xsl"/>
60
  <xsl:param name="idPrefix"/>
61
  <xsl:param name="oddmode">tei</xsl:param>
62
  <xsl:param name="STDOUT">true</xsl:param>
63
  <xsl:param name="outputSuffix">.html</xsl:param>
64
  <xsl:param name="selectedSchema"/>
65
  <xsl:param name="outputDir"/>
66
  <xsl:param name="splitLevel">-1</xsl:param>
67
  <xsl:param name="localsource"/>
68
  <xsl:param name="lang"/>
69
  <xsl:param name="doclang"/>
70
  <xsl:param name="patternPrefix"/>
71
  <xsl:param name="TEIC">false</xsl:param>
72
  <xsl:param name="autoGlobal">false</xsl:param>
73
  <xsl:param name="lookupDatabase">false</xsl:param>
74
  <xsl:param name="verbose">false</xsl:param>
75
  <xsl:param name="schemaBaseURL">http://localhost/schema/relaxng/</xsl:param>
76
  <xsl:key match="tei:*" name="LOCALIDENTS" use="@ident"/>
77
  <xsl:key match="tei:macroSpec" name="MACROS" use="@ident"/>
78
  <xsl:key match="tei:elementSpec" name="ELEMENTS" use="@ident"/>
79
  <xsl:key match="tei:elementSpec" name="ELEMENTS" use="tei:altIdent"/>
80
  <xsl:key match="tei:classSpec" name="CLASSES" use="@ident"/>
81
  <xsl:key match="rng:ref" name="REFS"  use="@name"/>
82
  <xsl:key match="rng:ref[contains(@name,'_')]" name="REFS" use="substring-before(@name,'_')"/>
83

    
84
  <xsl:key match="tei:elementSpec/tei:attList/tei:attDef/tei:datatype/rng:ref"
85
    name="ATTREFS-ELEMENT" use="@name"/>
86
  <xsl:key match="tei:classSpec/tei:attList/tei:attDef/tei:datatype/rng:ref" name="ATTREFS-CLASS"
87
    use="@name"/>
88
  <xsl:key match="tei:elementSpec/tei:attList/tei:attList/tei:attDef/tei:datatype/rng:ref"
89
    name="ATTREFS-ELEMENT" use="@name"/>
90
  <xsl:key match="tei:classSpec/tei:attList/tei:attList/tei:attDef/tei:datatype/rng:ref"
91
    name="ATTREFS-CLASS" use="@name"/>
92
  <xsl:key match="tei:macroSpec/tei:content//rng:ref" name="MACROREFS"  use="@name"/>
93

    
94
  <xsl:key match="tei:elementSpec|tei:classSpec" name="CLASSMEMBERS"
95
    use="tei:classes/tei:memberOf/@key"/>
96
  <xsl:key match="tei:elementSpec|tei:classSpec|tei:macroSpec" name="IDENTS" use="@ident"/>
97

    
98
  <xsl:key match="tei:macroSpec[@type='dt']" name="DATATYPES" use="1"/>
99
  <xsl:key match="tei:macroSpec" name="MACRODOCS" use="1"/>
100
  <xsl:key match="tei:attDef" name="ATTDOCS" use="1"/>
101
  <xsl:key match="tei:attDef" name="ATTRIBUTES" use="@ident"/>
102
  <xsl:key match="tei:classSpec//tei:attDef" name="ATTRIBUTES-CLASS" use="@ident"/>
103
  <xsl:key match="tei:elementSpec//tei:attDef" name="ATTRIBUTES-ELEMENT" use="@ident"/>
104
  <xsl:key match="tei:schemaSpec" name="SCHEMASPECS" use="1"/>
105
  <xsl:key match="tei:schemaSpec" name="LISTSCHEMASPECS" use="@ident"/>
106
  <xsl:key match="tei:classSpec[@type='atts']" name="ATTCLASSDOCS" use="1"/>
107
  <xsl:key match="tei:classSpec[@type='model']" name="MODELCLASSDOCS" use="1"/>
108
  <xsl:key match="tei:elementSpec" name="ELEMENTDOCS" use="1"/>
109
  <xsl:key match="tei:*" name="NameToID" use="@ident"/>
110
  <xsl:key match="tei:elementSpec" name="ElementModule" use="@module"/>
111
  <xsl:key match="tei:classSpec" name="ClassModule" use="@module"/>
112
  <xsl:key match="tei:macroSpec" name="MacroModule" use="@module"/>
113
  <xsl:key match="tei:moduleSpec" name="Modules" use="1"/>
114
  <xsl:key match="tei:moduleSpec" name="MODULES" use="@ident"/>
115
  <xsl:key match="tei:classSpec[@predeclare='true']" name="predeclaredClasses" use="1"/>
116
  <xsl:key match="tei:macroSpec[@predeclare='true']" name="PredeclareMacros" use="@ident"/>
117
  <xsl:key match="tei:macroSpec[@predeclare='true']" name="PredeclareMacrosModule" use="@module"/>
118
  <xsl:key match="tei:macroSpec[@predeclare='true']" name="PredeclareAllMacros" use="1"/>
119
  <xsl:variable name="parameterize">
120
    <xsl:choose>
121
      <xsl:when test="key('SCHEMASPECS',1)">false</xsl:when>
122
      <xsl:otherwise>true</xsl:otherwise>
123
    </xsl:choose>
124
  </xsl:variable>
125
  <xsl:variable name="whichSchemaSpec">
126
    <xsl:choose>
127
      <xsl:when test="$selectedSchema">
128
        <xsl:value-of select="$selectedSchema"/>
129
      </xsl:when>
130
      <xsl:otherwise>
131
        <xsl:value-of select="key('SCHEMASPECS',1)[1]/@ident"/>
132
      </xsl:otherwise>
133
    </xsl:choose>
134
  </xsl:variable>
135

    
136
  <!-- lookup table of element contents, and templates to access the result -->
137
  <xsl:key match="Contains" name="ELEMENTPARENTS" use="."/>
138

    
139
  <xsl:variable name="generalPrefix">
140
    <xsl:choose>
141
      <xsl:when test="string-length($patternPrefix)&gt;0">
142
        <xsl:value-of select="$patternPrefix"/>
143
      </xsl:when>
144
      <xsl:when test="key('LISTSCHEMASPECS',$whichSchemaSpec)[@prefix]">
145
        <xsl:value-of select="key('LISTSCHEMASPECS',$whichSchemaSpec)/@prefix"/>
146
      </xsl:when>
147
    </xsl:choose>
148
  </xsl:variable>
149

    
150
  <xsl:variable name="targetLanguage">
151
    <xsl:choose>
152
      <xsl:when test="string-length($lang)&gt;0">
153
        <xsl:value-of select="$lang"/>
154
      </xsl:when>
155
      <xsl:when test="key('LISTSCHEMASPECS',$whichSchemaSpec)[@targetLang]">
156
        <xsl:value-of select="key('LISTSCHEMASPECS',$whichSchemaSpec)/@targetLang"/>
157
      </xsl:when>
158
      <xsl:otherwise>
159
        <xsl:text>en</xsl:text>
160
      </xsl:otherwise>
161
    </xsl:choose>
162
  </xsl:variable>
163

    
164

    
165
  <xsl:template name="generateDoc">
166
    <xsl:choose>
167
      <xsl:when test="string-length($doclang)&gt;0">
168
        <xsl:value-of select="$doclang"/>
169
      </xsl:when>
170
      <xsl:when test="key('LISTSCHEMASPECS',$whichSchemaSpec)/@docLang">
171
        <xsl:value-of select="key('LISTSCHEMASPECS',$whichSchemaSpec)/@docLang"/>
172
      </xsl:when>
173
      <xsl:otherwise>
174
        <xsl:text>en</xsl:text>
175
      </xsl:otherwise>
176
    </xsl:choose>
177
  </xsl:template>
178

    
179

    
180
  <xsl:template match="processing-instruction()">
181
    <xsl:choose>
182
      <xsl:when test="name(.) = 'odds'">
183
	<xsl:choose>
184
	  <xsl:when test=".='date'"> This formatted version of the Guidelines was created on
185
	  <xsl:call-template name="showDate"/>. </xsl:when>
186
	</xsl:choose>
187
      </xsl:when>
188
      <xsl:otherwise>
189
	<xsl:copy-of select="."/>
190
      </xsl:otherwise>
191
    </xsl:choose>
192
  </xsl:template>
193

    
194
  <xsl:template match="*" mode="literal">
195
    <xsl:text>&#10;</xsl:text>
196
    <xsl:for-each select="ancestor::rng:*">
197
      <xsl:text> </xsl:text>
198
    </xsl:for-each>
199
    <xsl:text>&lt;</xsl:text>
200
    <xsl:value-of select="local-name(.)"/>
201
    <xsl:for-each select="@*">
202
      <xsl:text> </xsl:text>
203
      <xsl:value-of select="local-name(.)"/>="<xsl:value-of select="."/>"</xsl:for-each>
204
    <xsl:choose>
205
      <xsl:when test="child::node()">
206
        <xsl:text>&gt;</xsl:text>
207
        <xsl:apply-templates mode="literal"/>
208
        <xsl:if test="node()[last()]/self::rng:*">
209
          <xsl:text>&#10;</xsl:text>
210
        </xsl:if>
211
        <xsl:for-each select="ancestor::rng:*">
212
          <xsl:text> </xsl:text>
213
        </xsl:for-each>
214
        <xsl:text>&lt;/</xsl:text>
215
        <xsl:value-of select="local-name(.)"/>
216
        <xsl:text>&gt;</xsl:text>
217
      </xsl:when>
218
      <xsl:otherwise>
219
        <xsl:text>/&gt;</xsl:text>
220
        <xsl:if test="node()[last()]/self::rng:*">
221
          <xsl:text>&#10;	  </xsl:text>
222
        </xsl:if>
223
      </xsl:otherwise>
224
    </xsl:choose>
225
  </xsl:template>
226

    
227
  <xsl:template match="rng:ref">
228
    <xsl:call-template name="makeRef">
229
      <xsl:with-param name="lookup" select="replace(@name,'_(alternation|sequenceOptionalRepeatable|sequenceOptional|sequenceRepeatable|sequence)','')"/>
230
    </xsl:call-template>
231
  </xsl:template>
232

    
233
    <xsl:template name="makeRef">
234
      <xsl:param name="lookup"/>
235
      <xsl:variable name="fullname" select="@name"/>
236
      <xsl:variable name="myprefix"
237
		    select="ancestor::*[@prefix][1]/@prefix"/>
238
      <xsl:choose>
239
	<xsl:when test="ancestor::tei:content[@autoPrefix='false']">
240
	  <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$fullname}"/>
241
	</xsl:when>
242
	<xsl:when test="count(key('IDENTS',$lookup))&gt;1">
243
	  <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$myprefix}{$fullname}"/>
244
	</xsl:when>
245
	<xsl:when test="key('IDENTS',$lookup)">
246
	  <xsl:for-each select="key('IDENTS',$lookup)">
247
	    <xsl:variable name="localprefix">
248
	      <xsl:choose>
249
		<xsl:when test="@prefix">
250
		<xsl:value-of select="@prefix"/>
251
	      </xsl:when>
252
	      <xsl:otherwise>
253
		<xsl:value-of select="$generalPrefix"/>
254
	      </xsl:otherwise>
255
	      </xsl:choose>
256
	    </xsl:variable>
257
	    <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$localprefix}{$fullname}"/>
258
	  </xsl:for-each>
259
	</xsl:when>
260
	<xsl:otherwise>
261
	  <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{@name}"/>
262
      </xsl:otherwise>
263
    </xsl:choose>
264
  </xsl:template>
265

    
266

    
267
  <xsl:template match="rng:*">
268
    <xsl:element name="{local-name()}" xmlns="http://relaxng.org/ns/structure/1.0" >
269
      <xsl:copy-of select="@*"/>
270
      <xsl:apply-templates select="rng:*|tei:*|text()|comment()"/>
271
    </xsl:element>
272
  </xsl:template>
273

    
274

    
275
  <xsl:template match="rng:zeroOrMore">
276
    <xsl:choose>
277
      <xsl:when
278
        test="rng:ref/@name='model.global'   and preceding-sibling::rng:*[1][self::rng:zeroOrMore/rng:ref/@name='model.global']"/>
279
      <xsl:when test="count(rng:*)=1 and rng:zeroOrMore">
280
        <xsl:apply-templates select="rng:*|tei:*|text()|comment()"/>
281
      </xsl:when>
282
      <xsl:otherwise>
283
	<zeroOrMore xmlns="http://relaxng.org/ns/structure/1.0" >
284
          <xsl:copy-of select="@*"/>
285
          <xsl:apply-templates select="rng:*|tei:*|text()|comment()"/>
286
	</zeroOrMore>
287
      </xsl:otherwise>
288
    </xsl:choose>
289
  </xsl:template>
290

    
291

    
292
  <xsl:template match="rng:choice">
293
    <xsl:choose>
294
      <xsl:when test="count(rng:*)=1">
295
        <xsl:apply-templates select="a:*|rng:*|tei:*|text()|comment()"/>
296
      </xsl:when>
297
      <xsl:otherwise>
298
	<choice xmlns="http://relaxng.org/ns/structure/1.0">
299
          <xsl:copy-of select="@*"/>
300
          <xsl:apply-templates select="a:*|rng:*|tei:*|text()|comment()"/>
301
	</choice>
302
      </xsl:otherwise>
303
    </xsl:choose>
304
  </xsl:template>
305

    
306

    
307
  <xsl:template match="rng:group">
308
    <!-- check if this group is identical to the last -->
309
    <xsl:choose>
310
      <xsl:when
311
        test="count(rng:*)=1 and local-name(preceding-sibling::rng:*[1])='group' and rng:zeroOrMore">
312
        <xsl:variable name="that">
313
          <xsl:for-each select="preceding-sibling::rng:*[1]">
314
            <xsl:apply-templates mode="decomposed"/>
315
          </xsl:for-each>
316
        </xsl:variable>
317
        <xsl:variable name="this">
318
          <xsl:apply-templates mode="decomposed"/>
319
        </xsl:variable>
320
        <xsl:choose>
321
          <xsl:when test="$that=$this"/>
322
	  <xsl:otherwise>
323
	    <group xmlns="http://relaxng.org/ns/structure/1.0">
324
              <xsl:copy-of select="@*"/>
325
              <xsl:apply-templates select="rng:*|tei:*|text()|comment()"/>
326
	    </group>
327
          </xsl:otherwise>
328
        </xsl:choose>
329
      </xsl:when>
330
      <xsl:otherwise>
331
	<xsl:element name="{local-name()}" xmlns="http://relaxng.org/ns/structure/1.0" >
332
          <xsl:copy-of select="@*"/>
333
          <xsl:apply-templates select="rng:*|tei:*|text()|comment()"/>
334
	</xsl:element>
335
      </xsl:otherwise>
336
    </xsl:choose>
337
  </xsl:template>
338

    
339

    
340
  <xsl:template match="rng:*" mode="decomposed">
341
    <xsl:value-of select="local-name(.)"/>
342
    <xsl:for-each select="@*">
343
      <xsl:text>@</xsl:text>
344
      <xsl:value-of select="."/>
345
    </xsl:for-each>
346
    <xsl:apply-templates mode="decomposed"/>
347
  </xsl:template>
348

    
349

    
350
  <xsl:template match="tei:*" mode="tangle"/>
351

    
352
  <xsl:template match="tei:attRef" mode="tangle">
353
    <xsl:call-template name="makeRef">
354
      <xsl:with-param name="lookup" select="substring-before(@name,'.attribute')"/>
355
    </xsl:call-template>
356
  </xsl:template>
357

    
358
  <xsl:template match="tei:attDef" mode="tangle">
359
    <xsl:param name="element"/>
360
    <xsl:variable name="I">
361
      <xsl:value-of select="translate(@ident,':','')"/>
362
    </xsl:variable>
363
    <xsl:if test="not(starts-with(@ident,'xmlns'))">
364
      <xsl:choose>
365
        <xsl:when test="ancestor::tei:elementSpec">
366
          <xsl:call-template name="makeAnAttribute"/>
367
        </xsl:when>
368
        <xsl:when test="ancestor::tei:classSpec">
369
          <define xmlns="http://relaxng.org/ns/structure/1.0"
370
            name="{$element}.attribute.{translate(@ident,':','')}">
371
            <xsl:call-template name="makeAnAttribute"/>
372
          </define>
373
        </xsl:when>
374
      </xsl:choose>
375
    </xsl:if>
376
    <xsl:apply-templates select="tei:constraintSpec"/>
377

    
378
  </xsl:template>
379

    
380
  <xsl:template match="tei:attList" mode="tangle">
381
    <xsl:param name="element"/>
382
    <xsl:choose>
383
      <xsl:when test="count(*)=0"/>
384
      <xsl:when test="@org='group' and         parent::tei:attList[@org='choice']">
385
        <group xmlns="http://relaxng.org/ns/structure/1.0">
386
          <xsl:apply-templates mode="tangle" select="tei:*">
387
            <xsl:with-param name="element" select="$element"/>
388
          </xsl:apply-templates>
389
        </group>
390
      </xsl:when>
391

    
392
      <xsl:when test="@org='choice'">
393
        <choice xmlns="http://relaxng.org/ns/structure/1.0">
394
          <xsl:apply-templates mode="tangle" select="tei:*">
395
            <xsl:with-param name="element" select="$element"/>
396
          </xsl:apply-templates>
397
        </choice>
398
      </xsl:when>
399

    
400
      <xsl:otherwise>
401
        <xsl:apply-templates mode="tangle" select="tei:*">
402
          <xsl:with-param name="element" select="$element"/>
403
        </xsl:apply-templates>
404
      </xsl:otherwise>
405
    </xsl:choose>
406
  </xsl:template>
407

    
408

    
409
  <xsl:template match="tei:classSpec" mode="tangle">
410
    <xsl:variable name="c">
411
      <xsl:choose>
412
	<xsl:when test="@prefix">
413
	  <xsl:value-of select="@prefix"/>
414
	</xsl:when>
415
	<xsl:otherwise>
416
	  <xsl:value-of select="$generalPrefix"/>
417
	</xsl:otherwise>
418
      </xsl:choose>
419
      <xsl:value-of select="@ident"/>
420
    </xsl:variable>
421

    
422
    <xsl:if test="$verbose='true'">
423
      <xsl:message> classSpec <xsl:value-of select="@ident"/> (type <xsl:value-of select="@type"
424
        />)</xsl:message>
425
    </xsl:if>
426
    <xsl:choose>
427
      <xsl:when test="@type='model'">
428
        <xsl:apply-templates mode="processModel" select=".">
429
          <xsl:with-param name="declare">false</xsl:with-param>
430
          <!--	    <xsl:choose>
431
	      <xsl:when test="@module='tei'">true</xsl:when>
432
	      <xsl:otherwise>false</xsl:otherwise>
433
	    </xsl:choose>
434
	  </xsl:with-param>
435
-->
436
        </xsl:apply-templates>
437
      </xsl:when>
438
      <xsl:when test="@type='atts'">
439
        <xsl:call-template name="bitOut">
440
          <xsl:with-param name="grammar">true</xsl:with-param>
441
          <xsl:with-param name="content">
442
            <Wrapper>
443
              <xsl:variable name="contents">
444
                <ROOT>
445
                  <xsl:for-each select="tei:classes/tei:memberOf">
446
                    <xsl:for-each select="key('IDENTS',@key)[1]">
447
                      <xsl:if test="@type='atts'">
448
                        <ref  xmlns="http://relaxng.org/ns/structure/1.0">
449
			  <xsl:attribute name="name">
450
			    <xsl:choose>
451
			      <xsl:when test="@prefix">
452
				<xsl:value-of select="@prefix"/>
453
			      </xsl:when>
454
			      <xsl:otherwise>
455
				<xsl:value-of select="$generalPrefix"/>
456
			      </xsl:otherwise>
457
			    </xsl:choose>
458
			    <xsl:value-of select="@ident"/>
459
			    <xsl:text>.attributes</xsl:text>
460
			  </xsl:attribute>
461
			</ref>
462
                      </xsl:if>
463
                    </xsl:for-each>
464
                  </xsl:for-each>
465
                  <xsl:for-each select="tei:attList//tei:attDef">
466
                    <xsl:if test="not(starts-with(@ident,'xmlns'))">
467
                      <ref xmlns="http://relaxng.org/ns/structure/1.0"
468
                        name="{$c}.attribute.{translate(@ident,':','')}"/>
469
                    </xsl:if>
470
                  </xsl:for-each>
471
                  <xsl:for-each select="tei:attList//tei:attRef">
472
		    <xsl:call-template name="makeRef">
473
		      <xsl:with-param name="lookup" select="substring-before(@name,'.attribute')"/>
474
		    </xsl:call-template>
475
                  </xsl:for-each>
476
                </ROOT>
477
              </xsl:variable>
478
              <define xmlns="http://relaxng.org/ns/structure/1.0"
479
		      name="{$c}.attributes">
480
		<xsl:for-each select="$contents/ROOT">
481
		  <xsl:apply-templates mode="justcopy"/>
482
		  <xsl:if test="not($contents/ROOT/*)">
483
		    <empty/>
484
		  </xsl:if>
485
		</xsl:for-each>
486
              </define>
487
              <xsl:apply-templates mode="tangle" select="tei:attList//tei:attDef">
488
                <xsl:with-param name="element" select="$c"/>
489
              </xsl:apply-templates>
490
            </Wrapper>
491
          </xsl:with-param>
492
        </xsl:call-template>
493
      </xsl:when>
494
    </xsl:choose>
495
  </xsl:template>
496

    
497
  <xsl:template match="tei:classSpec" mode="processModel">
498
    <xsl:param name="declare">false</xsl:param>
499
    <xsl:if test="$verbose='true'">
500
      <xsl:message> .... model class <xsl:value-of select="@ident"/>
501
      </xsl:message>
502
    </xsl:if>
503
    <xsl:call-template name="bitOut">
504
      <xsl:with-param name="grammar">true</xsl:with-param>
505
      <xsl:with-param name="content">
506
        <Wrapper>
507
          <xsl:call-template name="processClassDefinition">
508
            <xsl:with-param name="type">
509
              <xsl:choose>
510
                <xsl:when test="@generate">
511
                  <xsl:value-of select="@generate"/>
512
                </xsl:when>
513
                <xsl:otherwise>
514
                  <xsl:text>&#10;			     NULL
515
			     alternation
516
			     sequence
517
			     sequenceOptional
518
			     sequenceOptionalRepeatable
519
			   sequenceRepeatable</xsl:text>
520
                </xsl:otherwise>
521
              </xsl:choose>
522
            </xsl:with-param>
523
            <xsl:with-param name="declare" select="$declare"/>
524
          </xsl:call-template>
525
        </Wrapper>
526
      </xsl:with-param>
527
    </xsl:call-template>
528
  </xsl:template>
529

    
530

    
531
  <xsl:template name="processClassDefinition">
532
    <xsl:param name="type"/>
533
    <xsl:param name="declare"/>
534
    <xsl:variable name="Type">
535
      <xsl:value-of select="normalize-space($type)"/>
536
    </xsl:variable>
537
    <xsl:choose>
538
      <xsl:when test="string-length($Type)=0">
539
        <xsl:call-template name="makeClassDefinition">
540
          <xsl:with-param name="type">NULL</xsl:with-param>
541
          <xsl:with-param name="declare" select="$declare"/>
542
        </xsl:call-template>
543
      </xsl:when>
544

    
545
      <xsl:when test="contains($Type,' ')">
546
        <xsl:call-template name="makeClassDefinition">
547
          <xsl:with-param name="type" select="substring-before($Type,' ')"/>
548
          <xsl:with-param name="declare" select="$declare"/>
549
        </xsl:call-template>
550
        <xsl:call-template name="processClassDefinition">
551
          <xsl:with-param name="type" select="substring-after($Type,' ')"/>
552
          <xsl:with-param name="declare" select="$declare"/>
553
        </xsl:call-template>
554
      </xsl:when>
555

    
556
      <xsl:otherwise>
557
        <xsl:call-template name="makeClassDefinition">
558
          <xsl:with-param name="type" select="$Type"/>
559
          <xsl:with-param name="declare" select="$declare"/>
560
        </xsl:call-template>
561
      </xsl:otherwise>
562
    </xsl:choose>
563
  </xsl:template>
564

    
565

    
566
  <xsl:template name="makeClassDefinition">
567
    <xsl:param name="type"/>
568
    <xsl:param name="declare"/>
569
    <!--
570
      alternation
571
      sequence
572
      sequenceOptional
573
      sequenceOptionalRepeatable
574
      sequenceRepeatable
575
  -->
576

    
577
    <xsl:variable name="thisClass">
578
      <xsl:value-of select="@ident"/>
579
    </xsl:variable>
580
    <xsl:variable name="localprefix">
581
      <xsl:choose>
582
	<xsl:when test="@prefix">
583
	  <xsl:value-of select="@prefix"/>
584
	</xsl:when>
585
	<xsl:otherwise>
586
	  <xsl:value-of select="$generalPrefix"/>
587
	</xsl:otherwise>
588
      </xsl:choose>
589
    </xsl:variable>
590

    
591
    <xsl:variable name="suffix">
592
      <xsl:choose>
593
        <xsl:when test="$type='NULL'"> </xsl:when>
594
        <xsl:otherwise>
595
          <xsl:text>_</xsl:text>
596
          <xsl:value-of select="$type"/>
597
        </xsl:otherwise>
598
      </xsl:choose>
599
    </xsl:variable>
600

    
601
    <xsl:choose>
602
      <xsl:when test="$declare='true'">
603
        <xsl:apply-templates mode="tangleModel" select="tei:classes/tei:memberOf"/>
604
        <define xmlns="http://relaxng.org/ns/structure/1.0"
605
          name="{$localprefix}{$thisClass}{$suffix}">
606
          <xsl:if test="@predeclare='true'">
607
            <xsl:attribute name="combine">choice</xsl:attribute>
608
          </xsl:if>
609
          <notAllowed/>
610
        </define>
611
      </xsl:when>
612
      <xsl:otherwise>
613
        <xsl:variable name="makeDecls">
614
          <xsl:call-template name="findUses">
615
            <xsl:with-param name="pattern" select="$type"/>
616
            <xsl:with-param name="class" select="$thisClass"/>
617
          </xsl:call-template>
618
        </xsl:variable>
619

    
620
        <!--<xsl:message>
621

    
622
DEBUG <xsl:value-of select="$thisClass"/><xsl:value-of
623
select="$suffix"/> generated <xsl:value-of
624
select="$makeDecls"/></xsl:message>
625
-->
626
        <xsl:choose>
627
          <!--
628
               <xsl:when test="$makeDecls=''">
629
                  <xsl:if test="$verbose='true'">
630
                     <xsl:message>Do NOT generate <xsl:value-of select="$thisClass"/>
631
                        <xsl:value-of select="$suffix"/> (<xsl:value-of select="$type"/>)                     </xsl:message>
632
                  </xsl:if>
633
               </xsl:when>
634
-->
635
          <xsl:when test="count(key('CLASSMEMBERS',$thisClass))&gt;0">
636
            <xsl:if test="$verbose='true'">
637
              <xsl:message> .... ... generate model <xsl:value-of select="$thisClass"/>
638
                <xsl:value-of select="$suffix"/> (<xsl:value-of select="$type"/>) </xsl:message>
639
            </xsl:if>
640
            <define xmlns="http://relaxng.org/ns/structure/1.0"
641
              name="{$localprefix}{$thisClass}{$suffix}">
642
              <xsl:choose>
643
                <xsl:when test="$type='sequence'">
644
                  <xsl:for-each select="key('CLASSMEMBERS',$thisClass)">
645
                    <xsl:apply-templates select="."
646
					 mode="classmember">
647
                      <xsl:with-param name="theClass" select="$thisClass"/>
648
                      <xsl:with-param name="suffix" select="$type"/>
649
                    </xsl:apply-templates>
650
                  </xsl:for-each>
651
                </xsl:when>
652
                <xsl:when test="$type='sequenceOptional'">
653
                  <xsl:for-each select="key('CLASSMEMBERS',$thisClass)">
654
                    <optional>
655
                      <xsl:apply-templates select="."  mode="classmember">
656
			<xsl:with-param name="theClass" select="$thisClass"/>
657
                        <xsl:with-param name="suffix" select="$type"/>
658
                      </xsl:apply-templates>
659
                    </optional>
660
                  </xsl:for-each>
661
                </xsl:when>
662

    
663
                <xsl:when test="$type='sequenceRepeatable'">
664
                  <xsl:for-each select="key('CLASSMEMBERS',$thisClass)">
665
                    <oneOrMore>
666
                      <xsl:apply-templates select="."  mode="classmember">
667
			<xsl:with-param name="theClass" select="$thisClass"/>		   
668
                        <xsl:with-param name="suffix" select="$type"/>
669
                      </xsl:apply-templates>
670
                    </oneOrMore>
671
                  </xsl:for-each>
672
                </xsl:when>
673

    
674
                <xsl:when test="$type='sequenceOptionalRepeatable'">
675
                  <xsl:for-each select="key('CLASSMEMBERS',$thisClass)">
676
                    <zeroOrMore>
677
                      <xsl:apply-templates select="." mode="classmember">
678
                        <xsl:with-param name="suffix" select="$type"/>
679
			<xsl:with-param name="theClass" select="$thisClass"/>
680
                      </xsl:apply-templates>
681
                    </zeroOrMore>
682
                  </xsl:for-each>
683
                </xsl:when>
684

    
685
                <xsl:otherwise>
686
                  <choice>
687
                    <xsl:for-each select="key('CLASSMEMBERS',$thisClass)">
688
                      <xsl:apply-templates select="." mode="classmember">
689
                        <xsl:with-param name="suffix" select="$type"/>
690
			<xsl:with-param name="theClass" select="$thisClass"/>
691
                      </xsl:apply-templates>
692
                    </xsl:for-each>
693
                  </choice>
694
                </xsl:otherwise>
695
              </xsl:choose>
696
            </define>
697
          </xsl:when>
698
          <xsl:otherwise>
699
            <define xmlns="http://relaxng.org/ns/structure/1.0"
700
              name="{$localprefix}{$thisClass}{$suffix}">
701
              <xsl:choose>
702
                <xsl:when
703
                  test="$type='sequence' or     $type='sequenceOptional' or      $type='sequenceOptionalRepeatable'">
704
                  <empty/>
705
                </xsl:when>
706
                <xsl:otherwise>
707
                  <notAllowed/>
708
                </xsl:otherwise>
709
              </xsl:choose>
710
            </define>
711
          </xsl:otherwise>
712
        </xsl:choose>
713
      </xsl:otherwise>
714
    </xsl:choose>
715
  </xsl:template>
716

    
717

    
718

    
719
  <xsl:template name="findUses">
720
    <xsl:param name="pattern"/>
721
    <xsl:param name="class"/>
722
    <xsl:variable name="suffix">
723
      <xsl:choose>
724
        <xsl:when test="$pattern='NULL'"/>
725
        <xsl:otherwise>
726
          <xsl:text>_</xsl:text>
727
          <xsl:value-of select="$pattern"/>
728
        </xsl:otherwise>
729
      </xsl:choose>
730
    </xsl:variable>
731

    
732
    <xsl:choose>
733
      <xsl:when test="not(ancestor::tei:schemaSpec)">x</xsl:when>
734
      <xsl:when test="key('REFS',concat($class,$suffix))">x</xsl:when>
735
      <xsl:when test="key('REFS',$class)">x</xsl:when>
736
      <xsl:when test="not($suffix='')"/>
737
      <xsl:when test="tei:classes/tei:memberOf">
738
        <xsl:for-each select="tei:classes/tei:memberOf">
739
          <xsl:for-each select="key('CLASSES',@key)">
740
            <xsl:call-template name="findUses">
741
              <xsl:with-param name="pattern"/>
742
              <xsl:with-param name="class" select="@ident"/>
743
            </xsl:call-template>
744
          </xsl:for-each>
745
        </xsl:for-each>
746
      </xsl:when>
747
    </xsl:choose>
748
  </xsl:template>
749

    
750

    
751
  <xsl:template match="tei:classSpec" mode="tangleadd">
752
    <xsl:apply-templates mode="tangleadd"/>
753
  </xsl:template>
754

    
755

    
756
  <xsl:template match="tei:classSpec/@ident"/>
757

    
758

    
759
  <xsl:template match="tei:code">
760
    <xsl:call-template name="typewriter">
761
      <xsl:with-param name="text">
762
        <xsl:apply-templates/>
763
      </xsl:with-param>
764
    </xsl:call-template>
765
  </xsl:template>
766

    
767
  <xsl:template match="text()" mode="doc">
768
    <xsl:value-of select="."/>
769
  </xsl:template>
770

    
771
  <xsl:template match="tei:desc" mode="tangle"/>
772

    
773
  <xsl:template match="tei:classSpec" mode="classmember">
774
    <xsl:param name="suffix"/>
775
    <xsl:variable name="localprefix">
776
      <xsl:choose>
777
	<xsl:when test="@prefix">
778
	  <xsl:value-of select="@prefix"/>
779
	</xsl:when>
780
	<xsl:otherwise>
781
	  <xsl:value-of select="$generalPrefix"/>
782
	</xsl:otherwise>
783
      </xsl:choose>
784
    </xsl:variable>
785

    
786
    <xsl:choose>
787
      <xsl:when test="$suffix='' or $suffix='NULL'">
788
        <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$localprefix}{@ident}"/>
789
      </xsl:when>
790
      <xsl:otherwise>
791
        <ref xmlns="http://relaxng.org/ns/structure/1.0"
792
	     name="{$localprefix}{@ident}_{$suffix}"/>
793
      </xsl:otherwise>
794
    </xsl:choose>
795
  </xsl:template>
796

    
797
  <xsl:template match="tei:elementSpec" mode="classmember">
798
    <xsl:param name="theClass"/>
799

    
800
    <xsl:variable name="min" select="tei:classes/tei:memberOf[@key=$theClass]/@min"/>
801
    <xsl:variable name="max" select="tei:classes/tei:memberOf[@key=$theClass]/@max"/>
802

    
803
    <xsl:variable name="mini" as="xs:integer">
804
      <xsl:choose>
805
        <xsl:when test="not($min castable as xs:integer)">1</xsl:when>
806
        <xsl:otherwise><xsl:value-of select="$min"/></xsl:otherwise>
807
      </xsl:choose>
808
    </xsl:variable>
809
    <xsl:variable name="elementPrefix">
810
      <xsl:choose>
811
        <xsl:when test="@prefix">
812
          <xsl:value-of select="@prefix"/>
813
        </xsl:when>
814
        <xsl:otherwise>
815
          <xsl:value-of select="$generalPrefix"/>
816
        </xsl:otherwise>
817
      </xsl:choose>
818
    </xsl:variable>
819
    <xsl:variable name="ident" select="@ident"/>
820
    
821
    <xsl:for-each select="for $i in 1 to $mini return $i">
822
      <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$elementPrefix}{$ident}"/>
823
    </xsl:for-each> 
824
    <xsl:choose>
825
      <xsl:when test="$max='unbounded'">
826
        <zeroOrMore xmlns="http://relaxng.org/ns/structure/1.0">
827
          <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$elementPrefix}{$ident}"/>
828
        </zeroOrMore>
829
      </xsl:when>
830
      <xsl:otherwise>
831
        <xsl:variable name="maxi" as="xs:integer">
832
          <xsl:choose>
833
            <xsl:when test="not($max castable as xs:integer)">1</xsl:when>
834
            <xsl:otherwise><xsl:value-of select="$max"/></xsl:otherwise>
835
          </xsl:choose>
836
        </xsl:variable>
837
        <xsl:for-each select="for $i in ($mini+1) to $maxi return $i">
838
          <optional xmlns="http://relaxng.org/ns/structure/1.0">
839
            <ref xmlns="http://relaxng.org/ns/structure/1.0" name="{$elementPrefix}{$ident}"/>
840
          </optional>
841
        </xsl:for-each>
842
      </xsl:otherwise>
843
    </xsl:choose> 
844
  </xsl:template>
845

    
846
  <xsl:template match="tei:elementSpec" mode="tangle">
847
    <xsl:variable name="elementPrefix">
848
      <xsl:choose>
849
        <xsl:when test="@prefix">
850
          <xsl:value-of select="@prefix"/>
851
        </xsl:when>
852
        <xsl:otherwise>
853
          <xsl:value-of select="$generalPrefix"/>
854
        </xsl:otherwise>
855
      </xsl:choose>
856
    </xsl:variable>
857
    <xsl:if test="$verbose='true'">
858
      <xsl:message> elementSpec [<xsl:value-of select="$elementPrefix"/>]<xsl:value-of select="@ident"/>
859
        <xsl:if test="@xml:id">: <xsl:value-of select="@xml:id"/>
860
        </xsl:if>
861
      </xsl:message>
862
    </xsl:if>
863
    <xsl:call-template name="bitOut">
864
      <xsl:with-param name="grammar"/>
865
      <xsl:with-param name="content">
866
        <Wrapper>
867
          <xsl:variable name="name">
868
            <xsl:choose>
869
              <xsl:when test="tei:altIdent=@ident">
870
                <xsl:value-of select="@ident"/>
871
              </xsl:when>
872
              <xsl:when test="tei:altIdent">
873
                <xsl:value-of select="normalize-space(tei:altIdent)"/>
874
              </xsl:when>
875
              <xsl:otherwise>
876
                <xsl:value-of select="@ident"/>
877
              </xsl:otherwise>
878
            </xsl:choose>
879
          </xsl:variable>
880
          <xsl:choose>
881
            <xsl:when test="tei:content/rng:notAllowed">
882
              <define xmlns="http://relaxng.org/ns/structure/1.0" name="{$elementPrefix}{@ident}">
883
                <notAllowed/>
884
              </define>
885
            </xsl:when>
886
            <xsl:otherwise>
887
              <xsl:variable name="Attributes">
888
                <xsl:call-template name="summarizeAttributes"/>
889
              </xsl:variable>
890
              <define xmlns="http://relaxng.org/ns/structure/1.0" name="{$elementPrefix}{@ident}">
891
                <element name="{$name}">
892
                  <xsl:if test="@ns">
893
                    <xsl:attribute name="ns">
894
                      <xsl:value-of select="@ns"/>
895
                    </xsl:attribute>
896
                  </xsl:if>
897
                  <xsl:if test="not($oddmode='tei')">
898
                    <a:documentation>
899
                      <xsl:call-template name="makeDescription">
900
                        <xsl:with-param name="includeValList">true</xsl:with-param>
901
                        <xsl:with-param name="coded">false</xsl:with-param>
902
                      </xsl:call-template>
903
                    </a:documentation>
904
                  </xsl:if>
905
                  <xsl:choose>
906
                    <xsl:when test="$parameterize='true'">
907
                      <ref name="{$elementPrefix}{@ident}.content"/>
908
                      <xsl:if test="not($Attributes='')">
909
			<xsl:if test="$verbose='true'">
910
			  <xsl:message>   refer to attributes: </xsl:message>
911
			</xsl:if>
912
                        <ref name="{$elementPrefix}{@ident}.localattributes"/>
913
                      </xsl:if>
914
		    </xsl:when>
915
		    <xsl:otherwise>
916
                      <xsl:call-template name="defineContent"/>
917
                      <xsl:if test="not($Attributes='')">
918
			<xsl:call-template name="defineAttributes"/>
919
                      </xsl:if>
920
                    </xsl:otherwise>
921
                  </xsl:choose>
922
                  <!--
923
		  <xsl:if test="@ident='TEI' or @ident='teiCorpus'">
924
		    <optional>
925
		      <attribute name="schemaLocation"
926
				     ns="http://www.w3.org/2001/XMLSchema-instance">
927
			<text/>
928
		      </attribute>
929
		    </optional>
930
		  </xsl:if>
931
-->
932
                </element>
933
              </define>
934
              <xsl:if test="$parameterize='true'">
935
                <define xmlns="http://relaxng.org/ns/structure/1.0"
936
                  name="{$elementPrefix}{@ident}.content">
937
                  <xsl:call-template name="defineContent"/>
938
                </define>
939
                <xsl:if test="not($Attributes='')">
940
                  <define xmlns="http://relaxng.org/ns/structure/1.0"
941
                    name="{$elementPrefix}{@ident}.localattributes">
942
                    <xsl:call-template name="defineAttributes"/>
943
                  </define>
944
                </xsl:if>
945
                <xsl:apply-templates mode="tangleModel" select="tei:classes/tei:memberOf"/>
946
              </xsl:if>
947
            </xsl:otherwise>
948
          </xsl:choose>
949
        </Wrapper>
950
      </xsl:with-param>
951
    </xsl:call-template>
952
  </xsl:template>
953

    
954
  <xsl:template name="summarizeAttributes">
955
    <xsl:for-each select=".//tei:attDef">x</xsl:for-each>
956
    <xsl:for-each select="tei:classes/tei:memberOf">
957
      <xsl:for-each select="key('CLASSES',@key)">
958
        <xsl:if test="@type='atts'">x</xsl:if>
959
      </xsl:for-each>
960
    </xsl:for-each>
961
  </xsl:template>
962

    
963
  <xsl:template name="defineAttributes">
964
    <xsl:variable name="name">
965
      <xsl:choose>
966
	<xsl:when test="@prefix">
967
	  <xsl:value-of select="@prefix"/>
968
	</xsl:when>
969
	<xsl:otherwise>
970
	  <xsl:value-of select="$generalPrefix"/>
971
	</xsl:otherwise>
972
      </xsl:choose>
973
      <xsl:value-of select="@ident"/>
974
    </xsl:variable>
975
    <xsl:if test="$verbose='true'">
976
      <xsl:message>   now define attributes for <xsl:value-of
977
      select="@ident"/> (parameterize=<xsl:value-of select="$parameterize"/>)</xsl:message>
978
    </xsl:if>
979
    <xsl:if test="$parameterize='true'">
980
      <xsl:if test="$autoGlobal='true'">
981
        <ref xmlns="http://relaxng.org/ns/structure/1.0" name="att.global.attributes"/>
982
      </xsl:if>
983
      <xsl:for-each select="tei:classes/tei:memberOf">
984
        <xsl:for-each select="key('CLASSES',@key)">
985
          <xsl:if test="@type='atts'">
986
            <ref xmlns="http://relaxng.org/ns/structure/1.0">
987
	      <xsl:attribute name="name">
988
		<xsl:choose>
989
		  <xsl:when test="@prefix">
990
		    <xsl:value-of select="@prefix"/>
991
		  </xsl:when>
992
		  <xsl:otherwise>
993
		    <xsl:value-of select="$generalPrefix"/>
994
		  </xsl:otherwise>
995
		</xsl:choose>
996
		<xsl:value-of select="@ident"/>
997
		<xsl:text>.attributes</xsl:text>
998
	      </xsl:attribute>
999
	    </ref>
1000
          </xsl:if>
1001
        </xsl:for-each>
1002
      </xsl:for-each>
1003
    </xsl:if>
1004
    <xsl:apply-templates mode="tangle" select="tei:attList">
1005
      <xsl:with-param name="element">
1006
	<xsl:value-of select="$name"/>
1007
      </xsl:with-param>
1008
    </xsl:apply-templates>
1009
    <!-- place holder to make sure something gets into the
1010
	 pattern -->
1011
    <empty xmlns="http://relaxng.org/ns/structure/1.0"/>
1012
  </xsl:template>
1013

    
1014
  <xsl:template name="defineContent">
1015
    <xsl:variable name="Contents">
1016
      <TEMPTREE>
1017
        <xsl:choose>
1018
          <xsl:when test="tei:valList[@type='closed' and @repeatable='true']">
1019
            <list xmlns="http://relaxng.org/ns/structure/1.0">
1020
              <oneOrMore>
1021
                <choice>
1022
                  <xsl:call-template name="valListChildren"/>
1023
                </choice>
1024
              </oneOrMore>
1025
            </list>
1026
          </xsl:when>
1027
          <xsl:when test="tei:valList[@type='closed']">
1028
            <xsl:call-template name="valListChildren"/>
1029
          </xsl:when>
1030
          <xsl:when test="tei:content">
1031
            <xsl:apply-templates select="tei:content/*|tei:content/processing-instruction()"/>
1032
          </xsl:when>
1033
          <xsl:otherwise>
1034
            <empty xmlns="http://relaxng.org/ns/structure/1.0"/>
1035
          </xsl:otherwise>
1036
        </xsl:choose>
1037
      </TEMPTREE>
1038
    </xsl:variable>
1039
    <xsl:choose>
1040
      <xsl:when test="count($Contents/TEMPTREE/*)=0">
1041
        <empty xmlns="http://relaxng.org/ns/structure/1.0"/>
1042
      </xsl:when>
1043
      <xsl:otherwise>
1044
        <xsl:for-each select="$Contents/TEMPTREE">
1045
          <xsl:apply-templates mode="justcopy"/>
1046
        </xsl:for-each>
1047
      </xsl:otherwise>
1048
    </xsl:choose>
1049
    <xsl:apply-templates select="tei:constraintSpec"/>
1050

    
1051
  </xsl:template>
1052

    
1053

    
1054
  <xsl:template name="valListChildren">
1055
    <choice xmlns="http://relaxng.org/ns/structure/1.0">
1056
      <xsl:for-each select="tei:valList/tei:valItem">
1057
        <value>
1058
          <xsl:choose>
1059
            <xsl:when test="tei:altIdent=@ident">
1060
              <xsl:value-of select="@ident"/>
1061
            </xsl:when>
1062
            <xsl:when test="tei:altIdent">
1063
              <xsl:value-of select="normalize-space(tei:altIdent)"/>
1064
            </xsl:when>
1065
            <xsl:otherwise>
1066
              <xsl:value-of select="@ident"/>
1067
            </xsl:otherwise>
1068
          </xsl:choose>
1069
        </value>
1070
        <xsl:if test="not($oddmode='tei')">
1071
          <a:documentation>
1072
            <xsl:call-template name="makeDescription">
1073
              <xsl:with-param name="includeValList">true</xsl:with-param>
1074
              <xsl:with-param name="coded">false</xsl:with-param>
1075
            </xsl:call-template>
1076
          </a:documentation>
1077
        </xsl:if>
1078
      </xsl:for-each>
1079
    </choice>
1080
  </xsl:template>
1081

    
1082

    
1083
  <xsl:template match="tei:elementSpec/@ident"/>
1084

    
1085
  <xsl:template match="tei:elementSpec/tei:desc"/>
1086

    
1087
  <xsl:template match="tei:classSpec/tei:desc"/>
1088

    
1089
  <xsl:template match="tei:macroSpec/tei:desc"/>
1090

    
1091
  <xsl:template match="tei:elementSpec/tei:gloss"/>
1092

    
1093
  <xsl:template match="tei:classSpec/tei:gloss"/>
1094

    
1095
  <xsl:template match="tei:macroSpec/tei:gloss"/>
1096

    
1097

    
1098
  <xsl:template match="tei:index">
1099
      <xsl:call-template name="makeAnchor">
1100
	<xsl:with-param name="name">IDX-<xsl:number level="any"/>
1101
	</xsl:with-param>
1102
      </xsl:call-template>
1103
  </xsl:template>
1104

    
1105

    
1106
  <xsl:template match="tei:macroSpec" mode="tangle">
1107
    <xsl:param name="filename"/>
1108
    <xsl:variable name="macroPrefix">
1109
      <xsl:choose>
1110
        <xsl:when test="@prefix">
1111
          <xsl:value-of select="@prefix"/>
1112
        </xsl:when>
1113
        <xsl:otherwise>
1114
          <xsl:value-of select="$generalPrefix"/>
1115
        </xsl:otherwise>
1116
      </xsl:choose>
1117
    </xsl:variable>
1118

    
1119
    <xsl:variable name="entityContent">
1120
      <TEMPTREE>
1121
        <xsl:choose>
1122
	  <xsl:when test="tei:valList[@type='closed']">
1123
            <xsl:call-template name="valListChildren"/>
1124
	  </xsl:when>
1125
          <xsl:when test="tei:content/rng:group/rng:ref">
1126
	    <xsl:apply-templates select="tei:content/*|tei:content/processing-instruction()"/>	    
1127
	  </xsl:when>
1128
          <xsl:when test="tei:content/rng:group">
1129
            <choice xmlns="http://relaxng.org/ns/structure/1.0">
1130
              <xsl:apply-templates select="tei:content/rng:group/*"/>
1131
            </choice>
1132
          </xsl:when>
1133
          <xsl:otherwise>
1134
            <xsl:apply-templates select="tei:content/*|tei:content/processing-instruction()"/>
1135
          </xsl:otherwise>
1136
        </xsl:choose>
1137
      </TEMPTREE>
1138
    </xsl:variable>
1139
    <xsl:variable name="entityCount">
1140
      <xsl:for-each select="$entityContent/TEMPTREE">
1141
        <xsl:value-of select="count(rng:*|processing-instruction())"/>
1142
      </xsl:for-each>
1143
    </xsl:variable>
1144
    <xsl:choose>
1145
      <xsl:when test="@ident=&#34;TEI.singleBase&#34;"/>
1146
      <xsl:when test="starts-with($entityContent,&#34;'&#34;)">
1147
        <xsl:if test="$verbose='true'">
1148
          <xsl:message>Omit <xsl:value-of select="$entityContent"/> for <xsl:value-of select="@ident"/>
1149
          </xsl:message>
1150
        </xsl:if>
1151
      </xsl:when>
1152
      <xsl:when test="starts-with($entityContent,&#34;-&#34;)">
1153
        <xsl:if test="$verbose='true'">
1154
          <xsl:message>Omit <xsl:value-of select="$entityContent"/> for <xsl:value-of select="@ident"/>
1155
          </xsl:message>
1156
        </xsl:if>
1157
      </xsl:when>
1158
      <xsl:otherwise>
1159
        <xsl:if test="$verbose='true'">
1160
          <xsl:message> macroSpec <xsl:value-of select="@ident"/>
1161
          </xsl:message>
1162
        </xsl:if>
1163
        <xsl:call-template name="bitOut">
1164
          <xsl:with-param name="grammar">true</xsl:with-param>
1165
          <xsl:with-param name="content">
1166
            <Wrapper>
1167
              <define xmlns="http://relaxng.org/ns/structure/1.0" name="{$macroPrefix}{@ident}">
1168
                <xsl:if test="$parameterize='true'">
1169
                  <xsl:if test="starts-with(@ident,'macro.component')     or @predeclare='true'">
1170
                    <xsl:attribute name="combine">choice</xsl:attribute>
1171
                  </xsl:if>
1172
                </xsl:if>
1173
                <xsl:choose>
1174
                  <xsl:when test="starts-with(@ident,'type')">
1175
                    <xsl:apply-templates mode="justcopy" select="$entityContent/TEMPTREE/node()"/>
1176
                  </xsl:when>
1177
                  <xsl:when test="$entityCount=0">
1178
                    <choice>
1179
                      <empty/>
1180
                    </choice>
1181
                  </xsl:when>
1182
                  <xsl:when test="$entityCount=1">
1183
                    <xsl:apply-templates mode="justcopy" select="$entityContent/TEMPTREE/node()"/>
1184
                  </xsl:when>
1185
                  <xsl:when test="tei:content/rng:text|tei:content/rng:ref">
1186
                    <choice>
1187
                      <xsl:apply-templates mode="justcopy" select="$entityContent/TEMPTREE/node()"/>
1188
                    </choice>
1189
                  </xsl:when>
1190
                  <xsl:otherwise>
1191
                    <xsl:apply-templates mode="justcopy"  select="$entityContent/TEMPTREE/node()"/>
1192
                  </xsl:otherwise>
1193
                </xsl:choose>
1194
              </define>
1195
            </Wrapper>
1196
          </xsl:with-param>
1197
        </xsl:call-template>
1198
      </xsl:otherwise>
1199
    </xsl:choose>
1200
  </xsl:template>
1201

    
1202

    
1203
  <xsl:template match="tei:macroSpec/@ident"/>
1204

    
1205
  <xsl:template match="tei:macroSpec/content/rng:*"/>
1206

    
1207
  <xsl:template match="tei:memberOf" mode="tangleModel">
1208
    <!--
1209
    <xsl:variable name="owner">
1210
      <xsl:value-of
1211
        select="ancestor::tei:elementSpec/@ident|ancestor::tei:classSpec/@ident"
1212
      />
1213
    </xsl:variable>
1214
    <xsl:for-each select="key('IDENTS',@key)">
1215
      <xsl:if test="@type='model'">
1216
        <define combine="choice" name="{@ident}"
1217
          xmlns="http://relaxng.org/ns/structure/1.0">
1218
          <ref name="{$generalPrefix}{$owner}"
1219
            xmlns="http://relaxng.org/ns/structure/1.0"/>
1220
        </define>
1221
      </xsl:if>
1222
    </xsl:for-each>
1223
-->
1224
  </xsl:template>
1225

    
1226

    
1227
  <xsl:template match="tei:moduleRef" mode="tangle">
1228
    <!-- save a reference to myself so I can access my attrs and -->
1229
    <!-- generated node ID later -->
1230
    <xsl:variable name="me-the-moduleRef" select="."/>
1231
    <xsl:variable name="This" select="@key"/>
1232
    <xsl:if test="$verbose='true'">
1233
      <xsl:message> .... import module [<xsl:value-of select="$This"/> <xsl:value-of select="@url"/>] </xsl:message>
1234
    </xsl:if>
1235
    <xsl:call-template name="bitOut">
1236
      <xsl:with-param name="grammar">true</xsl:with-param>
1237
      <xsl:with-param name="content">
1238
        <Wrapper>
1239
          <xsl:choose>
1240
            <xsl:when test="@url and $parameterize='true'">
1241
              <include xmlns="http://relaxng.org/ns/structure/1.0" href="{@url}">
1242
                <xsl:apply-templates mode="justcopy"  select="tei:content/*"/>
1243
              </include>
1244
            </xsl:when>
1245
            <xsl:when test="@url and $parameterize='false'">
1246
              <xsl:comment>Start of import of <xsl:value-of select="@url"/>
1247
              </xsl:comment>
1248
              <div xmlns="http://relaxng.org/ns/structure/1.0">
1249
                <xsl:for-each select="document(@url)/rng:grammar">
1250
                  <!-- the "expandRNG" processing changed 2011-08-25 by Syd Bauman: -->
1251
                  <!-- added a 'prefix' parameter which value is prefixed to pattern -->
1252
                  <!-- names in the included schema. This prevents collisions in the -->
1253
                  <!-- output RNG. -->
1254
                  <xsl:apply-templates mode="expandRNG" select="@*|node()">
1255
                    <xsl:with-param name="prefix">
1256
		      <xsl:if test="$me-the-moduleRef/@prefix">
1257
			<xsl:value-of select="$me-the-moduleRef/@prefix"/>
1258
		      </xsl:if>
1259
                    </xsl:with-param>
1260
                  </xsl:apply-templates>
1261
                </xsl:for-each>
1262
                <xsl:apply-templates mode="justcopy"  select="tei:content/*"/>
1263
              </div>
1264
              <xsl:comment>End of import of <xsl:value-of select="@url"/>
1265
              </xsl:comment>
1266
            </xsl:when>
1267
            <xsl:otherwise>
1268
              <include xmlns="http://relaxng.org/ns/structure/1.0"
1269
                href="{$schemaBaseURL}{$This}.rng">
1270
                <xsl:attribute name="ns">
1271
                  <xsl:choose>
1272
                    <xsl:when test="ancestor::tei:schemaSpec/@ns">
1273
                      <xsl:value-of select="ancestor::tei:schemaSpec/@ns"/>
1274
                    </xsl:when>
1275
                    <xsl:otherwise>http://www.tei-c.org/ns/1.0</xsl:otherwise>
1276
                  </xsl:choose>
1277
                </xsl:attribute>
1278
                <xsl:for-each select="../tei:*[@module=$This and not(@mode='add')]">
1279
                  <xsl:apply-templates mode="tangle" select="."/>
1280
                </xsl:for-each>
1281
              </include>
1282
            </xsl:otherwise>
1283
          </xsl:choose>
1284
        </Wrapper>
1285
      </xsl:with-param>
1286
    </xsl:call-template>
1287
  </xsl:template>
1288

    
1289
  <!-- begin expand RELAX NG section -->
1290

    
1291
  <xsl:template match="@*|text()|comment()|processing-instruction" mode="expandRNG">
1292
    <xsl:copy-of select="."/>
1293
  </xsl:template>
1294
  
1295
  <xsl:template match="rng:start" mode="expandRNG"/>
1296
  
1297
  <xsl:template match="rng:include" mode="expandRNG">
1298
    <xsl:param name="prefix"/>
1299
    <xsl:if test="$verbose='true'">
1300
      <xsl:message> .... import <xsl:value-of select="@href"/></xsl:message>
1301
    </xsl:if>
1302
    <xsl:comment>Start of import of <xsl:value-of select="@href"/></xsl:comment>
1303
    <div xmlns="http://relaxng.org/ns/structure/1.0">
1304
      <xsl:for-each select="document(@href)/rng:grammar">
1305
        <xsl:apply-templates mode="expandRNG" select="@*|node()">
1306
          <xsl:with-param name="prefix" select="$prefix"/>
1307
        </xsl:apply-templates>
1308
      </xsl:for-each>
1309
    </div>
1310
    <xsl:comment>End of import of <xsl:value-of select="@href"/>
1311
    </xsl:comment>
1312
  </xsl:template>
1313
  
1314
  <xsl:template match="rng:define | rng:ref" mode="expandRNG">
1315
    <xsl:param name="prefix"/>
1316
    <xsl:if test="$verbose='true'">
1317
      <xsl:message>expanding rng:<xsl:value-of select="local-name(.)"/> name=<xsl:value-of select="@name"/>, giving it a prefix of '<xsl:value-of select="$prefix"/>'.</xsl:message>
1318
    </xsl:if>
1319
    <!-- generate a copy of this <define> or <ref> -->
1320
    <xsl:copy>
1321
      <!-- copy over all attributes (including @name) -->
1322
      <xsl:apply-templates select="@*" mode="expandRNG"/>
1323
      <xsl:if test="@name">
1324
        <!-- then replace the copied @name with our own that is the same -->
1325
        <!-- except has our prefix in the value -->
1326
        <xsl:attribute name="name" select="concat( $prefix, @name )"/>
1327
      </xsl:if>
1328
      <!-- then copy over any content -->
1329
      <xsl:apply-templates select="node()" mode="expandRNG">
1330
        <xsl:with-param name="prefix" select="$prefix"/>
1331
      </xsl:apply-templates>
1332
    </xsl:copy>
1333
  </xsl:template>
1334
  
1335
  <xsl:template match="*" mode="expandRNG">
1336
    <xsl:param name="prefix">erng_</xsl:param>
1337
    <xsl:copy>
1338
      <xsl:apply-templates mode="expandRNG" select="@*|node()">
1339
        <xsl:with-param name="prefix" select="$prefix"/>
1340
      </xsl:apply-templates>
1341
    </xsl:copy>
1342
  </xsl:template>
1343

    
1344
  <!-- end expand RELAX NG section -->
1345

    
1346
  <xsl:template match="tei:remarks" mode="tangle"/>
1347

    
1348

    
1349
  <xsl:template match="tei:specGrp" mode="ok">
1350
    <xsl:param name="filename"/>
1351
    <xsl:if test="$verbose='true'">
1352
      <xsl:message> processing specGrp <xsl:value-of select="@xml:id"/>
1353
      </xsl:message>
1354
    </xsl:if>
1355
    <xsl:call-template name="processSchemaFragment">
1356
      <xsl:with-param name="filename" select="$filename"/>
1357
    </xsl:call-template>
1358
  </xsl:template>
1359

    
1360
  <xsl:template match="tei:tag">
1361
    <xsl:call-template name="typewriter">
1362
      <xsl:with-param name="text">
1363
        <xsl:text>&lt;</xsl:text>
1364
        <xsl:apply-templates/>
1365
        <xsl:text>&gt;</xsl:text>
1366
      </xsl:with-param>
1367
    </xsl:call-template>
1368
  </xsl:template>
1369

    
1370
  <xsl:template name="compositeNumber">
1371
    <xsl:choose>
1372
      <xsl:when test="ancestor::tei:div1">
1373
        <xsl:for-each select="ancestor::tei:div1">
1374
          <xsl:number/>
1375
        </xsl:for-each>
1376
        <xsl:text>.</xsl:text>
1377
        <xsl:number from="tei:div1" level="any"/>
1378
      </xsl:when>
1379
      <xsl:otherwise>
1380
        <xsl:for-each select="ancestor::tei:div[1]">
1381
          <xsl:number count="tei:div" from="tei:text" level="multiple"/>
1382
        </xsl:for-each>
1383
        <xsl:text>.</xsl:text>
1384
        <xsl:number from="tei:div"/>
1385
      </xsl:otherwise>
1386
    </xsl:choose>
1387
  </xsl:template>
1388

    
1389
  <xsl:template match="tei:p" mode="copyrighttext">
1390
    <xsl:text>&#10;</xsl:text>
1391
    <xsl:apply-templates/>
1392
  </xsl:template>
1393

    
1394
  <xsl:template match="tei:list" mode="copyrighttext">
1395
    <xsl:text>&#10;</xsl:text>
1396
    <xsl:apply-templates/>
1397
  </xsl:template>
1398

    
1399
  <xsl:template match="tei:item" mode="copyrighttext">
1400
    <xsl:text>&#10; *</xsl:text>
1401
    <xsl:apply-templates/>
1402
  </xsl:template>
1403

    
1404

    
1405

    
1406
  <xsl:template name="attributeData">
1407
    <xsl:choose>
1408
      <xsl:when test="tei:valList[@type='closed']">
1409
        <choice xmlns="http://relaxng.org/ns/structure/1.0">
1410
          <xsl:for-each select="tei:valList/tei:valItem">
1411
            <value>
1412
              <xsl:choose>
1413
                <xsl:when test="tei:altIdent=@ident">
1414
                  <xsl:value-of select="@ident"/>
1415
                </xsl:when>
1416
                <xsl:when test="tei:altIdent">
1417
                  <xsl:value-of select="normalize-space(tei:altIdent)"/>
1418
                </xsl:when>
1419
                <xsl:otherwise>
1420
                  <xsl:value-of select="@ident"/>
1421
                </xsl:otherwise>
1422
              </xsl:choose>
1423
            </value>
1424
            <xsl:if test="not($oddmode='tei')">
1425
              <a:documentation>
1426
                <xsl:call-template name="makeDescription">
1427
                  <xsl:with-param name="includeValList">true</xsl:with-param>
1428
                  <xsl:with-param name="coded">false</xsl:with-param>
1429
                </xsl:call-template>
1430
              </a:documentation>
1431
            </xsl:if>
1432
          </xsl:for-each>
1433
        </choice>
1434
      </xsl:when>
1435
      <xsl:when test="tei:valList[@type='semi']">
1436
        <choice xmlns="http://relaxng.org/ns/structure/1.0">
1437
          <xsl:for-each select="tei:valList/tei:valItem">
1438
            <value>
1439
              <xsl:choose>
1440
                <xsl:when test="tei:altIdent=@ident">
1441
                  <xsl:value-of select="@ident"/>
1442
                </xsl:when>
1443
                <xsl:when test="tei:altIdent">
1444
                  <xsl:value-of select="normalize-space(tei:altIdent)"/>
1445
                </xsl:when>
1446
                <xsl:otherwise>
1447
                  <xsl:value-of select="@ident"/>
1448
                </xsl:otherwise>
1449
              </xsl:choose>
1450
            </value>
1451
            <xsl:if test="not($oddmode='tei')">
1452
              <a:documentation>
1453
                <xsl:call-template name="makeDescription">
1454
                  <xsl:with-param name="includeValList">true</xsl:with-param>
1455
                  <xsl:with-param name="coded">false</xsl:with-param>
1456
                </xsl:call-template>
1457
              </a:documentation>
1458
            </xsl:if>
1459
          </xsl:for-each>
1460
          <xsl:choose>
1461
            <xsl:when test="tei:datatype/rng:ref[@name='data.enumerated']">
1462
              <data type="Name"/>
1463
            </xsl:when>
1464
	    <xsl:when test="not(tei:datatype)">
1465
              <data type="Name"/>
1466
	    </xsl:when>
1467
            <xsl:otherwise>
1468
              <xsl:apply-templates select="tei:datatype/rng:*"/>
1469
            </xsl:otherwise>
1470
          </xsl:choose>
1471
        </choice>
1472
      </xsl:when>
1473
      <xsl:when test="tei:datatype/rng:*">
1474
        <xsl:apply-templates select="tei:datatype/rng:*"/>
1475
      </xsl:when>
1476
      <xsl:otherwise>
1477
        <text xmlns="http://relaxng.org/ns/structure/1.0"/>
1478
      </xsl:otherwise>
1479
    </xsl:choose>
1480
  </xsl:template>
1481

    
1482
  <xsl:template name="makeSimpleAttribute">
1483
    <xsl:variable name="name">
1484
      <xsl:choose>
1485
        <xsl:when test="tei:altIdent=@ident">
1486
          <xsl:value-of select="@ident"/>
1487
        </xsl:when>
1488
        <xsl:when test="tei:altIdent">
1489
          <xsl:value-of select="normalize-space(tei:altIdent)"/>
1490
        </xsl:when>
1491
        <xsl:otherwise>
1492
          <xsl:if test="@ns='http://www.w3.org/XML/1998/namespace'">xml:</xsl:if>
1493
          <xsl:value-of select="@ident"/>
1494
        </xsl:otherwise>
1495
      </xsl:choose>
1496
    </xsl:variable>
1497
    <attribute xmlns="http://relaxng.org/ns/structure/1.0" name="{$name}">
1498
      <xsl:if test="@ns">
1499
        <xsl:copy-of select="@ns"/>
1500
      </xsl:if>
1501
      <xsl:if test="tei:defaultVal and not(tei:defaultVal='')">
1502
        <xsl:attribute name="a:defaultValue">
1503
          <xsl:value-of select="normalize-space(tei:defaultVal)"/>
1504
        </xsl:attribute>
1505
      </xsl:if>
1506
      <xsl:if test="not($oddmode='tei')">
1507
        <a:documentation>
1508
          <xsl:call-template name="makeDescription">
1509
            <xsl:with-param name="includeValList">true</xsl:with-param>
1510
            <xsl:with-param name="coded">false</xsl:with-param>
1511
          </xsl:call-template>
1512
        </a:documentation>
1513
      </xsl:if>
1514
      <!-- ************************************ -->
1515
      <!-- Ascertain minOccurs= and maxOccurs=. -->
1516
      <!-- ************************************ -->
1517
      <!-- get the value of minOccurs=, defaulting to "1" -->
1518
      <xsl:variable name="minOccurs">
1519
        <xsl:choose>
1520
          <xsl:when test="tei:datatype/@minOccurs">
1521
            <xsl:value-of select="tei:datatype/@minOccurs"/>
1522
          </xsl:when>
1523
          <xsl:otherwise>1</xsl:otherwise>
1524
        </xsl:choose>
1525
      </xsl:variable>
1526
      <!-- get the value of maxOccurs=, defaulting to "1" -->
1527
      <xsl:variable name="maxOccurs">
1528
        <xsl:choose>
1529
          <xsl:when test="tei:datatype/@maxOccurs">
1530
            <xsl:value-of select="tei:datatype/@maxOccurs"/>
1531
          </xsl:when>
1532
          <xsl:otherwise>1</xsl:otherwise>
1533
        </xsl:choose>
1534
      </xsl:variable>
1535
      <!-- We now have two _string_ representations of the attrs, but -->
1536
      <!-- we need integers. So cast them, converting "unbounded" to  -->
1537
      <!-- a special flag value (-1): -->
1538
      <xsl:variable name="min" select="xs:integer( $minOccurs )"/>
1539
      <xsl:variable name="max">
1540
        <xsl:choose>
1541
          <xsl:when test="$maxOccurs='unbounded'">
1542
            <xsl:value-of select="xs:integer( -1 )"/>
1543
          </xsl:when>
1544
          <xsl:otherwise>
1545
            <xsl:value-of select="xs:integer( $maxOccurs )"/>
1546
          </xsl:otherwise>
1547
        </xsl:choose>
1548
      </xsl:variable>
1549
      <xsl:choose>
1550
        <xsl:when test="tei:datatype/rng:text  or  not( tei:datatype )  or  $max=1">
1551
          <!-- If there is only going to be one output RELAX NG node   --> 
1552
          <!-- in the attribute definition, then we don't need to      -->
1553
          <!-- bother with the complex min & max code below (in the    -->
1554
          <!-- <xsl:otherwise>). Although it would generate the right  -->
1555
          <!-- number of RELAX NG constructs, it wraps them in a       -->
1556
          <!-- <list>, which makes no sense in some cases, and will    -->
1557
          <!-- cause an error if the RELAX NG node inside the list is  -->
1558
          <!-- <text>.                                                 -->
1559
          <!-- Cases we handle here:                                   -->
1560
          <!-- * my <datatype> child has an <rng:text> child: only 1   -->
1561
          <!--   output node (<rng:text>), which can't be wrapped in   -->
1562
          <!--   a <list>                                              -->
1563
          <!-- * I don't have a <datatype> child: the 'attributeData'  -->
1564
          <!--   template will spit out a single <rng:text> node (see  -->
1565
          <!--   the outermost <xsl:otherwise> of that template)       -->
1566
          <!-- * @maxOccurs=1: whatever ends up being generated, there -->
1567
          <!--   will only be 1 of them, so no need for a <list>       -->
1568
          <xsl:call-template name="attributeData"/>
1569
        </xsl:when>
1570
        <xsl:otherwise>
1571
          <!-- Note that in the (erroneous) event   -->
1572
          <!-- that minOccurs= is greater than      -->
1573
          <!-- maxOccurs=, the latter is simply     -->
1574
          <!-- ignored.                             -->
1575
          <!-- Hack(?): -->
1576
          <!-- The 'attributeData' template needs to operate from this node; -->
1577
          <!-- However, once we've used for-each to "loop", we've lost the -->
1578
          <!-- current node (it has become one of the integers in the select= -->
1579
          <!-- range). So here we remember the current node, and re-set it -->
1580
          <!-- before calling 'attributeData'. Perhaps it would be better to -->
1581
          <!-- send it as a parameter to 'attributeData' and have it set the -->
1582
          <!-- current node, but since I didn't write 'attributeData', I've -->
1583
          <!-- chosen this method so I don't have to muck with it. -Syd -->
1584
          <xsl:variable name="thisNode" select="."/>
1585
          <list>
1586
            <xsl:if test="$min > 0">
1587
              <xsl:for-each select="1 to $min">
1588
                <xsl:for-each select="$thisNode">
1589
                  <xsl:call-template name="attributeData"/>
1590
                </xsl:for-each>
1591
              </xsl:for-each>
1592
            </xsl:if>
1593
            <xsl:choose>
1594
              <xsl:when test="$max= -1"><!-- i.e., unbounded -->
1595
                <zeroOrMore>
1596
                  <xsl:for-each select="$thisNode">
1597
                    <xsl:call-template name="attributeData"/>
1598
                  </xsl:for-each>
1599
                </zeroOrMore>
1600
              </xsl:when>
1601
              <xsl:otherwise>
1602
                <xsl:for-each select="xs:integer( $min + 1 ) to $max">
1603
                  <optional>
1604
                    <xsl:for-each select="$thisNode">
1605
                      <xsl:call-template name="attributeData"/>
1606
                    </xsl:for-each>
1607
                  </optional>
1608
                </xsl:for-each>
1609
              </xsl:otherwise>
1610
            </xsl:choose>
1611
          </list>
1612
        </xsl:otherwise>
1613
      </xsl:choose>
1614
    </attribute>
1615
  </xsl:template>
1616

    
1617
  <xsl:template name="makeAnAttribute">
1618
    <xsl:choose>
1619
      <xsl:when test="@usage='req'">
1620
        <xsl:call-template name="makeSimpleAttribute"/>
1621
      </xsl:when>
1622
      <!--
1623
      <xsl:when test="parent::tei:attList[@org='choice']">
1624
        <xsl:call-template name="makeSimpleAttribute"/>
1625
      </xsl:when>
1626
-->
1627
      <xsl:otherwise>
1628
        <optional xmlns="http://relaxng.org/ns/structure/1.0">
1629
          <xsl:call-template name="makeSimpleAttribute"/>
1630
        </optional>
1631
      </xsl:otherwise>
1632
    </xsl:choose>
1633
  </xsl:template>
1634

    
1635
  <xsl:template name="generateClassParents">
1636
    <xsl:choose>
1637
      <xsl:when test="not(tei:classes)"> (none) </xsl:when>
1638
      <xsl:otherwise>
1639
        <xsl:for-each select="tei:classes/tei:memberOf">
1640
          <xsl:if test="preceding-sibling::tei:memberOf">
1641
            <xsl:text>: </xsl:text>
1642
          </xsl:if>
1643
          <xsl:choose>
1644
            <xsl:when test="key('CLASSES',@key)">
1645
              <xsl:for-each select="key('CLASSES',@key)">
1646
                <xsl:call-template name="linkTogether">
1647
                  <xsl:with-param name="name" select="@ident"/>
1648
                </xsl:call-template>
1649
              </xsl:for-each>
1650
            </xsl:when>
1651
            <xsl:otherwise>
1652
              <xsl:value-of select="@key"/>
1653
            </xsl:otherwise>
1654
          </xsl:choose>
1655
        </xsl:for-each>
1656
      </xsl:otherwise>
1657
    </xsl:choose>
1658
  </xsl:template>
1659

    
1660

    
1661
  <xsl:template name="linkStyle"/>
1662

    
1663

    
1664
  <xsl:template name="getSpecURL">
1665
    <xsl:param name="name"/>
1666
    <xsl:param name="type"/>
1667
    <xsl:choose>
1668
      <xsl:when test="$type='macro'">
1669
        <xsl:for-each select="id('REFENT')">
1670
          <xsl:apply-templates mode="generateLink" select="."/>
1671
        </xsl:for-each>
1672
      </xsl:when>
1673
      <xsl:when test="$type='element'">
1674
        <xsl:for-each select="id('REFTAG')">
1675
          <xsl:apply-templates mode="generateLink" select="."/>
1676
        </xsl:for-each>
1677
      </xsl:when>
1678
      <xsl:when test="$type='class'">
1679
        <xsl:for-each select="id('REFCLA')">
1680
          <xsl:apply-templates mode="generateLink" select="."/>
1681
        </xsl:for-each>
1682
      </xsl:when>
1683
    </xsl:choose>
1684
    <xsl:text>#</xsl:text>
1685
    <xsl:value-of select="$name"/>
1686
  </xsl:template>
1687

    
1688

    
1689
  <xsl:template name="linkTogether">
1690
    <xsl:param name="name"/>
1691
    <xsl:param name="reftext"/>
1692
    <xsl:param name="class">link_odd</xsl:param>
1693

    
1694
    <xsl:variable name="documentationLanguage">
1695
      <xsl:call-template name="generateDoc"/>
1696
    </xsl:variable>
1697
    <xsl:variable name="partialname">
1698
      <xsl:choose>
1699
        <xsl:when test="contains($name,'_')">
1700
          <xsl:value-of select="substring-before($name,'_')"/>
1701
        </xsl:when>
1702
        <xsl:otherwise>
1703
          <xsl:value-of select="$name"/>
1704
        </xsl:otherwise>
1705
      </xsl:choose>
1706
    </xsl:variable>
1707
    <xsl:variable name="link">
1708
      <xsl:choose>
1709
        <xsl:when test="$reftext=''">
1710
          <xsl:value-of select="$name"/>
1711
        </xsl:when>
1712
        <xsl:otherwise>
1713
          <xsl:value-of select="$reftext"/>
1714
        </xsl:otherwise>
1715
      </xsl:choose>
1716
    </xsl:variable>
1717

    
1718
    <xsl:choose>
1719
      <xsl:when test="not(key('IDENTS',$partialname))">
1720
        <xsl:value-of select="$link"/>
1721
      </xsl:when>
1722
      <xsl:when test="$oddmode='html' and number($splitLevel)=-1">
1723
        <a xmlns="http://www.w3.org/1999/xhtml" class="{$class}" href="#{$partialname}">
1724
          <xsl:value-of select="$link"/>
1725
        </a>
1726
      </xsl:when>
1727
      <xsl:when test="$oddmode='html' and $STDOUT='true'">
1728
        <a xmlns="http://www.w3.org/1999/xhtml" class="{$class}">
1729
          <xsl:attribute name="href">
1730
            <xsl:for-each select="key('IDENTS',$partialname)">
1731
              <xsl:call-template name="getSpecURL">
1732
                <xsl:with-param name="name">
1733
                  <xsl:value-of select="$partialname"/>
1734
                </xsl:with-param>
1735
                <xsl:with-param name="type">
1736
                  <xsl:value-of select="substring-before(local-name(),'Spec')"/>
1737
                </xsl:with-param>
1738
              </xsl:call-template>
1739
            </xsl:for-each>
1740
          </xsl:attribute>
1741
          <xsl:value-of select="$link"/>
1742
        </a>
1743
      </xsl:when>
1744

    
1745

    
1746
      <xsl:when test="$oddmode='html'">
1747
        <a xmlns="http://www.w3.org/1999/xhtml" class="{$class}"
1748
          href="{concat('ref-',$partialname,'.html')}">
1749
          <xsl:value-of select="$link"/>
1750
        </a>
1751
      </xsl:when>
1752

    
1753
      <xsl:when test="$oddmode='pdf'">
1754
        <fo:inline>
1755
          <xsl:value-of select="$link"/>
1756
        </fo:inline>
1757
      </xsl:when>
1758

    
1759
      <xsl:when test="$oddmode='tei'">
1760
        <tei:ref target="#{$partialname}">
1761
          <xsl:value-of select="$link"/>
1762
        </tei:ref>
1763
      </xsl:when>
1764

    
1765
      <xsl:otherwise>
1766
        <xsl:value-of select="$link"/>
1767
      </xsl:otherwise>
1768
    </xsl:choose>
1769
  </xsl:template>
1770

    
1771

    
1772
  <xsl:template name="processSchemaFragment">
1773
    <xsl:param name="filename"/>
1774
    <xsl:variable name="secnum">
1775
      <xsl:call-template name="sectionNumber"/>
1776
    </xsl:variable>
1777
    <xsl:apply-templates mode="tangle"/>
1778
  </xsl:template>
1779

    
1780

    
1781
  <xsl:template name="make-ns-declaration">
1782
    <xsl:param name="is-default"/>
1783
    <xsl:param name="prefix"/>
1784
    <xsl:param name="uri"/>
1785
  </xsl:template>
1786

    
1787
  <xsl:template name="inhnamespace"/>
1788

    
1789
  <xsl:template match="tei:constraintSpec/tei:desc"/>
1790
  <xsl:template match="tei:constraintSpec/tei:gloss"/>
1791
  <xsl:template match="tei:constraintSpec/tei:equiv"/>
1792

    
1793

    
1794
  <xsl:template match="tei:constraintSpec"/>
1795

    
1796
  <xsl:template match="tei:altIdent"/>
1797

    
1798
  <xsl:template match="a:*">
1799
    <xsl:apply-templates mode="justcopy" select="."/>
1800
  </xsl:template>
1801

    
1802
  <xsl:template match="tei:classSpec" mode="processDefaultAtts">
1803
    <xsl:if test="$verbose='true'">
1804
      <xsl:message> .. default attribute settings for <xsl:value-of select="@ident"/>
1805
      </xsl:message>
1806
    </xsl:if>
1807
    <xsl:call-template name="bitOut">
1808
      <xsl:with-param name="grammar">true</xsl:with-param>
1809
      <xsl:with-param name="content">
1810
        <Wrapper>
1811
	  <xsl:variable name="c">
1812
	    <xsl:choose>
1813
	      <xsl:when test="@prefix">
1814
		<xsl:value-of select="@prefix"/>
1815
	      </xsl:when>
1816
	      <xsl:otherwise>
1817
		<xsl:value-of select="$generalPrefix"/>
1818
	      </xsl:otherwise>
1819
	    </xsl:choose>
1820
	    <xsl:value-of select="@ident"/>
1821
	  </xsl:variable>
1822
          <define xmlns="http://relaxng.org/ns/structure/1.0" combine="choice"
1823
            name="{$c}.attributes">
1824
            <empty/>
1825
          </define>
1826
        </Wrapper>
1827
      </xsl:with-param>
1828
    </xsl:call-template>
1829
  </xsl:template>
1830

    
1831
  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
1832
    <desc>Process element schemaSpec</desc>
1833
  </doc>
1834

    
1835
  <xsl:template match="tei:schemaSpec">
1836
    <xsl:call-template name="processSchemaFragment"/>
1837
  </xsl:template>
1838

    
1839
  <xsl:template name="generateOutput">
1840
    <xsl:param name="body"/>
1841
    <xsl:param name="suffix"/>
1842
    <xsl:param name="method">xml</xsl:param>
1843
    <xsl:variable name="processor">
1844
      <xsl:value-of select="system-property('xsl:vendor')"/>
1845
    </xsl:variable>
1846
    <xsl:choose>
1847
      <xsl:when test="$outputDir='' or $outputDir='-'">
1848
        <xsl:copy-of select="$body"/>
1849
      </xsl:when>
1850
      <xsl:otherwise>
1851
        <xsl:result-document href="{$outputDir}/{@ident}{$suffix}" method="{$method}">
1852
          <xsl:copy-of select="$body"/>
1853
        </xsl:result-document>
1854
      </xsl:otherwise>
1855
    </xsl:choose>
1856
  </xsl:template>
1857

    
1858

    
1859
  <xsl:template name="showDate">
1860
    <xsl:choose>
1861
      	<xsl:when test="$useFixedDate='true'">1970-01-01</xsl:when>
1862
	<xsl:otherwise>
1863
    <xsl:value-of
1864
	select="format-dateTime(current-dateTime(),'[Y]-[M02]-[D02]T[H02]:[m02]:[s02]Z')"/>
1865
	</xsl:otherwise>
1866
    </xsl:choose>
1867
</xsl:template>
1868

    
1869
  <xsl:template name="sectionNumber">
1870
    <xsl:for-each
1871
      select="(ancestor::tei:div1|ancestor::tei:div2|ancestor::tei:div3|ancestor::tei:div4)[last()]">
1872
      <xsl:for-each select="ancestor-or-self::tei:div1">
1873
        <xsl:number from="tei:body" level="any"/>
1874
        <xsl:text>.</xsl:text>
1875
      </xsl:for-each>
1876
      <xsl:number count="tei:div2|tei:div3|tei:div4" from="tei:div1" level="multiple"/>
1877
    </xsl:for-each>
1878
  </xsl:template>
1879

    
1880

    
1881
  <xsl:template match="*" mode="expandSpecs">
1882
    <xsl:apply-templates mode="justcopy" select="."/>
1883
  </xsl:template>
1884

    
1885
  <xsl:template match="tei:specGrpRef" mode="expandSpecs">
1886
    <xsl:choose>
1887
      <xsl:when test="starts-with(@target,'#')">
1888
        <xsl:for-each select="id(substring(@target,2))">
1889
          <xsl:apply-templates mode="expandSpecs"/>
1890
        </xsl:for-each>
1891
      </xsl:when>
1892
      <xsl:otherwise>
1893
        <xsl:for-each select="document(@target)">
1894
          <xsl:apply-templates mode="expandSpecs"/>
1895
        </xsl:for-each>
1896
      </xsl:otherwise>
1897
    </xsl:choose>
1898
  </xsl:template>
1899

    
1900
  <!-- list inside <desc> -->
1901
  <xsl:template match="tei:desc/tei:list/tei:item">
1902
    <xsl:text> * </xsl:text>
1903
    <xsl:apply-templates/>
1904
  </xsl:template>
1905

    
1906
  <xsl:template name="makeTEIVersion">
1907
    <xsl:choose>
1908
      <xsl:when test="ancestor-or-self::tei:TEI/processing-instruction()[name()='TEIVERSION']">
1909
        <!-- JC Additions to form proper URL from version number -->
1910
        <xsl:variable name="TEIVersion"
1911
          select="ancestor-or-self::tei:TEI/processing-instruction()[name()='TEIVERSION'][1]"/>
1912
        <xsl:variable name="TEIVersion-edition"
1913
          select="substring-before($TEIVersion, ' Last')"/>
1914
        <xsl:variable name="TEIVersion-datestring"
1915
          select="concat(' Last',substring-after($TEIVersion, ' Last'))"/>
1916
        <xsl:variable name="TEIVersionWithoutFullStop">
1917
          <xsl:choose>
1918
            <xsl:when
1919
              test="substring($TEIVersion-edition,
1920
              string-length($TEIVersion-edition)) =
1921
              '.' and matches($TEIVersion-edition, '\d\d*\.\d\d*\.\d\d*\.')">
1922
              <xsl:value-of
1923
                select="substring($TEIVersion-edition,0,string-length($TEIVersion-edition))"
1924
              />
1925
            </xsl:when>
1926
            <xsl:otherwise>
1927
              <xsl:value-of select="$TEIVersion-edition"/>
1928
            </xsl:otherwise>
1929
          </xsl:choose>
1930
        </xsl:variable>
1931
        <xsl:variable name="versionURL"
1932
          select="concat('http://www.tei-c.org/Vault/P5/', $TEIVersionWithoutFullStop, '/')"/>
1933
        <xsl:text>&#10;Edition: </xsl:text>
1934
        <xsl:value-of select="$TEIVersion"/>
1935
        <xsl:text>&#10;Edition Location: </xsl:text>
1936
        <xsl:value-of select="$versionURL"/>
1937
        <xsl:text>&#10;</xsl:text>
1938
        </xsl:when>
1939
      <xsl:when
1940
        test="ancestor-or-self::tei:TEI/tei:teiHeader/tei:fileDesc/tei:editionStmt/tei:edition">
1941
        <xsl:text>&#10;Edition: </xsl:text>
1942
        <xsl:value-of
1943
          select="ancestor-or-self::tei:TEI/tei:teiHeader/tei:fileDesc/tei:editionStmt/tei:edition"/>
1944
        <xsl:text>&#10;</xsl:text>
1945
      </xsl:when>
1946
    </xsl:choose>
1947
  </xsl:template>
1948

    
1949
  <xsl:template match="tei:gloss" mode="inLanguage">
1950
    <xsl:value-of select="."/>
1951
  </xsl:template>
1952
  <xsl:template match="tei:desc" mode="inLanguage">
1953
    <xsl:apply-templates/>
1954
  </xsl:template>
1955

    
1956
  <xsl:template name="processSchematron">
1957
    <xsl:choose>
1958
      <xsl:when test="ancestor::teix:egXML"/>
1959
      <xsl:when test="self::s:ns">
1960
        <ns prefix="{@prefix}" uri="{@uri}" xmlns="http://www.ascc.net/xml/schematron"/>
1961
      </xsl:when>
1962
      <xsl:when test="(self::s:report or self::s:assert) and ancestor::tei:elementSpec">
1963
        <pattern xmlns="http://www.ascc.net/xml/schematron">
1964
          <xsl:attribute name="name">
1965
            <xsl:value-of select="ancestor::tei:elementSpec/@ident"/>
1966
            <xsl:text>-constraint-</xsl:text>
1967
            <xsl:value-of select="ancestor::tei:constraintSpec/@ident"/>
1968
            <xsl:if test="count(../s:report|s:assert) &gt;1">
1969
              <xsl:number/>
1970
            </xsl:if>
1971
          </xsl:attribute>
1972
          <rule>
1973
            <xsl:attribute name="context">
1974
              <xsl:text>tei:</xsl:text>
1975
              <xsl:value-of select="ancestor::tei:elementSpec/@ident"/>
1976
            </xsl:attribute>
1977
            <xsl:apply-templates mode="justcopy" select="."/>
1978
          </rule>
1979
        </pattern>
1980
      </xsl:when>
1981
      <xsl:when test="self::s:pattern">
1982
        <xsl:apply-templates mode="justcopy" select="."/>
1983
      </xsl:when>
1984
      <xsl:when test="self::s:rule">
1985
        <pattern name="{ancestor::tei:constraintSpec/parent::*/@ident}-constraint-{ancestor::tei:constraintSpec/@ident}"
1986
          xmlns="http://www.ascc.net/xml/schematron">
1987
          <xsl:apply-templates mode="justcopy" select="."/>
1988
        </pattern>
1989
      </xsl:when>
1990
      <xsl:when test="self::sch:ns">
1991
        <ns prefix="{@prefix}" uri="{@uri}" xmlns="http://purl.oclc.org/dsdl/schematron"/>
1992
      </xsl:when>
1993
      <xsl:when test="self::sch:pattern">
1994
        <xsl:apply-templates mode="justcopy" select="."/>
1995
      </xsl:when>
1996
      <xsl:when test="self::sch:rule">
1997
        <pattern xmlns="http://purl.oclc.org/dsdl/schematron"
1998
          id="{ancestor::tei:constraintSpec/parent::*/@ident}-constraint-{ancestor::tei:constraintSpec/@ident}">
1999
          <xsl:apply-templates mode="justcopy" select="."/>
2000
        </pattern>
2001
      </xsl:when>
2002
      <xsl:when test="(self::sch:report or self::sch:assert) and ancestor::tei:elementSpec">
2003
        <pattern xmlns="http://purl.oclc.org/dsdl/schematron">
2004
          <xsl:attribute name="id">
2005
            <xsl:value-of select="ancestor::tei:elementSpec/@ident"/>
2006
            <xsl:text>-constraint-</xsl:text>
2007
            <xsl:value-of select="../../@ident"/>
2008
            <xsl:if test="count(../sch:report|../sch:assert) &gt;1">
2009
              <xsl:number/>
2010
            </xsl:if>
2011
          </xsl:attribute>
2012
          <rule>
2013
            <xsl:attribute name="context">
2014
              <xsl:text>tei:</xsl:text>
2015
              <xsl:value-of select="ancestor::tei:elementSpec/@ident"/>
2016
            </xsl:attribute>
2017
            <xsl:apply-templates mode="justcopy" select="."/>
2018
          </rule>
2019
        </pattern>
2020
      </xsl:when>
2021
    </xsl:choose>
2022
  </xsl:template>
2023

    
2024

    
2025
   <xsl:template match="@*|text()" mode="justcopy">
2026
      <xsl:copy-of select="."/>
2027
   </xsl:template>
2028

    
2029
   <xsl:template match="processing-instruction()" mode="justcopy">
2030
      <xsl:copy-of select="."/>
2031
   </xsl:template>
2032

    
2033
   <xsl:template match="*" mode="justcopy">
2034
     <xsl:copy>
2035
         <xsl:apply-templates
2036
	     select="*|@*|processing-instruction()|text()" mode="justcopy"/>
2037
     </xsl:copy>
2038
   </xsl:template>
2039

    
2040
   <xsl:template match="a:*" mode="justcopy">
2041
      <xsl:element name="{name()}">
2042
         <xsl:apply-templates
2043
	     select="*|@*|processing-instruction()|text()" mode="justcopy"/>
2044
      </xsl:element>
2045
   </xsl:template>
2046

    
2047
</xsl:stylesheet>