Statistics
| Revision:

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

History | View | Annotate | Download (18.5 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
		xmlns:n="www.example.com"
4
		xmlns:rng="http://relaxng.org/ns/structure/1.0"
5
		xmlns:tei="http://www.tei-c.org/ns/1.0"
6
		xmlns="http://www.tei-c.org/ns/1.0"
7
		exclude-result-prefixes="rng tei n"
8
		xpath-default-namespace="http://www.tei-c.org/ns/1.0"
9
		version="2.0"><!--
10
This software is dual-licensed:
11

    
12
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
13
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
14

    
15
2. http://www.opensource.org/licenses/BSD-2-Clause
16
		
17
All rights reserved.
18

    
19
Redistribution and use in source and binary forms, with or without
20
modification, are permitted provided that the following conditions are
21
met:
22

    
23
* Redistributions of source code must retain the above copyright
24
notice, this list of conditions and the following disclaimer.
25

    
26
* Redistributions in binary form must reproduce the above copyright
27
notice, this list of conditions and the following disclaimer in the
28
documentation and/or other materials provided with the distribution.
29

    
30
This software is provided by the copyright holders and contributors
31
"as is" and any express or implied warranties, including, but not
32
limited to, the implied warranties of merchantability and fitness for
33
a particular purpose are disclaimed. In no event shall the copyright
34
holder or contributors be liable for any direct, indirect, incidental,
35
special, exemplary, or consequential damages (including, but not
36
limited to, procurement of substitute goods or services; loss of use,
37
data, or profits; or business interruption) however caused and on any
38
theory of liability, whether in contract, strict liability, or tort
39
(including negligence or otherwise) arising in any way out of the use
40
of this software, even if advised of the possibility of such damage.
41

    
42
$Id: oddbyexample.xsl 10028 2012-01-11 22:45:07Z rahtz $
43

    
44
2008, TEI Consortium
45
-->
46
  <!-- typical usage:
47
   saxon -it:main -o:myodd /usr/share/xml/tei/stylesheet/tools2/oddbyexample.xsl   corpus=`pwd`/
48

    
49
-->
50
  <xsl:import href="getfiles.xsl"/>
51
  <!-- 
52
read a corpus of TEI P5 documents and construct
53
an ODD customization file which expresses the subset
54
of the TEI you need to validate that corpus
55
-->
56
  <xsl:output indent="yes"/>
57
  <!-- name of odd -->
58
  <xsl:param name="schema">oddbyexample</xsl:param>
59
  <!-- whether to do all the global attributes -->
60
  <xsl:param name="keepGlobals">false</xsl:param>
61
  <!-- the document corpus -->
62
  <xsl:param name="corpus">./</xsl:param>
63
  <!-- the source of the TEI (just needs *Spec)-->
64
  <xsl:param name="tei">http://www.tei-c.org/Vault/P5/current/xml/tei/odd/p5subset.xml</xsl:param>
65
  <!-- should we make valList for @rend -->
66
  <xsl:param name="enumerateRend">false</xsl:param>
67
  <xsl:param name="enumerateType">false</xsl:param>
68
  <!-- should we deal with non-TEI namespaces -->
69
  <xsl:param name="processNonTEI">false</xsl:param>
70
  <!-- which attributes should be make valLists for, regardless -->
71
  <xsl:param name="attributeList"/>
72
  <xsl:variable name="checkAtts">
73
    <xsl:text>,</xsl:text>
74
    <xsl:value-of select="$attributeList"/>
75
    <xsl:text>,</xsl:text>
76
  </xsl:variable>
77
  <xsl:key name="Atts" match="@*" use="local-name(parent::*)"/>
78
  <xsl:key name="attVals" match="@*" use="concat(local-name(parent::*),local-name())"/>
79
  <xsl:key name="ELEMENTS" use="1" match="elementSpec"/>
80
  <xsl:key name="CLASSES" use="1" match="classSpec[@type='atts']"/>
81
  <xsl:key name="IDENTS" use="@ident" match="*[@ident]"/>
82
  <xsl:key name="MEMBERS" use="@key" match="elementSpec/classes/memberOf"/>
83
  <xsl:key name="CLASSMEMBERS" use="@key" match="classSpec/classes/memberOf"/>
84
  <xsl:key name="Used" use="@ident" match="docs/elementSpec"/>
85
  <xsl:key name="UsedAtt" use="concat(../@ident,@ident)" match="docs/elementSpec/attDef"/>
86
  <!--
87
1) start a variable and copy in all of the TEI 
88
2) read the corpus and get a list of all the elements and their
89
attributes that it uses, put that in the same variable
90
3) process the variable and read the TEI section. if an element or
91
 attribute is not present in the corpus section, put out a delete
