Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / odt / odttotei.xsl @ 187

History | View | Annotate | Download (41.2 kB)

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet
3
  exclude-result-prefixes="office style text table draw fo xlink dc
4
			   meta number tei svg chart dr3d math form
5
			   script ooo ooow oooc dom xforms xs xsd xsi"
6
  office:version="1.0" version="2.0" 
7
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
8
  xmlns="http://www.tei-c.org/ns/1.0"
9
  xmlns:tei="http://www.tei-c.org/ns/1.0"
10
  xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
11
  xmlns:dc="http://purl.org/dc/elements/1.1/"
12
  xmlns:dom="http://www.w3.org/2001/xml-events"
13
  xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
14
  xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
15
  xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
16
  xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
17
  xmlns:math="http://www.w3.org/1998/Math/MathML"
18
  xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
19
  xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
20
  xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
21
  xmlns:ooo="http://openoffice.org/2004/office"
22
  xmlns:oooc="http://openoffice.org/2004/calc"
23
  xmlns:ooow="http://openoffice.org/2004/writer"
24
  xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
25
  xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
26
  xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
27
  xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
28
  xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
29
  xmlns:xforms="http://www.w3.org/2002/xforms"
30
  xmlns:xlink="http://www.w3.org/1999/xlink"
31
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
32
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
34

    
35
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
36
      <desc>
37
         <p> TEI stylesheet for making TEI files from
38
	 OpenOffice. Originally derived from the OpenOffice /Docbook
39
	 conversion, but largely rewritten</p>
40
         <p> 
41
            <h1 xmlns="">License</h1>This software is dual-licensed:
42

    
43
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
44
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
45

    
46
2. http://www.opensource.org/licenses/BSD-2-Clause
47
		
48
All rights reserved.
49

    
50
Redistribution and use in source and binary forms, with or without
51
modification, are permitted provided that the following conditions are
52
met:
53

    
54
* Redistributions of source code must retain the above copyright
55
notice, this list of conditions and the following disclaimer.
56

    
57
* Redistributions in binary form must reproduce the above copyright
58
notice, this list of conditions and the following disclaimer in the
59
documentation and/or other materials provided with the distribution.
60

    
61
This software is provided by the copyright holders and contributors
62
"as is" and any express or implied warranties, including, but not
63
limited to, the implied warranties of merchantability and fitness for
64
a particular purpose are disclaimed. In no event shall the copyright
65
holder or contributors be liable for any direct, indirect, incidental,
66
special, exemplary, or consequential damages (including, but not
67
limited to, procurement of substitute goods or services; loss of use,
68
data, or profits; or business interruption) however caused and on any
69
theory of liability, whether in contract, strict liability, or tort
70
(including negligence or otherwise) arising in any way out of the use
71
of this software, even if advised of the possibility of such damage.
72
</p>
73
         <p>Author: See AUTHORS</p>
74
         <p>Id: $Id: odttotei.xsl 10180 2012-03-25 14:48:46Z rahtz $</p>
75
         <p>Copyright: 2008, TEI Consortium</p>
76
      </desc>
77
   </doc>
78

    
79
  <xsl:key match="style:style" name="STYLES" use="@style:name"/>
80

    
81

    
82
  <xsl:key name="LISTS" 
83
	 match="text:list-level-style-number" 
84
	 use="parent::text:list-style/@style:name"/>
85

    
86
  <xsl:key match="text:h" name="Headings" use="text:outline-level"/>
87

    
88
  <xsl:param name="debug">false</xsl:param>
89

    
90
  <xsl:param name="dir">.</xsl:param>
91

    
92
  <xsl:output encoding="utf-8" indent="yes"/>
93

    
94
  <!--  <xsl:strip-space elements="text:span"/>-->
95
  
96
  <xsl:variable name="META">
97
    <xsl:choose>
98
      <xsl:when test="doc-available(concat($dir,'/meta.xml'))">
99
        <xsl:copy-of select="document(concat($dir,'/meta.xml'))//office:meta"/>
100
      </xsl:when>
101
      <xsl:when test="/office:document/office:meta">
102
        <xsl:copy-of select="/office:document/office:meta"/>
103
      </xsl:when>
104
      <xsl:otherwise>
105
        <xsl:copy-of select="/office:document-meta/office:meta"/>
106
      </xsl:otherwise>
107
    </xsl:choose>
108
  </xsl:variable>
109

    
110
  <xsl:variable name="document-title">
111
    <xsl:choose>
112
      <xsl:when
113
	  test="/office:document-content/office:body/office:text/text:p[@text:style-name='Title']">
114
        <xsl:value-of
115
	    select="/office:document-content/office:body/office:text/text:p[@text:style-name='Title'][1]"
116
	    />
117
      </xsl:when>
118
      <xsl:when test="$META/office:meta/dc:title">
119
        <xsl:value-of select="$META/office:meta/dc:title"/>
120
      </xsl:when>
121
      <xsl:otherwise>
122
        <xsl:text>Untitled Document</xsl:text>
123
      </xsl:otherwise>
124
    </xsl:choose>
125
  </xsl:variable>
126

    
127
  <xsl:template match="*" mode="summary">
128
    <xsl:text>&#10;</xsl:text>
129
    <xsl:for-each select="ancestor::*"><xsl:text>.</xsl:text></xsl:for-each>
130
    <xsl:value-of select="name()"/>:
131
    <xsl:apply-templates mode="summary"/>
132
  </xsl:template>
133

    
134
  <xsl:template match="/">
135
    <xsl:if test="$debug='true'">
136
      <xsl:message>Look for metadata in <xsl:value-of select="concat($dir,'/meta.xml')"/></xsl:message>
137
    </xsl:if>
138
    <xsl:variable name="pass1">
139
      <xsl:apply-templates/>
140
    </xsl:variable>
141
    <xsl:variable name="pass2">
142
      <xsl:apply-templates mode="pass2" select="$pass1"/>
143
    </xsl:variable>
144
    <xsl:apply-templates mode="pass3" select="$pass2"/>    
145
  </xsl:template>
146

    
147
  <xsl:template match="office:document-content|office:body">
148
    <xsl:for-each select="descendant::text:variable-decl">
149
      <xsl:variable name="name">
150
        <xsl:value-of select="@text:name"/>
151
      </xsl:variable>
152
      <xsl:if test="contains(@text:name,'entitydecl')">
153
        <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE TEI [
154
	</xsl:text>
155
        <xsl:text disable-output-escaping="yes">&lt;!ENTITY </xsl:text>
156
        <xsl:value-of select="substring-after(@text:name,'entitydecl_')"/>
157
        <xsl:text> &quot;</xsl:text>
158
        <xsl:value-of
159
          select="/descendant::text:variable-set[@text:name= $name][1]"/>
160
        <xsl:text disable-output-escaping="yes">&quot;&gt;</xsl:text>
161
        <xsl:text disable-output-escaping="yes">]&gt;</xsl:text>
