Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / tools / xhtml2tei.xsl @ 187

History | View | Annotate | Download (15.6 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
                xmlns:v="urn:schemas-microsoft-com:vml"
4
                xmlns:o="urn:schemas-microsoft-com:office:office"
5
                xmlns:w="urn:schemas-microsoft-com:office:word"
6
                xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
7
                xmlns:h="http://www.w3.org/1999/xhtml"
8
                exclude-result-prefixes="h v o w dt"
9
                version="1.0">
10

    
11
   <xsl:output method="xml" indent="yes"/>
12

    
13
   <xsl:param name="filename"/>
14

    
15
   <xsl:key name="H" match="h:h1" use="1"/>
16
   <xsl:key name="H" match="h:h2" use="1"/>
17
   <xsl:key name="H" match="h:h3" use="1"/>
18
   <xsl:key name="H" match="h:h4" use="1"/>
19

    
20
   <xsl:template match="h:html">
21
      <xsl:variable name="author">
22
         <xsl:choose>
23
            <xsl:when test="h:head/h:meta[@name='Author']">
24
               <xsl:value-of select="h:head/h:meta[@name='Author']/@content"/>
25
            </xsl:when>
26
            <xsl:when test="h:body/h:address">
27
               <xsl:variable name="add">
28
                  <xsl:value-of select="h:body/h:address"/>
29
               </xsl:variable>
30
               <xsl:choose>
31
                  <xsl:when test="contains($add,'Author: ')">
32
                     <xsl:value-of select="substring-after($add,'Author: ')"/>
33
                  </xsl:when>
34
                  <xsl:otherwise>
35
                     <xsl:value-of select="$add"/>
36
                  </xsl:otherwise>
37
               </xsl:choose>
38
            </xsl:when>
39
            <xsl:otherwise>OUCS</xsl:otherwise>
40
         </xsl:choose>
41
      </xsl:variable>
42
      <xsl:variable name="date">
43
         <xsl:choose>
44
            <xsl:when test="h:body/h:address">
45
               <xsl:variable name="add">
46
                  <xsl:value-of select="h:body/h:address"/>
47
               </xsl:variable>
48
               <xsl:choose>
49
                  <xsl:when test="contains($add,'Date: ')">
50
                     <xsl:value-of select="substring-after($add,'Date: ')"/>
51
                  </xsl:when>
52
                  <xsl:when test="contains($add,'Last updated: ')">
53
                     <xsl:value-of select="substring-after($add,'Last updated: ')"/>
54
                  </xsl:when>
55
                  <xsl:when test="contains($add,'Last Updated: ')">
56
                     <xsl:value-of select="substring-after($add,'Last Updated: ')"/>
57
                  </xsl:when>
58
                  <xsl:otherwise>
59
                     <xsl:value-of select="$add"/>
60
                  </xsl:otherwise>
61
               </xsl:choose>
62
            </xsl:when>
63
            <xsl:otherwise>OUCS</xsl:otherwise>
64
         </xsl:choose>
65
      </xsl:variable>
66
      <TEI.2>
67
         <teiHeader>
68
            <fileDesc>
69
               <titleStmt>
70
                  <title>
71
                     <xsl:apply-templates select="h:head/h:title"/>
72
                  </title>
73
                  <author>
74
                     <xsl:value-of select="$author"/>
75
                  </author>
76
               </titleStmt>
77
               <editionStmt>
78
                  <edition>
79
  	                  <date>
80
                        <xsl:value-of select="$date"/>
81
                     </date>
82
                  </edition>
83
               </editionStmt>
84
               <publicationStmt>
85
                  <authority>OUCS</authority>
86
	                 <address>
87
                     <email>advisory@oucs.ox.ac.uk</email>
88
                  </address>
89
               </publicationStmt>
90
               <sourceDesc>
91
                  <p>This is the master version of an original document,
92
created by converting an HTML file to TEI XML on TODAYSDATE.</p>
93
               </sourceDesc>
94
            </fileDesc>
95
            <revisionDesc>
96
               <change>
97
	                 <date>$Date: 2011-10-17 09:55:51 +0100 (Mon, 17 Oct 2011) $</date>
98
                  <respStmt>
99
                     <name>$Author: rahtz $</name>
100
                  </respStmt>
101
	                 <item>$Revision: 9513 $</item>
102
               </change>
103
            </revisionDesc>
104
         </teiHeader>
105
         <text>
106
            <xsl:apply-templates/>
107
         </text>
108
      </TEI.2>
109
      <xsl:text>&#10;</xsl:text>
110
   </xsl:template>
111

    
112
   <xsl:template match="h:head"/>
113

    
114
   <xsl:template match="h:body">
115
      <body>
116
         <xsl:apply-templates/>
117
         <!--This code is intended to balance the div tags created by headers with -->
118
  <!--     the correct number of /divs. At the moment it fails if there are -->
119
  <!-- Hn tags inside td's, which we are intending to make cell labels -->
120
   <xsl:variable name="starter">    
121
            <xsl:value-of select="substring(name(key('H',1)[1]),2)"/>
122
         </xsl:variable>
123
         <xsl:variable name="ender">
124
            <xsl:value-of select="substring(name(key('H',1)[last()]),2)"/>
125
         </xsl:variable>
126
         <xsl:variable name="difference">
127
            <xsl:value-of select="$starter - $ender"/>
128
         </xsl:variable>
129
         <xsl:comment>
130
     Started with a <xsl:value-of select="$starter"/>, ended with a <xsl:value-of select="$ender"/>
131
   diff=<xsl:value-of select="$difference"/>
132
         </xsl:comment>
133
         <!-- test call of for loop to handle closure of divs
134
got to find a way to set a trigger for closing div if no headers -->
135
 <xsl:call-template name="for-loop">
136
            <xsl:with-param name="i" select="$ender"/>
137
            <xsl:with-param name="stepsize" select="1"/>
138
            <xsl:with-param name="until" select="$starter"/>
139
            <xsl:with-param name="insert" select="'/div'"/>
140
         </xsl:call-template> 
141
         <!-- old div closing code was here-->
142

    
143
  </body>
144
   </xsl:template>
145

    
146

    
147
   <xsl:template match="h:h1|h:h2|h:h3|h:h4">
148
  <!--headers inside td's are labels-->
149
  <xsl:choose>
150
         <xsl:when test="parent::h:td">
151
            <emph>
152
               <xsl:value-of select="."/>
153
            </emph>
154
         </xsl:when>
155
         <xsl:otherwise>
156
            <xsl:if test="preceding::h:h1 or preceding::h:h2 or preceding::h:h3 or preceding::h:h4">
157
               <xsl:variable name="previous">
158
                  <xsl:value-of select="substring(name(preceding::*[name()='h1' or name()='h2' or name()='h3' or name()='h4'][1]),2)"/>
159
               </xsl:variable>
160
               <xsl:variable name="me">
161
                  <xsl:value-of select="substring(name(.),2)"/>
162
               </xsl:variable>
163
               <xsl:variable name="difference">
164
                  <xsl:value-of select="$me - $previous"/>
165
               </xsl:variable>
166
               <!--
167
   <xsl:message>
168
 I am a <xsl:value-of select="$me"/>, previous is <xsl:value-of select="$previous"/>, difference is <xsl:value-of select="$difference"/>
169

    
170
   </xsl:message>
171
-->
172
<!-- test call of for loop to handle this ... -->
173
<xsl:if test="$difference &lt; 1">
174
                  <xsl:call-template name="for-loop">
175
                     <xsl:with-param name="i" select="$previous"/>
176
                     <xsl:with-param name="stepsize" select="1"/>
177
                     <xsl:with-param name="until" select="$me"/>
178
                     <xsl:with-param name="insert" select="'/div'"/>
179
                  </xsl:call-template>
180
               </xsl:if>
181
               <xsl:if test="$difference &gt; 1">
182
                  <xsl:call-template name="for-loop">
183
                     <xsl:with-param name="i" select="$previous"/>
184
                     <xsl:with-param name="stepsize" select="1"/>
185
                     <xsl:with-param name="until" select="$me - 2"/>
186
                     <xsl:with-param name="insert" select="'div'"/>
187
                  </xsl:call-template> 
188
               </xsl:if>
189
            </xsl:if>
190
            <xsl:choose>
191
               <xsl:when test="h:a[@name]">
192
                  <xsl:text disable-output-escaping="yes">&lt;div id="</xsl:text>
193
                  <xsl:value-of select="h:a[@name]/@name"/>
194
                  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
195
               </xsl:when>
196
               <xsl:when test="preceding::h:a[1][@name]">
197
                  <xsl:text disable-output-escaping="yes">&lt;div id="</xsl:text>
198
                  <xsl:value-of select="preceding::h:a[1][@name]/@name"/>
199
                  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
200
               </xsl:when>   
201
               <xsl:otherwise>
202
                  <xsl:text disable-output-escaping="yes">&lt;div&gt;</xsl:text>
203
               </xsl:otherwise>
204
            </xsl:choose>
205
            <head>
206
               <xsl:apply-templates mode="head"/>
207
            </head>
208
         </xsl:otherwise>
209
      </xsl:choose>
210
   </xsl:template>
211

    
212
   <!-- low-level markup -->
213
<xsl:template match="h:td/h:p">
214
      <xsl:apply-templates/>
215
   </xsl:template>
216

    
217
   <xsl:template match="h:p">
218
      <p>
219
         <xsl:apply-templates/>
220
      </p>
221
   </xsl:template>
222

    
223
   <xsl:template match="h:strong|h:b|h:em|h:i" mode="head">
224
      <xsl:apply-templates/>
225
   </xsl:template>
226

    
227
   <xsl:template match="h:strong|h:b">
228
      <hi>
229
         <xsl:apply-templates/>
230
      </hi>
231
   </xsl:template>
232

    
233
   <xsl:template match="h:tt|h:var|h:kbd|h:samp|h:code">
234
      <code>
235
         <xsl:apply-templates/>
236
      </code>
237
   </xsl:template>
238

    
239
   <xsl:template match="h:em|h:i">
240
      <emph>
241
         <xsl:apply-templates/>
242
      </emph>
243
   </xsl:template>
244

    
245
   <xsl:template match="h:font"><!-- just continue -->
246
  <xsl:apply-templates/>
247
   </xsl:template>
248

    
249
   <xsl:template match="h:big|h:small|h:center"><!-- just continue -->
250
  <xsl:apply-templates/>
251
   </xsl:template>
252

    
253
   <xsl:template match="h:span"><!-- just continue -->
254
  <xsl:apply-templates/>
255
   </xsl:template>
256
   <xsl:template match="h:span[@class]"><!-- just continue -->
257
     <hi rend="{@class}">
258
       <xsl:apply-templates/>
259
     </hi>
260
   </xsl:template>
261
   <!-- links -->
262

    
263
<xsl:template match="h:a[@href]">
264
      <xsl:variable name="content" select="descendant-or-self::text()"/>
265
      <xsl:choose>
266
         <xsl:when test="@href = $content">
267
            <xsl:choose>
268
               <xsl:when test="starts-with(@href,'#')">
269
                  <ptr target="{substring(@href,2)}"/>
270
               </xsl:when>
271
               <xsl:otherwise>
272
                  <xptr url="{@href}"/>
273
               </xsl:otherwise>
274
            </xsl:choose>
275
         </xsl:when>
276
         <xsl:otherwise>
277
            <xsl:choose>
278
               <xsl:when test="starts-with(@href,'#')">
279
                  <ref target="{substring(@href,2)}">
280
                     <xsl:value-of select="$content"/>
281
                  </ref>
282
               </xsl:when>
283
               <xsl:otherwise>
284
                  <xref url="{@href}">
285
                     <xsl:value-of select="$content"/>
286
                  </xref>
287
               </xsl:otherwise>
288
            </xsl:choose>
289
         </xsl:otherwise>
290
      </xsl:choose>
291
   </xsl:template>
292

    
293
   <!-- ====== quotes, verbatim ====== -->
294
<xsl:template match="h:blockquote">
295
      <p>
296
         <q rend="display">
297
            <xsl:apply-templates/>
298
         </q>
299
      </p>
300
   </xsl:template>
301

    
302
   <xsl:template match="h:pre">
303
      <eg>
304
         <xsl:apply-templates/>
305
      </eg>
306
   </xsl:template>
307

    
308
   <!-- ============ Lists ============== -->
309
<xsl:template match="h:dl">
310
      <list type="gloss">
311
         <xsl:apply-templates/>
312
      </list>
313
   </xsl:template>
314

    
315
   <xsl:template match="h:dt">
316
      <label>
317
         <xsl:apply-templates/>
318
      </label>
319
   </xsl:template>
320

    
321
   <xsl:template match="h:dd">
322
      <item>
323
         <xsl:apply-templates/>
324
      </item>
325
   </xsl:template>
326

    
327
   <xsl:template match="h:ol">
328
      <list type="ordered">
329
         <xsl:apply-templates/>
330
      </list>
331
   </xsl:template>
332

    
333
   <xsl:template match="h:ul">
334
      <list type="unordered">
335
         <xsl:apply-templates/>
336
      </list>
337
   </xsl:template>
338

    
339
   <xsl:template match="h:li">
340
      <item>
341
         <xsl:apply-templates/>
342
      </item>
343
   </xsl:template>
344

    
345
   <!-- ===== sub, sup -->
346
<xsl:template match="h:sub">
347
      <hi rend="sub">
348
         <xsl:apply-templates/>
349
      </hi>
350
   </xsl:template>
351
   <xsl:template match="h:sup">
352
      <hi rend="sup">
353
         <xsl:apply-templates/>
354
      </hi>
355
   </xsl:template>
356

    
357

    
358

    
359
   <!-- tables -->
360
<xsl:template match="h:tr">
361
      <row>
362
         <xsl:apply-templates/>
363
      </row>
364
   </xsl:template>
365

    
366
   <xsl:template match="h:p/h:table[@border]">
367
      <table border="{@border}">
368
         <xsl:apply-templates/>
369
      </table>
370
   </xsl:template>
371

    
372
   <xsl:template match="h:table[@border]">
373
      <p>
374
         <table border="{@border}">
375
            <xsl:apply-templates/>
376
         </table>
377
      </p>
378
   </xsl:template>
379

    
380
   <xsl:template match="h:td">
381
      <cell>
382
         <xsl:if test="@valign">
383
            <xsl:attribute name="valign">
384
               <xsl:value-of select="@valign"/>
385
            </xsl:attribute>
386
         </xsl:if>
387
         <xsl:if test="h:p[@align]">
388
            <xsl:attribute name="align">
389
               <xsl:value-of select="child::h:p/@align"/>
390
            </xsl:attribute>
391
         </xsl:if>
392
         <xsl:apply-templates/>
393
      </cell>
394
   </xsl:template>
395
   <xsl:template match="h:address"/>
396

    
397

    
398

    
399
   <xsl:template match="text()">
400
      <xsl:variable name="N">
401
         <xsl:value-of select="name(preceding-sibling::*[1])"/>
402
      </xsl:variable>
403
      <xsl:choose>
404
         <xsl:when test="$N='h1' or $N='h3' or $N='h3' or $N='h4'">
405
            <p>
406
               <xsl:value-of select="normalize-space(.)"/>
407
            </p>
408
         </xsl:when>
409
         <xsl:otherwise>
410
            <xsl:choose>
411
               <xsl:when test="parent::h:td">
412
                  <xsl:value-of select="."/>
413
               </xsl:when>
414
               <xsl:otherwise>
415
                  <xsl:value-of select="translate(.,'&#160;',' ')"/>      
416
               </xsl:otherwise>
417
            </xsl:choose>
418
         </xsl:otherwise>
419
      </xsl:choose>
420
   </xsl:template>
421

    
422
   <xsl:template match="h:img">
423
      <figure file="{@src}">
424
         <xsl:if test="@alt">
425
            <figDesc>
426
               <xsl:value-of select="@alt"/>
427
            </figDesc>
428
         </xsl:if>
429
      </figure>
430
   </xsl:template>
431

    
432
   <xsl:template match="h:form|h:input|h:textarea|h:option|h:select">
433
      <xsl:copy>
434
         <xsl:copy-of select="@*"/>
435
         <xsl:apply-templates/>
436
      </xsl:copy>
437
   </xsl:template>
438
   <xsl:template name="for-loop">
439
<!-- simplified from tidwell p86. only does step up or down until -->
440
    <xsl:param name="i" select="1"/>
441
      <xsl:param name="stepsize" select="1"/>
442
      <xsl:param name="until" select="1"/>
443
      <xsl:param name="iteration" select="1"/>
444
      <xsl:param name="insert" select="''"/>
445

    
446
      <xsl:variable name="step">
447
  
448
         <xsl:choose>
449
            <xsl:when test="$until &lt; $i">
450
               <xsl:text>-</xsl:text>
451
               <xsl:value-of select="$stepsize"/>
452
            </xsl:when>
453
            <xsl:otherwise>
454
               <xsl:value-of select="$stepsize"/>
455
            </xsl:otherwise>
456
         </xsl:choose>
457
      </xsl:variable>  
458
    
459
      <xsl:text disable-output-escaping="yes">&lt;</xsl:text>      
460
      <xsl:value-of select="$insert"/>
461
      <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
462
      <xsl:variable name="testpassed">
463
         <xsl:choose>
464
            <xsl:when test="$i != $until">
465
               <xsl:text>true</xsl:text>
466
            </xsl:when>
467
            <xsl:otherwise>
468
               <xsl:text>false</xsl:text>
469
            </xsl:otherwise>
470
         </xsl:choose>
471
      </xsl:variable>
472
      <xsl:if test="$testpassed = 'true'">    
473

    
474
         <xsl:call-template name="for-loop">
475
            <xsl:with-param name="i" select="$i + $step"/>
476
            <xsl:with-param name="stepsize" select="$stepsize"/>
477
            <xsl:with-param name="until" select="$until"/>
478
            <xsl:with-param name="iteration" select="$iteration + 1"/>
479
            <xsl:with-param name="insert" select="$insert"/>
480
         </xsl:call-template>
481
      </xsl:if>
482
   </xsl:template>
483
</xsl:stylesheet>