92
 customization
93
4) for every attribute which is of type "enumerated", construct a
94
valList
95

    
96
-->
97
  <xsl:template name="processAll">
98
    <xsl:variable name="count">
99
      <xsl:value-of select="count(/n:ROOT/*)"/>
100
    </xsl:variable>
101
    <!-- assemble together all the TEI elements and attributes, 
102
     followed by all the
103
     elements and attributes used in the corpus -->
104
    <xsl:variable name="stage1">
105
      <stage1>
106
        <tei>
107
          <xsl:for-each select="document($tei)">
108
            <xsl:for-each select="key('CLASSES',1)">
109
              <classSpec>
110
                <xsl:copy-of select="@ident"/>
111
                <xsl:copy-of select="@module"/>
112
                <xsl:for-each select=".//attDef">
113
                  <attDef>
114
                    <xsl:copy-of select="@ident"/>
115
                    <xsl:call-template name="checktype"/>
116
                  </attDef>
117
                </xsl:for-each>
118
                <xsl:call-template name="classmembers"/>
119
              </classSpec>
120
            </xsl:for-each>
121
            <xsl:for-each select="key('ELEMENTS',1)">
122
              <elementSpec>
123
                <xsl:copy-of select="@ident"/>
124
                <xsl:copy-of select="@module"/>
125
                <xsl:for-each select=".//tei:attDef">
126
                  <attDef>
127
                    <xsl:copy-of select="@ident"/>
128
                    <xsl:call-template name="checktype"/>
129
                  </attDef>
130
                </xsl:for-each>
131
                <xsl:call-template name="classatts"/>
132
              </elementSpec>
133
            </xsl:for-each>
134
          </xsl:for-each>
135
        </tei>
136
        <docs>
137
          <xsl:for-each-group select="key('AllTEI',1)" group-by="local-name()">
138
            <xsl:sort select="current-grouping-key()"/>
139
            <xsl:variable name="ident" select="current-grouping-key()"/>
140
            <elementSpec>
141
              <xsl:attribute name="ident">
142
                <xsl:copy-of select="current-grouping-key()"/>
143
              </xsl:attribute>
144
              <xsl:for-each-group select="key('Atts',$ident)" group-by="local-name()">
145
                <attDef ident="{name()}">
146
                  <valList type="closed">
147
                    <xsl:for-each-group select="key('attVals',concat($ident,local-name()))" group-by=".">
148
                      <xsl:sort select="."/>
149
                      <xsl:choose>
150
                        <xsl:when test="contains(current-grouping-key(),' ')">
151
                          <xsl:for-each select="tokenize(current-grouping-key(),' ')">
152
                            <valItem ident="{.}"/>
153
                          </xsl:for-each>
154
                        </xsl:when>
155
                        <xsl:otherwise>
156
                          <valItem ident="{current-grouping-key()}"/>
157
                        </xsl:otherwise>
158
                      </xsl:choose>
159
                    </xsl:for-each-group>
160
                  </valList>
161
                </attDef>
162
              </xsl:for-each-group>
163
            </elementSpec>
164
          </xsl:for-each-group>
165
        </docs>
166
      </stage1>
167
    </xsl:variable>
168
    <xsl:variable name="stage2">
169
      <stage2>
170
        <!-- for every attribute class, see if its members should be
171
     deleted, by seeing if they are used anywhere-->
172
        <xsl:for-each select="$stage1/stage1/tei/classSpec">
173
          <classSpec ident="{@ident}" module="{@module}" type="atts" mode="change">
174
            <attList>
175
              <xsl:for-each select="attDef">
176
                <xsl:variable name="this" select="@ident"/>
177
                <xsl:variable name="used">
178
                  <xsl:for-each select="../member">
179
                    <!--		 <xsl:message>check for <xsl:value-of select="concat(@ident,$this)"/></xsl:message>-->