162
      </xsl:if>
163
    </xsl:for-each>
164
    <TEI>
165
	<xsl:for-each select="$META/office:meta/dc:language">
166
	  <xsl:attribute name="xml:lang">
167
	    <xsl:value-of select="normalize-space(.)"/>
168
	  </xsl:attribute>
169
	</xsl:for-each>  
170
      <xsl:call-template name="teiHeader"/>
171
      <text>
172
	<xsl:apply-templates/>
173
      </text>
174
    </TEI>
175
  </xsl:template>
176

    
177

    
178
  <xsl:template match="text:variable-set|text:variable-get">
179
    <xsl:choose>
180
      <xsl:when test="contains(@text:style-name,'entitydecl')">
181
        <xsl:text disable-output-escaping="yes">&amp;</xsl:text>
182
        <xsl:value-of select="substring-after(@text:style-name,'entitydecl_')"/>
183
        <xsl:text disable-output-escaping="yes">;</xsl:text>
184
      </xsl:when>
185
    </xsl:choose>
186
  </xsl:template>
187

    
188
  <xsl:template name="teiHeader">
189
    <teiHeader>
190
      <fileDesc>
191
        <titleStmt>
192
          <title>
193
            <xsl:value-of select="$document-title"/>
194
          </title>
195
          <author>
196
            <xsl:value-of
197
              select="$META/office:meta/meta:initial-creator"/>
198
          </author>
199
        </titleStmt>
200
        <editionStmt>
201
          <edition>
202
            <date>
203
              <xsl:value-of
204
                select="$META/office:meta/meta:creation-date"/>
205
            </date>
206
          </edition>
207
        </editionStmt>
208
        <publicationStmt>
209
          <p>no publication statement available</p>
210
        </publicationStmt>
211
        <sourceDesc>
212
          <p>
213
	    <xsl:apply-templates
214
              select="$META/office:meta/meta:generator"/>
215
	    <xsl:text>Written by OpenOffice</xsl:text>
216
	  </p>
217
        </sourceDesc>
218
      </fileDesc>
219
      <xsl:variable name="pD">
220
	<xsl:if test="$META/office:meta/dc:language">
221
	  <langUsage>
222
	    <language>
223
	      <xsl:attribute name="ident">
224
		<xsl:value-of select="$META/office:meta/dc:language"/>
225
	      </xsl:attribute>
226
	      <xsl:value-of select="$META/office:meta/dc:language"/>
227
	    </language>
228
	  </langUsage>
229
	</xsl:if>
230
	<xsl:if test="$META/office:meta/meta:keyword">
231
	  <textClass>
232
	    <keywords scheme="unknown">
233
	      <list>
234
		<xsl:for-each select="$META/office:meta/meta:keyword">
235
		  <item>
236
		    <xsl:value-of select="."/>
237
		  </item>
238
		</xsl:for-each>
239
	      </list>
240
	    </keywords>
241
	  </textClass>
242
	</xsl:if>
243
      </xsl:variable>
244
      <xsl:if test="$pD/*">
245
	<profileDesc>
246
	  <xsl:copy-of select="$pD"/>
247
	</profileDesc>
248
      </xsl:if>
249
      <revisionDesc>
250
	<change>
251
	  <name>
252
	    <xsl:apply-templates
253
		select="$META/office:meta/dc:creator"/>
254
	  </name>
255
	  <date>
256
	    <xsl:apply-templates select="$META/office:meta/dc:date"/>
257
	  </date>
258
	</change>
259
      </revisionDesc>
260
    </teiHeader>
261
  </xsl:template>
262

    
263

    
264

    
265
  <xsl:template match="/office:document-content/office:body">
266
      <xsl:apply-templates/>
267
  </xsl:template>
268

    
269
  <!-- sections -->
270
  <xsl:template match="text:h">
271
    <xsl:choose>
272
      <xsl:when test="ancestor::text:note-body">
273
          <xsl:apply-templates/>
274
      </xsl:when>
275
      <xsl:when test="@text:style-name='ArticleInfo'"> </xsl:when>
276
      <xsl:when test="@text:style-name='Abstract'">
277
        <div type="abstract">
278
          <xsl:apply-templates/>
279
        </div>
280
      </xsl:when>
281
      <xsl:when test="@text:style-name='Appendix'">
282
        <div type="appendix">
283
          <xsl:apply-templates/>
284
        </div>
285
      </xsl:when>
286
      <xsl:otherwise>
287
	<HEAD level="1" style="{@text:style-name}">
288
	  <xsl:apply-templates/>
289
	</HEAD>
290
      </xsl:otherwise>
291
    </xsl:choose>
292
  </xsl:template>
293

    
294
  <xsl:template match="text:h[@text:outline-level]">
295
    <xsl:choose>
296
      <xsl:when test="ancestor::text:note-body">
297
	<p>
298
	  <xsl:attribute name="rend">
299
	    <xsl:choose>
300
	      <xsl:when test="@text:style-name">
301
		<xsl:value-of select="@text:style-name"/>
302
	      </xsl:when>
303
	      <xsl:otherwise>heading</xsl:otherwise>
304
	    </xsl:choose>
305
	  </xsl:attribute>
306
          <xsl:apply-templates/>
307
	</p>
308
      </xsl:when>
309
      <xsl:otherwise>
310
	<HEAD level="{@text:outline-level}" >
311
	  <xsl:attribute name="style">
312
	    <xsl:choose>
313
	      <xsl:when test="@text:style-name">
314
		<xsl:value-of select="@text:style-name"/>
315
	      </xsl:when>
316
	      <xsl:otherwise>nostyle</xsl:otherwise>
317
	    </xsl:choose>
318
	  </xsl:attribute>
319
	  <xsl:apply-templates/>
320
	</HEAD>
321
      </xsl:otherwise>
322
    </xsl:choose>
323
  </xsl:template>
324
  
325
  <!-- special case paragraphs -->
326
  <xsl:template match="text:p[@text:style-name='XMLComment']">
327
    <xsl:comment>
328
      <xsl:value-of select="."/>
329
    </xsl:comment>
330
  </xsl:template>
331
 
332

    
333
  <xsl:template match="text:p[@text:style-name]">
334
    <xsl:choose>
335
      <xsl:when test="draw:frame and parent::draw:text-box">
336
	<xsl:apply-templates select="draw:frame"/>
337
	<head>
338
	  <xsl:apply-templates select="text()|*[not(local-name(.)='frame')]"/>
339
	</head>
340
      </xsl:when>
341
      <xsl:when test="parent::table:table-cell">
342
	<xsl:call-template name="applyStyle"/>
343
      </xsl:when>
344

    
345
      <xsl:when test="count(parent::text:note-body/text:p)=1">
346
          <xsl:apply-templates/>
347
      </xsl:when>
348

    
349
      <xsl:when test="count(parent::text:list-item/text:p)=1">
350
          <xsl:apply-templates/>
