forked from TEIC/Stylesheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodd2nuodd.xsl
200 lines (179 loc) · 6.07 KB
/
odd2nuodd.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
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
xmlns="http://www.tei-c.org/ns/1.0"
>
<!--
$Id$
Sebastian Rahtz 2011/03/26
Read an ODD with <elementSpec mode="delete"> statements and rewrite it
as <moduleRef include="* * *" or except="* * *">
-->
<!-- where is a copy of P5? -->
<xsl:param name="defaultSource">https://www.tei-c.org/Vault/P5/current/xml/tei/odd/p5subset.xml</xsl:param>
<!-- do you want moduleRef generated with @include or @except? -->
<xsl:param name="method">include</xsl:param>
<xsl:key name="EbyM" match="elementSpec" use="@module"/>
<xsl:key name="deletedE" match="elementSpec[@mode='delete']" use="@ident"/>
<xsl:key name="changedE" match="elementSpec[@mode='change']" use="@ident"/>
<xsl:key name="changedE" match="elementSpec[@mode='replace']" use="@ident"/>
<xsl:variable name="orig" select="/"/>
<!-- identifty transforms -->
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()" mode="stage2">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="stage2">
<xsl:copy>
<xsl:apply-templates
select="*|@*|processing-instruction()|comment()|text()" mode="stage2"/>
</xsl:copy>
</xsl:template>
<!-- work in two phases, so we can zap empty specGrp on pass 2-->
<xsl:template match="/">
<xsl:variable name="stage1">
<xsl:apply-templates/>
</xsl:variable>
<xsl:for-each select="$stage1">
<xsl:apply-templates mode="stage2"/>
</xsl:for-each>
</xsl:template>
<!-- ignore elementSpec @mode='delete' -->
<xsl:template match="elementSpec[@mode='delete']"/>
<xsl:template match="moduleRef/@include"/>
<xsl:template match="moduleRef/@except"/>
<!-- for any moduleRef, look up all the members of it in P5;
if they are not deleted by this odd, add them to a list to be
included -->
<xsl:template match="moduleRef[@key]">
<xsl:variable name="here" select="."/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:variable name="module" select="@key"/>
<xsl:choose>
<xsl:when test="$method='include' and @include">
<xsl:copy-of select="@include"/>
</xsl:when>
<xsl:when test="$method='except' and @except">
<xsl:copy-of select="@except"/>
</xsl:when>
<xsl:when test="$method='include' and @except">
<xsl:variable name="not">
<xsl:for-each select="tokenize($here/@except,' ')">
<not ident="{.}"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="includelist">
<xsl:for-each select="document($defaultSource)">
<xsl:for-each select="key('EbyM',$module)">
<xsl:sort select="@ident"/>
<xsl:variable name="e" select="@ident"/>
<xsl:if test="not($not/not[@ident=$e])">
<xsl:value-of select="$e"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not($includelist='')">
<xsl:attribute name="include" select="normalize-space($includelist)"/>
</xsl:if>
</xsl:when>
<xsl:when test="$method='include'">
<xsl:variable name="includelist">
<xsl:for-each select="document($defaultSource)">
<xsl:for-each select="key('EbyM',$module)">
<xsl:sort select="@ident"/>
<xsl:variable name="e" select="@ident"/>
<xsl:for-each select="$orig">
<xsl:choose>
<xsl:when test="key('deletedE',$e)"/>
<xsl:otherwise>
<xsl:value-of select="$e"/>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not($includelist='')">
<xsl:attribute name="include"
select="normalize-space($includelist)"/>
</xsl:if>
</xsl:when>
<xsl:when test="$method='except' and @include">
<xsl:variable name="yes">
<xsl:for-each select="tokenize($here/@include,' ')">
<yes ident="{.}"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="exceptlist">
<xsl:for-each select="document($defaultSource)">
<xsl:for-each select="key('EbyM',$module)">
<xsl:sort select="@ident"/>
<xsl:variable name="e" select="@ident"/>
<xsl:for-each select="$orig">
<xsl:if test="not($yes/yes[@ident=$e])">
<xsl:value-of select="$e"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not($exceptlist='')">
<xsl:attribute name="except" select="normalize-space($exceptlist)"/>
</xsl:if>
</xsl:when>
<xsl:when test="$method='except'">
<xsl:variable name="exceptlist">
<xsl:for-each select="document($defaultSource)">
<xsl:for-each select="key('EbyM',$module)">
<xsl:sort select="@ident"/>
<xsl:variable name="e" select="@ident"/>
<xsl:for-each select="$orig">
<xsl:if test="key('deletedE',$e)">
<xsl:value-of select="$e"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not($exceptlist='')">
<xsl:attribute name="except" select="normalize-space($exceptlist)"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Method <xsl:value-of
select="$method"/> not supported</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:template mode="stage2" match="specGrp[not(*)]"/>
<xsl:template mode="stage2" match="specGrpRef">
<xsl:choose>
<xsl:when test="starts-with(@target,'#')">
<xsl:for-each
select="id(substring(@target,2))">
<xsl:if test="*">
<specGrpRef target="#{@xml:id}"/>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>