180
                    <xsl:if test="key('UsedAtt',concat(@ident,$this))">true</xsl:if>
181
                  </xsl:for-each>
182
                </xsl:variable>
183
                <xsl:choose>
184
                  <xsl:when test="$keepGlobals='true' and $this='n'"/>
185
                  <xsl:when test="$keepGlobals='true' and $this='rendition'"/>
186
                  <xsl:when test="$keepGlobals='true' and $this='xml:id'"/>
187
                  <xsl:when test="$keepGlobals='true' and $this='xml:base'"/>
188
                  <xsl:when test="$keepGlobals='true' and $this='xml:space'"/>
189
                  <xsl:when test="$keepGlobals='true' and $this='xml:lang'"/>
190
                  <xsl:when test="$keepGlobals='true' and $this='rend'"/>
191
                  <xsl:when test="$used=''">
192
                    <attDef ident="{$this}" mode="delete"/>
193
                  </xsl:when>
194
                </xsl:choose>
195
              </xsl:for-each>
196
            </attList>
197
          </classSpec>
198
        </xsl:for-each>
199
        <!-- for every TEI element, say if it is actually used or is to be deleted -->
200
        <xsl:for-each select="$stage1/stage1/tei/elementSpec">
201
          <xsl:choose>
202
            <xsl:when test="key('Used',@ident)">
203
              <elementSpec ident="{@ident}" module="{@module}" mode="keep">
204
                <xsl:copy-of select="attDef"/>
205
              </elementSpec>
206
            </xsl:when>
207
            <xsl:otherwise>
208
              <elementSpec ident="{@ident}" module="{@module}" mode="delete"/>
209
            </xsl:otherwise>
210
          </xsl:choose>
211
        </xsl:for-each>
212
        <xsl:if test="$processNonTEI='true'">
213
          <xsl:for-each-group select="key('All',1)" group-by="local-name()">
214
            <xsl:sort/>
215
            <xsl:choose>
216
              <xsl:when test="self::n:ROOT"/>
217
              <xsl:when test="namespace-uri()='http://www.tei-c.org/ns/1.0'"/>
218
              <xsl:otherwise>
219
                <!-- build new 'add' elementSpec -->
220
                <elementSpec ident="{current-grouping-key()}" mode="add" ns="{namespace-uri()}">
221
                  <xsl:text>&#10;</xsl:text>
222
                  <xsl:comment>add an &lt;equiv/&gt; to point to an named  template
223
in an XSLT file which will transform this to pure TEI</xsl:comment>
224
                  <xsl:text>&#10;</xsl:text>
225
                  <equiv filter="somefile.xsl" mimeType="text/xsl" name="{current-grouping-key()}"/>
226
                  <desc>
227
                    <xsl:text>&#10;</xsl:text>
228
                    <xsl:comment> Describe the <xsl:value-of select="current-grouping-key()"/> element  here</xsl:comment>
229
                    <xsl:text>&#10;</xsl:text>
230
                  </desc>
231
                  <classes>
232
                    <xsl:text>&#10;</xsl:text>
233
                    <xsl:comment> Add a 'memberOf key="model.className"' stanza for whatever classes it belongs to  here</xsl:comment>
234
                    <xsl:text>&#10;</xsl:text>
235
                  </classes>
236
                  <content>
237
                    <xsl:text>&#10;</xsl:text>
238
                    <xsl:comment>Add RNG content model here</xsl:comment>
239
                    <xsl:text>&#10;</xsl:text>
240
                  </content>
241
                  <xsl:if test="key('Atts',local-name())">
242
                    <attList>
243
                      <xsl:comment>Add attDefs:</xsl:comment>
244
                      <xsl:text>&#10;</xsl:text>
245
                      <xsl:for-each-group select="key('Atts',local-name())" group-by="local-name()">
246
                        <xsl:sort/>
247
                        <attDef ident="{local-name()}" mode="add"/>
248
                      </xsl:for-each-group>
249
                    </attList>
250
                  </xsl:if>
251
                </elementSpec>
252
              </xsl:otherwise>
253
            </xsl:choose>
254
          </xsl:for-each-group>
255
        </xsl:if>
256
      </stage2>
257
    </xsl:variable>
258
    <!-- start writing the final ODD document -->
259
    <TEI xml:lang="en">
260
      <teiHeader>