351
      </xsl:when>
352

    
353
      <xsl:when test="@text:style-name='Document Title'">
354
        <title>
355
          <xsl:apply-templates/>
356
        </title>
357
      </xsl:when>
358

    
359
      <xsl:when test="@text:style-name='Author'">
360
        <author>
361
          <xsl:apply-templates/>
362
        </author>
363
      </xsl:when>
364

    
365
      <xsl:when test="@text:style-name='lg'">
366
        <lg>
367
	  <xsl:for-each-group select="node()"
368
			      group-ending-with="text:line-break">
369
	    <l><xsl:apply-templates select="current-group()"/></l>
370
	  </xsl:for-each-group>
371
        </lg>
372
      </xsl:when>
373
      <xsl:when test="@text:style-name='Title'">
374
        <title>
375
          <xsl:apply-templates/>
376
        </title>
377
      </xsl:when>
378
      <xsl:when test="@text:style-name='Date'">
379
        <date>
380
          <xsl:apply-templates/>
381
        </date>
382
      </xsl:when>
383
      <xsl:when test="@text:style-name='Section Title'">
384
        <head>
385
          <xsl:apply-templates/>
386
        </head>
387
      </xsl:when>
388
      <xsl:when test="@text:style-name='Appendix Title'">
389
        <head>
390
          <xsl:apply-templates/>
391
        </head>
392
      </xsl:when>
393
      <xsl:when test="@text:style-name='Screen'">
394
        <Screen>
395
          <xsl:apply-templates/>
396
        </Screen>
397
      </xsl:when>
398
      <xsl:when test="@text:style-name='Output'">
399
        <Output>
400
          <xsl:apply-templates/>
401
        </Output>
402
      </xsl:when>
403

    
404
      <xsl:otherwise>
405
        <p>
406
          <xsl:variable name="stylename"><xsl:value-of select="@text:style-name"></xsl:value-of></xsl:variable>
407
          <xsl:choose>
408
            <xsl:when test="matches($stylename,'P[0-9]+')">
409
              <xsl:attribute name="rend" select="//style:style[@style:name=$stylename]/@style:parent-style-name"></xsl:attribute>
410
            </xsl:when>
411
            <xsl:otherwise>
412
              <xsl:attribute name="rend" select="@text:style-name"></xsl:attribute>
413
            </xsl:otherwise>
414
          </xsl:choose> 
415
          <!-- <xsl:attribute name="rend" select="@text:style-name"/> -->
416
	<xsl:apply-templates/>
417
<!--      <xsl:call-template name="applyStyle"/>-->
418
        </p>
419
      </xsl:otherwise>
420
    </xsl:choose>
421
  </xsl:template>
422

    
423
  <xsl:template match="office:annotation/text:p" priority="100">
424
    <note>
425
        <xsl:apply-templates/>
426
    </note>
427
  </xsl:template>
428

    
429

    
430
  <!-- normal paragraphs -->
431
  <xsl:template match="text:p">
432
    <xsl:choose>
433
      <xsl:when test="parent::text:list-item">
434
	<xsl:call-template name="applyStyle"/>
435
      </xsl:when>
436
      <xsl:when test="parent::table:table-cell">
437
        <xsl:call-template name="applyStyle"/>
438
      </xsl:when>
439
      <xsl:when test="@text:style-name='Table'"/>
440
      <xsl:when test="text:span[@text:style-name = 'XrefLabel']"/>
441
      <xsl:when test="@text:style-name='Speech'">
442
        <sp>
443
          <speaker/>
444
          <p>
445
            <xsl:call-template name="id.attribute"/>
446
            <xsl:call-template name="applyStyle"/>
447
          </p>
448
        </sp>
449
      </xsl:when>
450
      <xsl:otherwise>
451
        <p>
452
          <xsl:call-template name="id.attribute"/>
453
          <xsl:call-template name="applyStyle"/>
454
        </p>
455
      </xsl:otherwise>
456
    </xsl:choose>
457
  </xsl:template>
458

    
459
  <!-- lists -->
460
  <xsl:template match="text:list">
461
    <xsl:variable name="style">
462
      <xsl:for-each select="key('LISTS',@text:style-name)[1]">
463
	<xsl:value-of select="@text:style-name"/>
464
      </xsl:for-each>
465
    </xsl:variable>
466
    <xsl:choose>
467
      <xsl:when test="text:list-item/text:h">
468
	<xsl:for-each select="text:list-item">
469
	  <xsl:apply-templates/>
470
	</xsl:for-each>
471
      </xsl:when>
472
      <xsl:when test="@text:style-name='Var List'">
473
        <list>
474
          <xsl:apply-templates/>
475
        </list>
476
      </xsl:when>
477
      <xsl:when test="contains($style,'Number')">
478
        <list type="ordered">
479
          <xsl:apply-templates/>
480
        </list>
481
      </xsl:when>
482
      <xsl:otherwise>
483
        <list type="unordered">
484
          <xsl:apply-templates/>
485
        </list>
486
      </xsl:otherwise>
487
    </xsl:choose>
488
  </xsl:template>
489

    
490
  <xsl:template match="text:list-header">
491
    <head>
492
      <xsl:apply-templates/>
493
    </head>
494
  </xsl:template>
495

    
496
  <xsl:template match="text:list-item">
497
    <xsl:choose>
498
      <xsl:when test="parent::text:list/@text:style-name='Var List'">
499
        <item>
500
          <xsl:for-each select="text:p[@text:style-name='VarList Term']">
501
            <xsl:apply-templates select="."/>
502
          </xsl:for-each>
503
        </item>
504
      </xsl:when>
505
      <xsl:otherwise>
506
        <item>
507
	  <xsl:apply-templates/>
508
	</item>
509
      </xsl:otherwise>
510
    </xsl:choose>
511
  </xsl:template>
512

    
513

    
514
  <xsl:template
515
    match="text:p[@text:style-name='VarList Item' or
516
	   @text:style-name='List Contents']">
517
    <GLOSS n="item">
518
      <xsl:apply-templates/>
519
    </GLOSS>
520
  </xsl:template>
521

    
522
  <xsl:template
523
    match="text:p[@text:style-name='VarList Term' or @text:style-name='List Heading']">
524
    <GLOSS n="label"> 
525
      <xsl:apply-templates/>
526
    </GLOSS>
527
  </xsl:template>
528

    
529
  <!-- inline -->
530

    
531
  <xsl:template match="text:span">
532
    <xsl:variable name="Style">
533
      <xsl:value-of select="@text:style-name"/>
534
    </xsl:variable>
535
    <xsl:choose>
536
      <xsl:when test="$Style='Emphasis'">
537
        <emph>
538
          <xsl:apply-templates/>
539
        </emph>
540
      </xsl:when>
541
      <xsl:when test="$Style='Underline'">
542
        <hi rend="ul">
543
          <xsl:apply-templates/>
544
        </hi>
545
      </xsl:when>
