-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdocxtotei.xsl
514 lines (446 loc) · 18.9 KB
/
docxtotei.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:prop="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:iso="http://www.iso.org/ns/1.0"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main"
xmlns:mv="urn:schemas-microsoft-com:mac:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
xmlns:tbx="http://www.lisa.org/TBX-Specification.33.0.html"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:teidocx="http://www.tei-c.org/ns/teidocx/1.0"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns="http://www.tei-c.org/ns/1.0"
version="2.0"
exclude-result-prefixes="a cp dc dcterms dcmitype prop
iso m mml mo mv o pic r rel html tbx tei teidocx v xs ve w10 w wne wp">
<xsl:import href="../../common2/functions.xsl"/>
<xsl:import href="../utils/maths/omml2mml.xsl"/>
<xsl:import href="../utils/functions.xsl"/>
<xsl:import href="../utils/variables.xsl"/>
<xsl:import href="../utils/identity/identity.xsl"/>
<xsl:param name="convert-graphics">true</xsl:param>
<xsl:param name="convert-headers">true</xsl:param>
<xsl:param name="mathMethod">mml</xsl:param>
<xsl:param name="termMethod">tei</xsl:param>
<xsl:param name="tableMethod">tei</xsl:param>
<xsl:param name="preserveWordSections">false</xsl:param>
<xsl:param name="preserveWordHeadersFooters">false</xsl:param>
<xsl:param name="preserveEffects">false</xsl:param>
<xsl:param name="verbose">false</xsl:param>
<xsl:param name="processChangeInformation">false</xsl:param>
<xsl:param name="pageHeight">890</xsl:param>
<xsl:param name="pageWidth">576</xsl:param>
<xsl:include href="pass0.xsl"/>
<xsl:include href="pass2.xsl"/>
<xsl:include href="fields.xsl"/>
<xsl:include href="toc.xsl"/>
<xsl:include href="graphics.xsl"/>
<xsl:include href="lists.xsl"/>
<xsl:include href="marginals.xsl"/>
<xsl:include href="maths.xsl"/>
<xsl:include href="paragraphs.xsl"/>
<xsl:include href="tables.xsl"/>
<xsl:include href="tei-templates.xsl"/>
<xsl:include href="textruns.xsl"/>
<xsl:include href="utility-templates.xsl"/>
<xsl:include href="wordsections.xsl"/>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
<desc>
<p> TEI stylesheet for converting Word docx files to TEI </p>
<p>This software is dual-licensed:
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
Unported License http://creativecommons.org/licenses/by-sa/3.0/
2. http://www.opensource.org/licenses/BSD-2-Clause
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This software is provided by the copyright holders and contributors
"as is" and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability and fitness for
a particular purpose are disclaimed. In no event shall the copyright
holder or contributors be liable for any direct, indirect, incidental,
special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use,
data, or profits; or business interruption) however caused and on any
theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage.
</p>
<p>Author: See AUTHORS</p>
<p>Id: $Id: docxtotei.xsl 11487 2013-01-20 15:46:41Z rahtz $</p>
<p>Copyright: 2008, TEI Consortium</p>
<p>Modifications copyright Martin Paul Eve (https://www.martineve.com) 2013</p>
</desc>
</doc>
<xsl:variable name="processor">
<xsl:value-of select="system-property('xsl:vendor')"/>
</xsl:variable>
<xsl:variable name="digits">1234567890</xsl:variable>
<xsl:variable name="characters">~!@#$%^&*()<>{}[]|:;,.?`'"=+-_</xsl:variable>
<xsl:variable name="wordDirectory">
<xsl:value-of
select="translate($word-directory,'\\','/')"/>
</xsl:variable>
<xsl:variable name="relsFile" select="concat($wordDirectory,'/word/_rels/document.xml.rels')"/>
<xsl:variable name="numberFile" select="concat($wordDirectory,'/word/numbering.xml')"/>
<xsl:variable name="styleFile" select="concat($wordDirectory,'/word/styles.xml')"/>
<xsl:variable name="docProps" select="doc(concat($wordDirectory,'/docProps/core.xml'))"/>
<xsl:variable name="customFile" select="concat($wordDirectory,'/docProps/custom.xml')"/>
<xsl:variable name="relsDoc" select="concat($wordDirectory,'/word/_rels/document.xml.rels')"/>
<xsl:variable name="styleDoc" select="concat($wordDirectory,'/word/styles.xml')"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="w:t"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
<p>The main template that starts the conversion from docx to TEI</p>
<p><b>IMPORTING STYLESHEETS AND OVERRIDING MATCHED TEMPLATES:</b></p>
<p>When importing a stylesheet (xsl:import) all the templates
in the imported stylesheet get a lower import-precedence than
the ones in the importing stylesheet. If the importing
stylesheet wants to override, let's say a general template to
match all <w:p> elements where no more specialized rule
applies it can't, since it will automatically override all
w:p[somepredicate] template in the imported stylesheet as
well. In this case we have outsourced the processing of the
general template into a named template and all the imported
stylesheet does is to call the named template. Now, the
importing stylesheet can simply override the named template,
and everything works out fine.</p>
<p>See templates: - w:p (mode: paragraph)</p>
<p>Modes:</p>
<ul>
<li>pass0: a normalization process for styles. Can also
detect illegal styles.</li>
<li>pass2: templates that apply in the second stage
of the conversion, cleaning TEI elements created in the
first ise."</li>
<li>inSectionGroup: defines a template that works on a
group of consecutive elements (w:p or w:tbl elements) that
form a section (a normal section, not to be confused with
w:sectPr).</li>
<li>paragraph: defines that the template
works on an individual element (usually
starting with a w:p element). </li>
<li>iden: simply copies the content</li>
</ul>
</desc>
</doc>
<xsl:template match="/">
<!-- Do an initial normalization and store everything in $pass0 -->
<xsl:if test="not(doc-available($relsFile))">
<xsl:message terminate="yes">The file <xsl:value-of
select="$relsFile"/> cannot be read</xsl:message>
</xsl:if>
<xsl:if test="not(doc-available($styleFile))">
<xsl:message terminate="yes">The file <xsl:value-of
select="$styleFile"/> cannot be read</xsl:message>
</xsl:if>
<xsl:variable name="pass0">
<xsl:apply-templates mode="pass0"/>
</xsl:variable>
<!-- Do the main transformation and store everything in the variable pass1 -->
<xsl:variable name="pass1">
<xsl:for-each select="$pass0">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:variable>
<!-- debug
<xsl:result-document href="/tmp/foo.xml">
<xsl:copy-of select="$pass0"/>
</xsl:result-document>
-->
<!-- Do the final parse and create valid TEI -->
<xsl:apply-templates select="$pass1" mode="pass2"/>
<xsl:call-template name="fromDocxFinalHook"/>
</xsl:template>
<xsl:template name="fromDocxFinalHook"/>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Main document template
</desc>
</doc>
<xsl:template match="w:document">
<TEI>
<!-- create teiHeader -->
<xsl:call-template name="create-tei-header"/>
<!-- convert main and back matter -->
<xsl:apply-templates select="w:body"/>
</TEI>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Create the basic text; worry later about dividing it up
</desc>
</doc>
<xsl:template match="w:body">
<text>
<!-- Create forme work -->
<xsl:call-template name="extract-forme-work"/>
<!-- create TEI body -->
<body>
<xsl:call-template name="mainProcess"/>
</body>
</text>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Process the text by high-level divisions
</desc>
</doc>
<xsl:template name="mainProcess">
<!--
group all paragraphs that form a first level section.
-->
<xsl:for-each-group select="w:sdt|w:p|w:tbl"
group-starting-with="w:p[tei:is-firstlevel-heading(.)]">
<xsl:choose>
<!-- We are dealing with a first level section, we now have
to further divide the section into subsections that we can then
finally work on -->
<xsl:when test="tei:is-heading(.)">
<xsl:call-template name="group-by-section"/>
</xsl:when>
<!-- We have found some loose paragraphs. These are most probably
front matter paragraps. We can simply convert them without further
trying to split them up into sub sections. -->
<xsl:otherwise>
<xsl:apply-templates select="." mode="inSectionGroup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
<!-- I have no idea why I need this, but I apparently do.
//TODO: find out what is going on-->
<xsl:apply-templates select="w:sectPr" mode="paragraph"/>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
<p>Bookmarks in section mode</p>
<p>
There are certain elements that we don't really care about, but that
force us to regroup everything from the next sibling on.
@see grouping in construction of headline outline.
</p>
</desc>
</doc>
<xsl:template match="w:bookmarkStart|w:bookmarkEnd"
mode="inSectionGroup">
<xsl:for-each-group select="current-group() except ." group-adjacent="1">
<xsl:apply-templates select="." mode="inSectionGroup"/>
</xsl:for-each-group>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
<p>Bookmarks in normal mode</p>
<p>Copy bookmarks for processing in pass 2</p>
</desc>
</doc>
<xsl:template match="w:bookmarkStart|w:bookmarkEnd" >
<xsl:choose>
<xsl:when test="starts-with(@w:name,'_Ref')">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="self::w:bookmarkEnd"/>
<xsl:otherwise>
<ANCHOR>
<xsl:attribute name="xml:id" select="substring(@w:name,2)"/>
</ANCHOR>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
<p>Grouping consecutive elements that belong together</p>
<p>
We are now working on a group of all elements inside some group bounded by
headings. These need to be further split up into smaller groups for figures,
list etc. and into individual groups for simple paragraphs...
</p>
</desc>
</doc>
<xsl:template match="w:tbl|w:p" mode="inSectionGroup">
<!--
We are looking for:
- Lists -> 1
- Table of Contents -> 2
- Figures -> 3
Anything else is assigned a number of position()+100. This should be
sufficient even if we find lots more things to group.
-->
<xsl:for-each-group
select="current-group()"
group-adjacent="if (tei:is-list(.)) then 1
else if (tei:is-toc(.)) then 2
else if (tei:is-figure(.)) then 3
else if (tei:is-line(.)) then 4
else position() + 100">
<!-- For each defined grouping call a specific template. If there is no
grouping defined, apply templates with mode
paragraph -->
<xsl:choose>
<xsl:when test="current-grouping-key()=1">
<xsl:call-template name="listSection"/>
</xsl:when>
<xsl:when test="current-grouping-key()=2">
<xsl:call-template name="tocSection"/>
</xsl:when>
<xsl:when test="current-grouping-key()=3">
<xsl:call-template name="figureSection"/>
</xsl:when>
<xsl:when test="current-grouping-key()=4">
<xsl:call-template name="lineSection"/>
</xsl:when>
<!-- it is not a defined grouping .. apply templates -->
<xsl:otherwise>
<xsl:apply-templates select="." mode="paragraph"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Creating a group of a figure
</desc>
</doc>
<xsl:template name="figureSection">
<figure>
<xsl:for-each select="current-group()">
<xsl:apply-templates select="." mode="paragraph"/>
</xsl:for-each>
</figure>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Creating a gloss list
</desc>
</doc>
<xsl:template name="glossListSection">
<list type="gloss">
<xsl:for-each select="current-group()">
<xsl:apply-templates select="." mode="paragraph"/>
</xsl:for-each>
</list>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Creating a group of a figure
</desc>
</doc>
<xsl:template name="lineSection">
<lg>
<xsl:for-each select="current-group()">
<xsl:apply-templates select="." mode="paragraph"/>
</xsl:for-each>
</lg>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
Groups the document by headings and thereby creating the document structure.
</desc>
</doc>
<xsl:template name="group-by-section">
<xsl:variable name="Style" select="w:pPr/w:pStyle/@w:val"/>
<xsl:variable name="NextHeader" select="tei:get-nextlevel-header($Style)"/>
<div>
<xsl:apply-templates select="." mode="inSectionGroup"/>
</div>
</xsl:template>
<doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
<desc>
<p>Looks through the document to find forme work related sections.</p>
<p>
Creates a <fw> element for each forme work related section. These include
running headers and footers. The corresponding elements in OOXML are w:headerReference
and w:footerReference. These elements only define a reference that to a header or
footer definition file. The reference itself is resolved in the file word/_rels/document.xml.rels.
</p>
</desc>
</doc>
<xsl:template name="extract-forme-work">
<xsl:if test="preserveWordHeadersFooters='true'">
<xsl:for-each-group select="//w:headerReference|//w:footerReference" group-by="@r:id">
<fw>
<xsl:attribute name="xml:id">
<xsl:value-of select="@r:id"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:choose>
<xsl:when test="self::w:headerReference">header</xsl:when>
<xsl:otherwise>footer</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:variable name="rid" select="@r:id"/>
<xsl:variable name="h-file">
<xsl:value-of select="document($relsDoc)//rel:Relationship[@Id=$rid]/@Target"/>
</xsl:variable>
<!-- for the moment, just copy content -->
<xsl:if test="doc-available(concat($wordDirectory,'/word/', $h-file))">
<xsl:for-each-group select="document(concat($wordDirectory,'/word/', $h-file))/*[1]/w:*"
group-adjacent="1">
<xsl:apply-templates select="." mode="inSectionGroup"/>
</xsl:for-each-group>
</xsl:if>
</fw>
</xsl:for-each-group>
</xsl:if>
</xsl:template>
<xsl:template match="w:hyperlink">
<ref>
<xsl:variable name="rid" select="@r:id"/>
<xsl:attribute name="target">
<xsl:choose>
<xsl:when test="ancestor::w:endnote">
<xsl:value-of
select="document(concat($wordDirectory,'/word/_rels/endnotes.xml.rels'))//rel:Relationship[@Id=$rid]/@Target"/>
</xsl:when>
<xsl:when test="ancestor::w:footnote">
<xsl:value-of
select="document(concat($wordDirectory,'/word/_rels/footnotes.xml.rels'))//rel:Relationship[@Id=$rid]/@Target"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="document($relsDoc)//rel:Relationship[@Id=$rid]/@Target"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@w:anchor">#<xsl:value-of select="@w:anchor" /></xsl:if>
</xsl:attribute>
<xsl:apply-templates/>
</ref>
</xsl:template>
<xsl:template match="w:instrText">
<xsl:choose>
<xsl:when test="contains(.,'REF _')"></xsl:when>
<xsl:when test="starts-with(.,'HYPERLINK')"></xsl:when>
<xsl:when test="starts-with(.,' XE')"></xsl:when>
<xsl:when test="starts-with(.,'XE')"></xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@rend[.='Body_Text']" mode="pass2"/>
<xsl:template match="@rend[.='Normal (Web)']" mode="pass2"/>
</xsl:stylesheet>