261
        <fileDesc>
262
          <titleStmt>
263
            <title>ODD by Example customization</title>
264
          </titleStmt>
265
          <editionStmt>
266
            <edition>generated on     
267
	   <date><xsl:value-of select="format-dateTime(current-dateTime(),'[Y]-[M02]-[D02]T[H02]:[m02]:[s02]Z')"/></date>
268
	                 </edition>
269
          </editionStmt>
270
          <publicationStmt>
271
            <p> </p>
272
          </publicationStmt>
273
          <sourceDesc>
274
            <p>generated by oddbyexample.xsl, based on analyzing 
275
	    <xsl:value-of select="$count"/> files from
276
	    <xsl:value-of select="$corpus"/>
277
                  </p>
278
          </sourceDesc>
279
        </fileDesc>
280
      </teiHeader>
281
      <text>
282
        <body>
283
          <!--<xsl:copy-of select="$stage1"/>-->
284
          <schemaSpec ident="{$schema}">
285
            <xsl:attribute name="start">
286
              <xsl:if test="$stage2/stage2/elementSpec[@mode='keep' and     @ident='TEI']">TEI</xsl:if>
287
              <xsl:text> </xsl:text>
288
              <xsl:if test="$stage2/stage2/elementSpec[@mode='keep' and @ident='teiCorpus']">teiCorpus</xsl:if>
289
            </xsl:attribute>
290
            <moduleRef key="tei"/>
291
            <xsl:apply-templates select="$stage2/stage2/classSpec[@module='tei']"/>
292
            <moduleRef key="core"/>
293
            <!-- we need to list only modules from which elements have been used -->
294
            <xsl:for-each-group select="$stage2/stage2/elementSpec[@mode='keep']" group-by="@module">
295
              <xsl:sort select="current-grouping-key()"/>
296
              <xsl:choose>
297
                <xsl:when test="@module='core'"/>
298
                <xsl:otherwise>
299
                  <moduleRef key="{@module}"/>
300
                </xsl:otherwise>
301
              </xsl:choose>
302
              <xsl:for-each select="current-group()">
303
                <xsl:variable name="e" select="@ident"/>
304
                <!-- for every attribute, if its a class attribute, see if
305
	       its already deleted. if its a local attribute, see if its used. -->
306
                <xsl:variable name="a">
307
                  <attList>
308
                    <xsl:for-each select="attDef">
309
                      <xsl:variable name="class" select="@class"/>
310
                      <xsl:variable name="ident" select="@ident"/>
311
                      <xsl:variable name="enumerated" select="@enumerated"/>
312
                      <xsl:for-each select="$stage1">
313
                        <xsl:choose>
314
                          <xsl:when test="not($class='')      and      $stage2/stage2/classSpec[@ident=$class]/attList/attDef[@ident=$ident]">
315
			 
316
		       </xsl:when>
317
		       <xsl:when test="$keepGlobals='true' and $ident='xml:id'"/>
318
		       <xsl:when test="$keepGlobals='true' and $ident='xml:base'"/>
319
		       <xsl:when test="$keepGlobals='true' and $ident='xml:space'"/>
320
		       <xsl:when test="$keepGlobals='true' and $ident='xml:lang'"/>
321
		       <xsl:when test="$keepGlobals='true' and $ident='n'"/>
322
		       <xsl:when test="$keepGlobals='true' and $ident='rendition'"/>
323
		       <xsl:when test="$keepGlobals='true' and
324
				       $ident='rend'"/>
325
		       <xsl:when test="$keepGlobals='true' and $ident='facs'"/>
326
		       <xsl:when test="not(key('UsedAtt',concat($e,$ident)))">
327
			 <attDef ident="{$ident}" mode="delete"/>
328
		       </xsl:when>
329
		       <xsl:when test="$enumerated='true'">
330
			 <attDef ident="{$ident}" mode="change">
331
			   <xsl:apply-templates select="key('UsedAtt',concat($e,$ident))/valList"/>
332
			 </attDef>
333
		       </xsl:when>
334
			</xsl:choose>
335
		      </xsl:for-each>
336
		    </xsl:for-each>
337
		  </attList>
338
		</xsl:variable>
339
                <xsl:for-each select="$a">
340
                  <xsl:if test="attList/attDef">