546
      <xsl:when test="$Style='SmallCaps'">
547
        <hi rend="sc">
548
          <xsl:apply-templates/>
549
        </hi>
550
      </xsl:when>
551
      <xsl:when test="$Style='Emphasis Bold'">
552
        <hi rend="bold">
553
          <xsl:apply-templates/>
554
        </hi>
555
      </xsl:when>
556
      <xsl:when test="$Style='Highlight'">
557
        <hi>
558
          <xsl:apply-templates/>
559
        </hi>
560
      </xsl:when>
561
      <xsl:when test="$Style='q'">
562
        <q>
563
          <xsl:choose>
564
            <xsl:when test="starts-with(.,'&#x2018;')">
565
              <xsl:value-of
566
                select="substring-before(substring-after(.,'&#x2018;'),'&#x2019;')"
567
              />
568
            </xsl:when>
569
            <xsl:otherwise>
570
              <xsl:apply-templates/>
571
            </xsl:otherwise>
572
          </xsl:choose>
573
        </q>
574
      </xsl:when>
575
      <xsl:when test="$Style='date'">
576
        <date>
577
          <xsl:apply-templates/>
578
        </date>
579
      </xsl:when>
580
      <xsl:when test="$Style='l'">
581
        <l>
582
          <xsl:apply-templates/>
583
        </l>
584
      </xsl:when>
585
      <xsl:when test="$Style='Filespec'">
586
        <Filespec>
587
          <xsl:apply-templates/>
588
        </Filespec>
589
      </xsl:when>
590
      <xsl:when test="$Style='gi'">
591
        <gi>
592
          <xsl:apply-templates/>
593
        </gi>
594
      </xsl:when>
595
      <xsl:when test="$Style='Code'">
596
        <Code>
597
          <xsl:apply-templates/>
598
        </Code>
599
      </xsl:when>
600
      <xsl:when test="$Style='Input'">
601
        <Input>
602
          <xsl:apply-templates/>
603
        </Input>
604
      </xsl:when>
605
      <xsl:when test="$Style='Internet Link'">
606
        <xsl:apply-templates/>
607
      </xsl:when>
608
      <xsl:when test="$Style='SubScript'">
609
	<hi rend="sub">
610
	  <xsl:apply-templates/>
611
        </hi>
612
      </xsl:when>
613
      <xsl:when test="$Style='SuperScript'">
614
        <hi rend="sup">
615
	  <xsl:apply-templates/>
616
        </hi>
617
      </xsl:when>
618
      <xsl:when test="style:text-properties[@fo:text-transform='small-caps']">
619
	<hi rend="sc">
620
	  <xsl:apply-templates/>
621
	</hi>
622
      </xsl:when>
623
      <xsl:when test="../text:h">
624
        <xsl:apply-templates/>
625
      </xsl:when>
626
      <xsl:otherwise>
627
        <xsl:call-template name="applyStyle"/>
628
      </xsl:otherwise>
629
    </xsl:choose>
630
  </xsl:template>
631

    
632
  <xsl:template name="applyStyle">
633
    <xsl:variable name="name">
634
      <xsl:value-of select="@text:style-name"/>
635
    </xsl:variable>
636

    
637
    <xsl:choose>
638
      <xsl:when test="string-length(.)=0"/>
639
      <xsl:when test="text:note">
640
	<xsl:apply-templates/>
641
      </xsl:when>
642
      <xsl:when test="key('STYLES',$name)">
643
        <xsl:variable name="contents">
644
          <xsl:apply-templates/>
645
        </xsl:variable>
646
        <xsl:for-each select="key('STYLES',$name)">
647

    
648
	  <!--! <xsl:for-each select="style:text-properties/@*">
649
	      <xsl:value-of select="name(.)"/>:        <xsl:value-of select="."/>&#10;
650
	      </xsl:for-each>
651
	  -->
652
	  <xsl:variable name="divrendstring">
653
	    <xsl:if
654
		test="style:paragraph-properties/@fo:text-align">
655
	      <xsl:value-of
656
		  select="style:paragraph-properties/@fo:text-align"/>
657
	      <xsl:text> </xsl:text>
658
	    </xsl:if>
659
	  </xsl:variable>
660

    
661
	  <xsl:variable name="rendstring">
662
	    <xsl:if
663
		test="style:text-properties[starts-with(@style:text-position,'super')]">
664
	      <xsl:text>sup </xsl:text>
665
	    </xsl:if>
666
	    
667
	    <xsl:if
668
		test="style:text-properties[starts-with(@style:text-position,'sub')]">
669
	      <xsl:text>sub </xsl:text>
670
	    </xsl:if>
671
	    
672
	    <xsl:if test="style:text-properties[@fo:font-weight='bold']">
673
	      <xsl:text>bold </xsl:text>
674
	    </xsl:if>
675
	    
676
	    <xsl:if
677
		test="style:text-properties[@style:text-underline-type='double']">
678
	      <xsl:text>underdoubleline </xsl:text>
679
	    </xsl:if>
680
	    
681
	    <xsl:if
682
		test="style:text-properties[@style:text-underline-style='solid']">
683
	      <xsl:text>underline </xsl:text>
684
	    </xsl:if>
685
	    
686
	    <xsl:if
687
		test="style:text-properties[@style:text-line-through-style='solid']">
688
	      <xsl:text>strikethrough </xsl:text>
689
	    </xsl:if>
690
	    
691
	    <xsl:if
692
		test="style:text-properties[@fo:font-variant='small-caps']">
693
	      <xsl:text>smallcaps </xsl:text>
694
	    </xsl:if>
695
	    
696
	    <xsl:if test="style:text-properties[@fo:font-style='italic']">
697
	      <xsl:text>italic </xsl:text>
698
	    </xsl:if>
699
	  </xsl:variable>
700

    
701
	  <!--<xsl:message>so: <xsl:value-of select="$divrendstring"/></xsl:message>
702
	  <xsl:if test="not($divrendstring='')">
703
	      <xsl:attribute name="rend" select="normalize-space($divrendstring)"/>
704
	  </xsl:if>-->
705

    
706
	  <xsl:choose>
707
	    <xsl:when test="$rendstring=''">
708
	      <xsl:copy-of select="$contents"/>
709
	    </xsl:when>
710
	    <xsl:otherwise>
711
	      <hi rend="{normalize-space($rendstring)}">
712
		<xsl:copy-of select="$contents"/>
713
	      </hi>
714
	    </xsl:otherwise>
715
	  </xsl:choose>
716
	  
717
        </xsl:for-each>
718
      </xsl:when>
719
      <xsl:otherwise>
720
        <xsl:apply-templates/>
721
      </xsl:otherwise>
722
    </xsl:choose>
723
  </xsl:template>
724

    
725
  <!-- tables -->
726
  <xsl:template match="table:table">
727
    <table rend="frame">
728
      <xsl:if test="@table:name and not(@table:name = 'local-table')">
