Statistics
| Revision:

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

History | View | Annotate | Download (6.1 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet version="2.0" 
3
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
	xpath-default-namespace="http://www.tei-c.org/ns/1.0"
5
	xmlns="http://www.tei-c.org/ns/1.0"
6
>
7

    
8
  <!--
9
      
10
      $Id: odd2nuodd.xsl 9297 2011-09-07 21:37:09Z rahtz $
11
      Sebastian Rahtz 2011/03/26
12
      
13
      Read an ODD with <elementSpec mode="delete"> statements and rewrite it
14
      as <moduleRef include="* * *" or except="* * *">
15
      
16
  -->
17
  <!-- where is a copy of P5? -->
18
  <xsl:param name="P5">http://www.tei-c.org/Vault/P5/current/xml/tei/odd/p5subset.xml</xsl:param>
19
  
20
  <!-- do you want moduleRef generated with @include or @except? -->
21
  <xsl:param name="method">include</xsl:param>
22

    
23

    
24

    
25
  <xsl:key name="EbyM" match="elementSpec" use="@module"/>
26

    
27
  <xsl:key name="deletedE" match="elementSpec[@mode='delete']" use="@ident"/>
28
  <xsl:key name="changedE" match="elementSpec[@mode='change']" use="@ident"/>
29
  <xsl:key name="changedE" match="elementSpec[@mode='replace']" use="@ident"/>
30

    
31
  <xsl:variable name="orig" select="/"/>
32

    
33
  <!-- identifty transforms -->
34
  <xsl:template match="@*|text()|comment()|processing-instruction()">
35
    <xsl:copy-of select="."/>
36
  </xsl:template>
37

    
38
  <xsl:template match="@*|text()|comment()|processing-instruction()" mode="stage2">
39
    <xsl:copy-of select="."/>
40
  </xsl:template>
41

    
42
  <xsl:template match="*">
43
    <xsl:copy>
44
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
45
    </xsl:copy>
46
  </xsl:template>
47

    
48
  <xsl:template match="*" mode="stage2">
49
    <xsl:copy>
50
      <xsl:apply-templates
51
	  select="*|@*|processing-instruction()|comment()|text()" mode="stage2"/>
52
    </xsl:copy>
53
  </xsl:template>
54
  
55
  
56
  <!-- work in two phases, so we can zap empty specGrp on pass 2-->
57
  <xsl:template match="/">
58
    <xsl:variable name="stage1">
59
      <xsl:apply-templates/>
60
    </xsl:variable>
61
    <xsl:for-each select="$stage1">
62
      <xsl:apply-templates mode="stage2"/>
63
    </xsl:for-each>
64
  </xsl:template>
65
  
66
  <!-- ignore elementSpec @mode='delete' -->
67
  <xsl:template match="elementSpec[@mode='delete']"/>
68
  
69
  <xsl:template match="moduleRef/@include"/>
70
  <xsl:template match="moduleRef/@except"/>
71

    
72
  <!-- for any moduleRef, look up all the members of it in P5;
73
       if they are not deleted by this odd, add them to a list to be
74
       included -->
75
  <xsl:template match="moduleRef[@key]">
76
    <xsl:variable name="here" select="."/>
77
    <xsl:copy>
78
      <xsl:apply-templates select="@*"/>
79
      <xsl:variable name="module" select="@key"/>
80
      <xsl:choose>
81
	<xsl:when test="$method='include' and @include">
82
	  <xsl:copy-of select="@include"/>
83
	</xsl:when>	
84
	<xsl:when test="$method='except' and @except">
85
	  <xsl:copy-of select="@except"/>
86
	</xsl:when>	
87
	<xsl:when test="$method='include' and @except">
88
	  <xsl:variable name="not">
89
	    <xsl:for-each select="tokenize($here/@except,' ')">
90
	      <not ident="{.}"/>
91
	    </xsl:for-each>
92
	  </xsl:variable>
93
	  <xsl:variable name="includelist">
94
	    <xsl:for-each select="document($P5)">
95
	      <xsl:for-each select="key('EbyM',$module)">
96
		<xsl:sort select="@ident"/>
97
		<xsl:variable name="e" select="@ident"/>
98
		<xsl:if test="not($not/not[@ident=$e])">
99
		  <xsl:value-of select="$e"/>
100
		    <xsl:text> </xsl:text>
101
		</xsl:if>
102
	      </xsl:for-each>
103
	    </xsl:for-each>
104
	  </xsl:variable>
105
	  <xsl:if test="not($includelist='')">
106
	    <xsl:attribute name="include" select="normalize-space($includelist)"/>
107
	  </xsl:if>
108
	</xsl:when>
109
	<xsl:when test="$method='include'">
110
	  <xsl:variable name="includelist">
111
	    <xsl:for-each select="document($P5)">
112
	      <xsl:for-each select="key('EbyM',$module)">
113
		<xsl:sort select="@ident"/>
114
		<xsl:variable name="e" select="@ident"/>
115
		<xsl:for-each select="$orig">
116
		  <xsl:choose>
117
		  <xsl:when test="key('deletedE',$e)"/>
118
		  <xsl:otherwise>
119
		    <xsl:value-of select="$e"/>
120
		    <xsl:text> </xsl:text>
121
		  </xsl:otherwise>
122
		  </xsl:choose>
123
		</xsl:for-each>
124
	      </xsl:for-each>
125
	    </xsl:for-each>
126
	  </xsl:variable>
127
	  <xsl:if test="not($includelist='')">
128
	    <xsl:attribute name="include"
129
			   select="normalize-space($includelist)"/>
130
	  </xsl:if>
131
	</xsl:when>
132
	<xsl:when test="$method='except' and @include">
133
	  <xsl:variable name="yes">
134
	    <xsl:for-each select="tokenize($here/@include,' ')">
135
	      <yes ident="{.}"/>
136
	    </xsl:for-each>
137
	  </xsl:variable>
138
	  <xsl:variable name="exceptlist">
139
	    <xsl:for-each select="document($P5)">
140
	      <xsl:for-each select="key('EbyM',$module)">
141
		<xsl:sort select="@ident"/>
142
		<xsl:variable name="e" select="@ident"/>
143
		<xsl:for-each select="$orig">
144
		  <xsl:if test="not($yes/yes[@ident=$e])">
145
		    <xsl:value-of select="$e"/>
146
		    <xsl:text> </xsl:text>
147
		  </xsl:if>
148
		</xsl:for-each>
149
	      </xsl:for-each>
150
	    </xsl:for-each>
151
	  </xsl:variable>
152
	  <xsl:if test="not($exceptlist='')">
153
	    <xsl:attribute name="except"  select="normalize-space($exceptlist)"/>
154
	  </xsl:if>
155
	</xsl:when>
156
	<xsl:when test="$method='except'">
157
	  <xsl:variable name="exceptlist">
158
	    <xsl:for-each select="document($P5)">
159
	      <xsl:for-each select="key('EbyM',$module)">
160
		<xsl:sort select="@ident"/>
161
		<xsl:variable name="e" select="@ident"/>
162
		<xsl:for-each select="$orig">
163
		  <xsl:if test="key('deletedE',$e)">
164
		    <xsl:value-of select="$e"/>
165
		    <xsl:text> </xsl:text>
166
		  </xsl:if>
167
		</xsl:for-each>
168
	      </xsl:for-each>
169
	    </xsl:for-each>
170
	  </xsl:variable>
171
	  <xsl:if test="not($exceptlist='')">
172
	    <xsl:attribute name="except" select="normalize-space($exceptlist)"/>
173
	  </xsl:if>
174
	</xsl:when>
175
	<xsl:otherwise>
176
	  <xsl:message terminate="yes">Method <xsl:value-of
177
	  select="$method"/> not supported</xsl:message>
178
	</xsl:otherwise>
179
      </xsl:choose>
180
	</xsl:copy>
181
      </xsl:template>
182

    
183
  <xsl:template mode="stage2" match="specGrp[not(*)]"/>
184

    
185
  <xsl:template mode="stage2" match="specGrpRef">
186
    <xsl:choose>
187
      <xsl:when test="starts-with(@target,'#')">
188
	<xsl:for-each
189
	    select="id(substring(@target,2))">
190
	  <xsl:if test="*">
191
	    <specGrpRef target="#{@xml:id}"/>
192
	  </xsl:if>
193
	</xsl:for-each>
194
      </xsl:when>
195
      <xsl:otherwise>
196
	<xsl:copy-of select="."/>
197
      </xsl:otherwise>
198
    </xsl:choose>
199
  </xsl:template>
200
</xsl:stylesheet>