341
                    <elementSpec ident="{$e}" mode="change">
342
                      <xsl:copy-of select="attList"/>
343
                    </elementSpec>
344
                  </xsl:if>
345
                </xsl:for-each>
346
              </xsl:for-each>
347
              <xsl:copy-of select="$stage2/stage2/elementSpec[@mode='delete'          and @module=current-grouping-key()]"/>
348
              <xsl:apply-templates select="$stage2/stage2/classSpec[@module=current-grouping-key()]"/>
349
            </xsl:for-each-group>
350
            <xsl:for-each select="$stage2/stage2/elementSpec[@mode='add']">
351
              <xsl:text>
352

    
353

    
354
</xsl:text>
355
              <xsl:comment>You've added an element '<xsl:value-of select="@ident"/>'
356
	   which does not seem to be a proper TEI element. This will make your TEI
357
	   documents non-conformant, but if you really want to do this you should
358
	   add an elementSpec for it if you want your document to validate. It
359
	   would be better to use a separate namespace. </xsl:comment>
360
              <xsl:text>
361

    
362
</xsl:text>
363
              <xsl:copy-of select="."/>
364
            </xsl:for-each>
365
          </schemaSpec>
366
        </body>
367
      </text>
368
    </TEI>
369
  </xsl:template>
370
  <xsl:template name="classmembers">
371
    <xsl:choose>
372
      <xsl:when test="@ident='att.global'">
373
        <xsl:for-each select="key('ELEMENTS',1)">
374
          <xsl:sort select="@ident"/>
375
          <member ident="{@ident}"/>
376
        </xsl:for-each>
377
      </xsl:when>
378
      <xsl:otherwise>
379
        <xsl:for-each select="key('MEMBERS',@ident)">
380
          <member ident="{ancestor::elementSpec/@ident}"/>
381
        </xsl:for-each>
382
        <xsl:for-each select="key('CLASSMEMBERS',@ident)/ancestor::classSpec">
383
          <xsl:call-template name="classmembers"/>
384
        </xsl:for-each>
385
      </xsl:otherwise>
386
    </xsl:choose>
387
  </xsl:template>
388
  <xsl:template name="classatts">
389
    <xsl:for-each select="classes/memberOf">
390
      <xsl:for-each select="key('IDENTS',@key)">
391
        <xsl:for-each select=".//tei:attDef">
392
          <attDef class="{ancestor::classSpec/@ident}">
393
            <xsl:copy-of select="@ident"/>
394
            <xsl:call-template name="checktype"/>
395
          </attDef>
396
        </xsl:for-each>
397
        <xsl:call-template name="classatts"/>
398
      </xsl:for-each>
399
    </xsl:for-each>
400
  </xsl:template>
401
  <xsl:template name="checktype">
402
    <xsl:attribute name="enumerated">
403
      <xsl:choose>
404
        <xsl:when test="contains($checkAtts,concat(',',@ident,','))">true</xsl:when>
405
        <xsl:when test="@ident='n'">false</xsl:when>
406
        <xsl:when test="@ident='rend' and $enumerateRend='true'">true</xsl:when>
407
        <xsl:when test="@ident='type' and $enumerateType='true'">true</xsl:when>
408
        <xsl:when test="valList[@type='closed']">true</xsl:when>
409
        <xsl:when test="datatype/rng:ref[@name='data.enumerated']">true</xsl:when>
410
        <!--
411
      <xsl:when
412
	  test="datatype/rng:ref[@name='data.word']">true</xsl:when>
413
      -->
414
        <xsl:otherwise>false</xsl:otherwise>
415
      </xsl:choose>
416
    </xsl:attribute>
417
  </xsl:template>
418
  <xsl:template match="valList">
419
    <valList mode="add" type="closed">
420
      <xsl:for-each-group select="valItem" group-by="@ident">
421
        <xsl:sort select="@ident"/>
422
        <valItem ident="{current-grouping-key()}"/>
423
      </xsl:for-each-group>
424
    </valList>
425
  </xsl:template>
426
  <xsl:template match="classSpec">
427
    <xsl:if test="attList/attDef">
428
      <xsl:copy-of select="."/>
429
    </xsl:if>
430
  </xsl:template>
431

    
432
  <xsl:template match="text()" mode="copy"/>
433

    
434
</xsl:stylesheet>