729
        <xsl:attribute name="xml:id">
730
          <xsl:value-of select="@table:name"/>
731
        </xsl:attribute>
732
      </xsl:if>
733
      <xsl:if test="following-sibling::text:p[@text:style-name='Table']">
734
        <head>
735
          <xsl:value-of
736
            select="following-sibling::text:p[@text:style-name='Table']"/>
737
        </head>
738
      </xsl:if>
739
      <xsl:call-template name="generictable"/>
740
    </table>
741

    
742
  </xsl:template>
743

    
744

    
745
  <xsl:template name="generictable">
746
    <xsl:variable name="cells" select="count(descendant::table:table-cell)"/>
747
    <xsl:variable name="rows">
748
      <xsl:value-of select="count(descendant::table:table-row) "/>
749
    </xsl:variable>
750
    <xsl:variable name="cols">
751
      <xsl:value-of select="$cells div $rows"/>
752
    </xsl:variable>
753
    <xsl:variable name="numcols">
754
      <xsl:choose>
755
        <xsl:when
756
          test="child::table:table-column/@table:number-columns-repeated">
757
          <xsl:value-of
758
            select="number(table:table-column/@table:number-columns-repeated+1)"
759
          />
760
        </xsl:when>
761
        <xsl:otherwise>
762
          <xsl:value-of select="$cols"/>
763
        </xsl:otherwise>
764
      </xsl:choose>
765
    </xsl:variable>
766

    
767
    <xsl:apply-templates/>
768
  </xsl:template>
769

    
770
  <xsl:template name="colspec">
771
    <xsl:param name="left"/>
772
    <xsl:if
773
      test="number($left &lt; ( table:table-column/@table:number-columns-repeated +2)  )">
774
      <colspec>
775
        <xsl:attribute name="colnum">
776
          <xsl:value-of select="$left"/>
777
        </xsl:attribute>
778
        <xsl:attribute name="colname">
779
          <xsl:text>c</xsl:text>
780
          <xsl:value-of select="$left"/>
781
        </xsl:attribute>
782
      </colspec>
783
      <xsl:call-template name="colspec">
784
        <xsl:with-param name="left" select="$left+1"/>
785
      </xsl:call-template>
786
    </xsl:if>
787
  </xsl:template>
788

    
789
  <xsl:template match="table:table-column">
790
    <xsl:apply-templates/>
791
  </xsl:template>
792

    
793
  <xsl:template match="table:table-header-rows">
794
    <xsl:apply-templates/>
795
  </xsl:template>
796

    
797
  <xsl:template match="table:table-header-rows/table:table-row">
798
    <row role="label">
799
      <xsl:apply-templates/>
800
    </row>
801
  </xsl:template>
802

    
803
  <xsl:template match="table:table/table:table-row">
804
    <row>
805
      <xsl:apply-templates/>
806
    </row>
807
  </xsl:template>
808

    
809
  <xsl:template match="table:table-cell/text:h">
810
    <xsl:apply-templates/>
811
  </xsl:template>
812

    
813

    
814
  <xsl:template match="table:table-cell">
815
    <cell>
816
      <xsl:if test="@table:number-columns-spanned &gt;'1'">
817
        <xsl:attribute name="cols">
818
          <xsl:value-of select="@table:number-columns-spanned"/>
819
        </xsl:attribute>
820
      </xsl:if>
821
      <xsl:if test="@table:number-rows-spanned &gt;'1'">
822
        <xsl:attribute name="rows">
823
          <xsl:value-of select="@table:number-rows-spanned"/>
824
        </xsl:attribute>
825
      </xsl:if>
826
      <xsl:if test="text:h">
827
        <xsl:attribute name="role">label</xsl:attribute>
828
      </xsl:if>
829
      <xsl:apply-templates/>
830
    </cell>
831
  </xsl:template>
832

    
833

    
834
  <!-- notes -->
835
  <xsl:template match="text:note-citation"/>
836

    
837
  <xsl:template match="text:note-body">
838
    <xsl:apply-templates/>
839
  </xsl:template>
840

    
841
  <xsl:template match="text:note-ref">
842
    <ref target="#{@text:ref-name}">
843
      <xsl:apply-templates/>
844
    </ref>
845
  </xsl:template>
846

    
847
  <xsl:template match="text:note">
848
    <note>
849
      <xsl:if test="@text:id">
850
	<xsl:attribute name="xml:id">
851
	  <xsl:value-of select="@text:id"/>
852
	</xsl:attribute>
853
      </xsl:if>
854
      <xsl:choose>
855
        <xsl:when test="@text:note-class='endnote'">
856
          <xsl:attribute name="place">end</xsl:attribute>
857
        </xsl:when>
858
        <xsl:when test="@text:note-class='footnote'">
859
          <xsl:attribute name="place">foot</xsl:attribute>
860
        </xsl:when>
861
      </xsl:choose>
862
      <xsl:if test="text:note-citation">
863
	<xsl:attribute name="n">
864
	  <xsl:value-of select="text:note-citation"/>
865
	</xsl:attribute>
866
      </xsl:if>
867
      <xsl:apply-templates/>
868
    </note>
869
  </xsl:template>
870

    
871
  <!-- drawing -->
872
  <xsl:template match="draw:plugin">
873
    <ptr target="{@xlink:href}"/>
874
  </xsl:template>
875

    
876
  <xsl:template match="draw:text-box">
877
    <xsl:apply-templates/>
878
  </xsl:template>
879

    
880
  <xsl:template match="draw:frame">
881
    <xsl:choose>
882
      <xsl:when test="ancestor::draw:frame">
883
	<xsl:apply-templates/>
884
      </xsl:when>
885
      <xsl:otherwise>
886
	<figure>
887
	  <xsl:apply-templates/>
888
	</figure>
889
      </xsl:otherwise>
890
    </xsl:choose>
891
  </xsl:template>
892

    
893
  <xsl:template match="draw:image">
894
    <xsl:choose>
895
      <xsl:when test="ancestor::draw:text-box">
896
	<xsl:call-template name="findGraphic"/>
897
      </xsl:when>
898
      <xsl:when test="ancestor::draw:frame">
899
	<xsl:call-template name="findGraphic"/>
900
      </xsl:when>
901
      <xsl:when test="parent::text:p[@text:style-name='Mediaobject']">
902
        <figure>
903
          <xsl:call-template name="findGraphic"/>
904
          <head>
905
            <xsl:value-of select="."/>
906
          </head>
907
        </figure>
908
      </xsl:when>
909
      <xsl:otherwise>
910
        <figure>
911
          <xsl:call-template name="findGraphic"/>
912
        </figure>
913
      </xsl:otherwise>
914
    </xsl:choose>
915
  </xsl:template>
916

    
917
  <xsl:template name="findGraphic">
918
    <xsl:choose>
919
      <xsl:when test="office:binary-data">
920
	<xsl:apply-templates/>
921
      </xsl:when>
922
      <xsl:when test="@xlink:href">
923
        <graphic>
924
          <xsl:attribute name="url">
925
            <xsl:value-of select="@xlink:href"/>
926
          </xsl:attribute>
927
        </graphic>
928
      </xsl:when>
929
    </xsl:choose>
930
  </xsl:template>
931

    
932
  <xsl:template match="office:binary-data">    
933
    <binaryObject mimeType="image/jpg">
934
      <xsl:value-of select="."/>
935
    </binaryObject>
936
  </xsl:template>
937

    
938

    
939
  <!-- linking -->
940
  <xsl:template match="text:a">
941
    <xsl:choose>
942
      <xsl:when test="starts-with(@xlink:href,'mailto:')">
943
        <xsl:choose>
944
          <xsl:when test=".=@xlink:href">
945
            <ptr target="{substring-after(@xlink:href,'mailto:')}"/>
946
          </xsl:when>
947
          <xsl:otherwise>
948
            <ref target="{@xlink:href}">
949
              <xsl:apply-templates/>
950
            </ref>
951
          </xsl:otherwise>
952
        </xsl:choose>
953
      </xsl:when>
954
      <xsl:when test="contains(@xlink:href,'://')">
955
        <xsl:choose>
956
          <xsl:when test=".=@xlink:href">
957
            <ptr target="{@xlink:href}"/>
958
          </xsl:when>
959
          <xsl:otherwise>
960
            <ref target="{@xlink:href}">
961
              <xsl:apply-templates/>
962
            </ref>
963
          </xsl:otherwise>
964
        </xsl:choose>
965
      </xsl:when>
966
      <xsl:when test="not(contains(@xlink:href,'#'))">
967
        <ref target="{@xlink:href}">
968
          <xsl:apply-templates/>
969
        </ref>
970
      </xsl:when>
971
      <xsl:otherwise>
972
        <xsl:variable name="linkvar" select="@xlink:href"/>
973
        <xsl:choose>
974
          <xsl:when test=".=$linkvar">
975
            <ptr target="{$linkvar}"/>
976
          </xsl:when>
977
          <xsl:otherwise>
978
            <ref target="{$linkvar}">
979
              <xsl:apply-templates/>
980
            </ref>
981
          </xsl:otherwise>
982
        </xsl:choose>
983
      </xsl:otherwise>
984
    </xsl:choose>
985
  </xsl:template>
986

    
987
  <xsl:template match="text:line-break">
988
    <xsl:choose>
989
      <xsl:when test="parent::text:span[@text:style-name='l']"/>
990
      <xsl:when test="parent::text:p[@text:style-name='lg']"/>
991
      <xsl:otherwise>
992
	<lb/>
993
      </xsl:otherwise>
994
    </xsl:choose>
995
  </xsl:template>
996

    
997

    
998
  <xsl:template match="text:tab">
999
    <xsl:text>	</xsl:text>
1000
  </xsl:template>
1001

    
1002
  <xsl:template match="text:reference-ref">
1003
    <ptr target="#id_{@text:ref-name}"/>
1004
  </xsl:template>
1005

    
1006
  <xsl:template name="id.attribute.literal">
1007
    <xsl:if test="child::text:reference-mark-start">
1008
      <xsl:text> xml:id=&quot;</xsl:text>
1009
	<xsl:text>id_</xsl:text>
1010
        <xsl:value-of select="child::text:reference-mark-start/@text:style-name"
1011
        />
1012
	<xsl:text>&quot;</xsl:text>
1013
    </xsl:if>
1014
  </xsl:template>
1015

    
1016
  <xsl:template name="id.attribute">
1017
    <xsl:if test="child::text:reference-mark-start">
1018
      <xsl:attribute name="xml:id">
1019
	<xsl:text>id_</xsl:text>
1020
        <xsl:value-of select="child::text:reference-mark-start/@text:style-name"
1021
        />
1022
      </xsl:attribute>
1023
    </xsl:if>
1024
  </xsl:template>
1025

    
1026
  <xsl:template match="text:reference-mark-start"/>
1027

    
1028
  <xsl:template match="text:reference-mark-end"/>
1029

    
1030
  <xsl:template match="comment">
1031
    <xsl:comment>
1032
      <xsl:value-of select="."/>
1033
    </xsl:comment>
1034
  </xsl:template>
1035

    
1036
  <xsl:template match="text:user-index-mark">
1037
    <index indexName="{@text:index-name}">
1038
      <term>
1039
	<xsl:value-of select="@text:string-value"/>
1040
      </term>
1041
    </index>
1042
  </xsl:template>
1043

    
1044
  <xsl:template match="text:alphabetical-index-mark">
1045
    <xsl:if test="not(normalize-space(@text:string-value)='')">
1046
    <index>
1047
      <xsl:if test="@text:id">
1048
	<xsl:attribute name="xml:id">
1049
	  <xsl:value-of select="@text:id"/>
1050
	</xsl:attribute>
1051
      </xsl:if>
1052
      <xsl:choose>
1053
	<xsl:when test="@text:key1">
1054
	  <term>
1055
	    <xsl:value-of select="@text:key1"/>
1056
	  </term>
1057
	  <index>
1058
	    <term>
1059
	      <xsl:value-of select="@text:string-value"/>
1060
	    </term>
1061
	  </index>
1062
	</xsl:when>
1063
	<xsl:otherwise>
1064
	  <term>
1065
	    <xsl:value-of select="@text:string-value"/>
1066
	  </term>
1067
	</xsl:otherwise>
1068
      </xsl:choose>
1069
    </index>
1070
    </xsl:if>
1071
  </xsl:template>
1072

    
1073
  <xsl:template match="text:alphabetical-index">
1074
    <index>
1075
      <xsl:apply-templates select="text:index-body"/>
1076
    </index>
1077
  </xsl:template>
1078

    
1079
  <xsl:template match="text:index-body">
1080
    <xsl:for-each select="text:p[@text:style-name = 'Index 1']">
1081
      <index>
1082
        <term>
1083
          <xsl:value-of select="."/>
1084
        </term>
1085
        <xsl:if test="key('secondary_children', generate-id())">
1086
          <index>
1087
	    <term>
1088
            <xsl:value-of select="key('secondary_children', generate-id())"/>
1089
	    </term>
1090
	  </index>
1091
        </xsl:if>
1092
      </index>
1093
    </xsl:for-each>
1094
  </xsl:template>
1095

    
1096
  <xsl:template match="text:bookmark-ref">
1097
    <ref target="#id_{@text:ref-name}" type="{@text:reference-format}">
1098
      <xsl:apply-templates/>
1099
    </ref>
1100
  </xsl:template>
1101

    
1102
  <xsl:template match="text:bookmark-start">
1103
    <anchor type="bookmark-start">
1104
      <xsl:attribute name="xml:id">
1105
	<xsl:text>id_</xsl:text>
1106
	<xsl:value-of select="@text:name"/>
1107
      </xsl:attribute>
1108
    </anchor>
1109
  </xsl:template>
1110

    
1111
  <xsl:template match="text:bookmark-end">
1112
    <anchor type="bookmark-end">
1113
      <xsl:attribute name="corresp">
1114
	<xsl:text>#id_</xsl:text>
1115
	<xsl:value-of select="@text:name"/>
1116
      </xsl:attribute>
1117
    </anchor>
1118
  </xsl:template>
1119

    
1120
  <xsl:template match="text:bookmark">
1121
    <anchor>
1122
      <xsl:attribute name="xml:id">
1123
	<xsl:text>id_</xsl:text>
1124
	<xsl:value-of select="@text:name"/>
1125
      </xsl:attribute>
1126
    </anchor>
1127
  </xsl:template>
1128
  <!--
1129
These seem to have no obvious translation
1130
-->
1131

    
1132
  <xsl:template match="text:endnotes-configuration"/>
1133

    
1134
  <xsl:template match="text:file-name"/>
1135

    
1136
  <xsl:template match="text:footnotes-configuration"/>
1137

    
1138
  <xsl:template match="text:linenumbering-configuration"/>
1139

    
1140
  <xsl:template match="text:list-level-style-bullet"/>
1141

    
1142
  <xsl:template match="text:list-level-style-number"/>
1143

    
1144
  <xsl:template match="text:list-style"/>
1145

    
1146
  <xsl:template match="text:outline-level-style"/>
1147

    
1148
  <xsl:template match="text:outline-style"/>
1149

    
1150
  <xsl:template match="text:s"/>
1151

    
1152
  <xsl:template match="text:editing-cycles"><xsl:value-of select="."/></xsl:template>
1153
  <xsl:template match="text:page-count"><xsl:value-of select="."/></xsl:template>
1154
  <xsl:template match="text:word-count"><xsl:value-of select="."/></xsl:template>
1155
  <xsl:template match="text:image-count"><xsl:value-of select="."/></xsl:template>
1156
  <xsl:template match="text:table-count"><xsl:value-of select="."/></xsl:template>
1157
  <xsl:template match="text:modification-date"><xsl:value-of select="."/></xsl:template>
1158
  <xsl:template match="text:modification-time"><xsl:value-of select="."/></xsl:template>
1159
  <xsl:template match="text:sequence-ref"><xsl:value-of select="."/></xsl:template>
1160

    
1161
  <xsl:template match="text:*"> [[[UNTRANSLATED <xsl:value-of select="name(.)"/>: <xsl:apply-templates/>]]] </xsl:template>
1162

    
1163

    
1164
  <!-- sections of the OO format we don't need at present -->
1165

    
1166
  <xsl:template match="office:automatic-styles"/>
1167

    
1168
  <xsl:template match="office:font-decls"/>
1169

    
1170
  <xsl:template match="office:meta"/>
1171

    
1172
  <xsl:template match="office:script"/>
1173

    
1174
  <xsl:template match="office:settings"/>
1175

    
1176
  <xsl:template match="office:styles"/>
1177

    
1178
  <xsl:template match="style:*"/>
1179

    
1180

    
1181
  <xsl:template match="dc:*">
1182
    <xsl:apply-templates/>
1183
  </xsl:template>
1184

    
1185
  <xsl:template match="meta:creation-date">
1186
    <xsl:apply-templates/>
1187
  </xsl:template>
1188

    
1189
  <xsl:template match="meta:editing-cycles"/>
1190

    
1191
  <xsl:template match="meta:editing-duration"/>
1192

    
1193
  <xsl:template match="meta:generator"/>
1194

    
1195
  <xsl:template match="meta:user-defined"/>
1196

    
1197
  <!--
1198
<xsl:template match="text()">
1199
  <xsl:apply-templates select="normalize-space(.)"/>
1200
</xsl:template>
1201
-->
1202

    
1203
<xsl:template match="text:section">
1204
  <xsl:apply-templates/>
1205
</xsl:template>
1206

    
1207
<xsl:template match="text:sequence-decl">
1208
  <xsl:apply-templates/>
1209
</xsl:template>
1210

    
1211
<xsl:template match="text:sequence-decls">
1212
  <xsl:apply-templates/>
1213
</xsl:template>
1214

    
1215

    
1216
<xsl:template match="text:sequence">
1217
  <xsl:apply-templates/>
1218
</xsl:template>
1219

    
1220

    
1221
<xsl:template match="text:section-source"/>
1222

    
1223

    
1224
<xsl:template match="text:soft-page-break">
1225
	<pb/>
1226
</xsl:template>
1227

    
1228
<xsl:template name="stars">
1229
   <xsl:param name="n"/>
1230
   <xsl:if test="$n &gt;0">
1231
     <xsl:text>*</xsl:text>
1232
     <xsl:call-template name="stars">
1233
       <xsl:with-param name="n">
1234
	 <xsl:value-of select="$n - 1"/>
1235
       </xsl:with-param>
1236
     </xsl:call-template>
1237
   </xsl:if>
1238
</xsl:template>
1239

    
1240
<xsl:template match="text:change|text:changed-region|text:change-end|text:change-start">
1241
  <xsl:apply-templates/>
1242
</xsl:template>
1243

    
1244
<xsl:template match="text:table-of-content"/>
1245
<xsl:template match="text:index-entry-chapter"/>
1246
<xsl:template match="text:index-entry-page-number"/>
1247
<xsl:template match="text:index-entry-tab-stop"/>
1248
<xsl:template match="text:index-entry-text"/>
1249
<xsl:template match="text:index-title-template"/>
1250
<xsl:template match="text:table-of-content-entry-template"/>
1251
<xsl:template match="text:table-of-content-source"/>
1252

    
1253
  <xsl:template match="office:text">
1254
    <body>
1255
      <xsl:variable name="Body">
1256
	<HEAD level="1" magic="true">Start</HEAD>
1257
        <xsl:apply-templates/>
1258
      </xsl:variable>
1259
      <!-- debug
1260
      <xsl:result-document href="/tmp/temp.xml">
1261
	<xsl:copy-of select="$Body"/>
1262
      </xsl:result-document>
1263
      -->
1264
      <xsl:variable name="Body2">
1265
	<xsl:for-each select="$Body">
1266
	  <xsl:apply-templates mode="pass1"/>
1267
	</xsl:for-each>
1268
      </xsl:variable>
1269
      <xsl:for-each select="$Body2">
1270
        <xsl:for-each-group select="tei:*" group-starting-with="tei:HEAD[@level='1']">
1271
          <xsl:choose>
1272
            <xsl:when test="self::tei:HEAD[@level='1']">
1273
	      <xsl:call-template name="group-by-section"/>
1274
            </xsl:when>
1275
            <xsl:otherwise>
1276
	      <xsl:call-template name="inSection"/>
1277
            </xsl:otherwise>
1278
          </xsl:choose>
1279
        </xsl:for-each-group>
1280
      </xsl:for-each>
1281
    </body>
1282
  </xsl:template>
1283

    
1284
  <xsl:template name="group-by-section">
1285
    <xsl:variable name="ThisHeader" select="number(@level)"/>
1286
    <xsl:variable name="NextHeader" select="number(@level)+1"/>
1287
    <xsl:choose>
1288
      <xsl:when test="@magic">
1289
	  <xsl:for-each-group select="current-group() except ."
1290
			      group-starting-with="tei:HEAD[number(@level)=$NextHeader]">
1291
	    <xsl:choose>
1292
	      <xsl:when test="self::tei:HEAD">
1293
		<xsl:call-template name="group-by-section"/>
1294
	      </xsl:when>
1295
	    <xsl:otherwise>
1296
	      <xsl:call-template name="inSection"/>
1297
	    </xsl:otherwise>
1298
	    </xsl:choose>
1299
	  </xsl:for-each-group>
1300
      </xsl:when>
1301
      <xsl:otherwise>
1302
	<div>
1303
	  <xsl:choose>
1304
	    <xsl:when test="starts-with(@style,'Heading')"/>
1305
	    <xsl:when test="@style">
1306
	      <xsl:attribute name="rend" select="@style"/>
1307
	    </xsl:when>
1308
	  </xsl:choose>
1309
	  <xsl:if test="not(@interpolated='true')">
1310
	    <head>
1311
	      <xsl:apply-templates mode="pass1"/>
1312
	    </head>
1313
	  </xsl:if>
1314
	  <xsl:for-each-group select="current-group() except ."
1315
			      group-starting-with="tei:HEAD[number(@level)=$NextHeader]">
1316
	    <xsl:choose>
1317
	      <xsl:when test="self::tei:HEAD">
1318
		<xsl:call-template name="group-by-section"/>
1319
	      </xsl:when>
1320
	    <xsl:otherwise>
1321
		<xsl:call-template name="inSection"/>
1322
	    </xsl:otherwise>
1323
	    </xsl:choose>
1324
	  </xsl:for-each-group>
1325
	</div>
1326
      </xsl:otherwise>
1327
    </xsl:choose>
1328
  </xsl:template>
1329

    
1330

    
1331
  <xsl:template name="inSection">
1332
    <xsl:for-each-group select="current-group()"
1333
			group-adjacent="if (self::tei:GLOSS)
1334
					then 1
1335
					else 2">      
1336
      <xsl:choose>
1337
	<xsl:when test="current-grouping-key()=1">
1338
	  <list type="gloss">
1339
	    <xsl:for-each select="current-group()">
1340
	      <xsl:element name="{@n}">
1341
		<xsl:apply-templates mode="pass2"/>
1342
	      </xsl:element>
1343
	    </xsl:for-each>
1344
	  </list>
1345
	</xsl:when>
1346
	<xsl:otherwise>
1347
	  <xsl:for-each select="current-group()">
1348
	    <xsl:apply-templates select="." mode="pass2"/>
1349
	  </xsl:for-each>
1350
	</xsl:otherwise>
1351
      </xsl:choose>
1352
    </xsl:for-each-group>
1353
  </xsl:template>
1354
		
1355
<xsl:template match="@*|text()|comment()|processing-instruction()" mode="pass1">
1356
    <xsl:copy-of select="."/>
1357
  </xsl:template>
1358

    
1359
  <xsl:template match="tei:p[not(node())]" mode="pass1"/>
1360

    
1361
  <xsl:template match="tei:HEAD" mode="pass1">
1362
    <xsl:if test="preceding-sibling::tei:HEAD">
1363
      <xsl:variable name="prev"
1364
		    select="xs:integer(number(preceding-sibling::tei:HEAD[1]/@level))"/>
1365
      <xsl:variable name="current"
1366
		    select="xs:integer(number(@level))"/>
1367
	<xsl:if test="($current - $prev) &gt;1 ">
1368
	  <!--<xsl:message>Walk from <xsl:value-of select="$prev"/> to <xsl:value-of select="$current"/></xsl:message>-->
1369
	  <xsl:for-each
1370
	      select="$prev + 1   to $current - 1 ">
1371
	    <HEAD interpolated='true' level="{.}"/>
1372
	  </xsl:for-each>
1373
	</xsl:if>
1374
    </xsl:if>
1375
    <xsl:copy>
1376
    <xsl:apply-templates
1377
	select="*|@*|processing-instruction()|comment()|text()"
1378
	mode="pass1"/>
1379
    </xsl:copy>
1380
  </xsl:template>
1381

    
1382
  <xsl:template match="*" mode="pass1">
1383
    <xsl:copy>
1384
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass1"/>
1385
    </xsl:copy>
1386
  </xsl:template>
1387

    
1388

    
1389
  <!-- second pass -->
1390

    
1391

    
1392
  <xsl:template match="tei:p[not(*) and normalize-space(.)='']" mode="pass2"/>
1393

    
1394
    <xsl:template match="@*|comment()|processing-instruction()" mode="pass2">
1395
        <xsl:copy-of select="."/>
1396
    </xsl:template>
1397
    
1398
    <xsl:template match="*" mode="pass2">
1399
        <xsl:copy>
1400
            <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass2"/>
1401
        </xsl:copy>
1402
    </xsl:template>
1403
    
1404
    
1405
    <xsl:template match="text()" mode="pass2">
1406
        <xsl:value-of select="."/>
1407
    </xsl:template>
1408

    
1409

    
1410
    <xsl:template match="tei:title" mode="pass2">
1411
      <xsl:choose>
1412
	<xsl:when test="parent::tei:div|parent::tei:body">
1413
	  <head>
1414
            <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass2"/>
1415
	  </head>
1416
	</xsl:when>
1417
	<xsl:otherwise>
1418
	  <xsl:copy>
1419
	    <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass2"/>
1420
	  </xsl:copy>
1421
	</xsl:otherwise>
1422
      </xsl:choose>
1423
    </xsl:template>
1424

    
1425
  <!-- third pass -->
1426

    
1427

    
1428

    
1429
    <xsl:template match="@*|comment()|processing-instruction()" mode="pass3">
1430
        <xsl:copy-of select="."/>
1431
    </xsl:template>
1432
    
1433
    <xsl:template match="*" mode="pass3">
1434
        <xsl:copy>
1435
            <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass3"/>
1436
        </xsl:copy>
1437
    </xsl:template>
1438
    
1439
    
1440
    <xsl:template match="text()" mode="pass3">
1441
        <xsl:value-of select="."/>
1442
    </xsl:template>
1443

    
1444
    <xsl:template match="tei:div[not(@type)]" mode="pass3">
1445
      <div type="div{count(ancestor-or-self::tei:div)}">
1446
	<xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass3"/>
1447
      </div>
1448
    </xsl:template>
1449

    
1450
</xsl:stylesheet>