-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsDec-to-Schematron.xsl
220 lines (200 loc) · 11.3 KB
/
fsDec-to-Schematron.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
xmlns="http://purl.oclc.org/dsdl/schematron" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="boundary">', '</xsl:variable>
<xsl:template match="/">
<xsl:element name="schema">
<xsl:attribute name="queryBinding">xslt2</xsl:attribute>
<ns uri="http://www.tei-c.org/ns/1.0" prefix="tei"/>
<!--Variables to contain the IDs present in the Feature Structure Declaration-->
<let name="featureIDs"
value="{concat($apos, string-join(//fDecl[@xml:id]/concat('#', @xml:id), $boundary), $apos)}"/>
<let name="fsIDs"
value="{concat($apos, string-join(//fsDecl[@xml:id]/concat('#', @xml:id), $boundary), $apos)}"/>
<pattern>
<rule context="@feats">
<let name="values"
value="for $i in tokenize(current(), '\s+') return $i"/>
<assert
test="
every $value in $values
satisfies $value = $featureIDs"
>One of the values inside the @feats attribute does not match any f/@xml:id of
the Feature Structure Declaration</assert>
</rule>
<rule context="@fVal">
<let name="values"
value="for $i in tokenize(current(), '\s+') return $i"/>
<assert
test="
every $value in $values
satisfies $value = $fsIDs"
>One of the values inside the @fVal attribute does not match any fs/@xml:id of
the Feature Structure Declaration</assert>
</rule>
<xsl:apply-templates select="//fsdDecl"/>
</pattern>
</xsl:element>
</xsl:template>
<xsl:template match="fsDecl">
<xsl:variable name="optionalFeatures" select="fDecl[@optional eq 'true']/@name"/>
<xsl:variable name="optionalFeaturesList"
select="concat($apos, string-join($optionalFeatures, $boundary), $apos)"/>
<!--If there is no @optional attribute with a 'true' value, that feature is considered mandatory-->
<xsl:variable name="mandatoryFeatures" select="fDecl[not(@optional eq 'true')]/@name"/>
<xsl:variable name="mandatoryFeaturesList"
select="concat($apos, string-join($mandatoryFeatures, $boundary), $apos)"/>
<xsl:variable name="countMandatory" select="count($mandatoryFeatures)"/>
<!-- The <fs> element has a @type attribute to discern it from others which means that the @type is unique-->
<xsl:if
test="current()[@type][not(@type = (preceding-sibling::fsDecl | following-sibling::fsDecl)/@type)]">
<xsl:element name="rule">
<xsl:attribute name="context">
<xsl:value-of
select="concat('tei:fs[not(@feats)][@type eq ', $apos, current()/@type, $apos, ']')"
/>
</xsl:attribute>
<!--Rule to check that the features used that are not declared as mandatory (that is, the optional ones) are indeed in the
feature structure declaration -->
<xsl:if test="count($optionalFeatures) gt 0">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:value-of
select="concat('if (count(./tei:f) gt ', $countMandatory, ') then tei:f[@name = (', $optionalFeaturesList, ')] else true()')"
/>
</xsl:attribute> The mandatory features are <xsl:value-of
select="$mandatoryFeaturesList"/>; and the optional ones are
<xsl:value-of select="$optionalFeaturesList"/>. There is a feature in
this structure that has not been declared </xsl:element>
</xsl:if>
<!--Rule to check that the non-optional features are in the
feature structure declaration -->
<xsl:if test="count($mandatoryFeatures) gt 0">
<xsl:for-each select="$mandatoryFeatures">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:value-of
select="concat('tei:f[@name eq ', $apos, current(), $apos, ']')"
/>
</xsl:attribute> The feature “<xsl:value-of select="current()"/>” is
missing </xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="fDecl"/>
</xsl:template>
<xsl:template match="fDecl">
<xsl:element name="rule">
<xsl:attribute name="context">
<xsl:value-of
select="concat('tei:f[@name eq ', $apos, current()/@name, $apos, ']', '[parent::fs[@type eq ', $apos, current()/parent::fsDecl/@type, $apos, ']]')"
/>
</xsl:attribute>
<!-- I'm considering that the use of <vNot> followed by an empty element means
that the element is mandatory (and that it must have content)-->
<xsl:if test="vRange/vNot/*[not(*)]">
<xsl:for-each select="vRange/vNot/*">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:value-of select="concat('tei:', current()/name())"/>
</xsl:attribute> The element <xsl:value-of select="current()/name()"/> is
required </xsl:element>
<xsl:element name="report">
<xsl:attribute name="test"><xsl:value-of
select="concat('tei:', current()/name(), '[not(*)]')"/>
</xsl:attribute>This feature must contain a <xsl:value-of
select="current()/name()"/> element with content </xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:if test="vRange/vAlt/count(distinct-values(*/name())) = 1">
<xsl:choose>
<xsl:when test="vRange/vAlt/vNot">
<xsl:element name="assert">
<xsl:attribute name="test">tei:vAlt/tei:string/text()</xsl:attribute>
<xsl:text>This cannot be an empty element</xsl:text>
</xsl:element>
</xsl:when>
<xsl:when test="vRange/vAlt/fs">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:variable name="types">
<xsl:value-of
select="replace(string-join(./vRange/vAlt/fs/@type, '-'), '-', $boundary)"
/>
</xsl:variable>
<xsl:value-of
select="concat('tei:fs[@type = (', $apos, $types, $apos, ')]')"
/>
</xsl:attribute> Incorrect @type value. Possible values are:
<xsl:value-of
select="concat($apos, string-join(./vRange/vAlt/fs/@type, ', '), $apos)"
/>
</xsl:element>
</xsl:when>
<xsl:when test="vRange/vAlt/binary">
<xsl:element name="assert">
<xsl:attribute name="test">tei:binary</xsl:attribute> Binary feature.
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:variable name="symbols">
<xsl:value-of
select="replace(string-join(./vRange/vAlt/symbol/@value, ','), ',', $boundary)"
/>
</xsl:variable>
<xsl:value-of
select="concat('tei:symbol[@value = (', $apos, $symbols, $apos, ')]')"
/>
</xsl:attribute> Incorrect @type value. Possible values are:
<xsl:value-of
select="concat($apos, string-join(./vRange/vAlt/symbol/@value, ', '), $apos)"
/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="vRange/vAlt/count(distinct-values(*/name())) = 2">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:variable name="types">
<xsl:value-of
select="replace(string-join(./vRange/vAlt/fs/@type, ','), ',', $boundary)"
/>
</xsl:variable>
<xsl:variable name="symbols">
<xsl:value-of
select="replace(string-join(./vRange/vAlt/symbol/@value, ','), ',', $boundary)"
/>
</xsl:variable>
<xsl:value-of
select="concat('tei:symbol[@value = (', $apos, $symbols, $apos, ')] or tei:fs[@type = (', $apos, $types, $apos, ')]')"
/>
</xsl:attribute> Incorrect value. Possible values are: a fs element with one of
the following @type attributes <xsl:value-of
select="concat($apos, string-join(./vRange/vAlt/fs/@type, ', '), $apos)"/>
or a symbol element with one of the following @value attributes <xsl:value-of
select="concat($apos, string-join(./vRange/vAlt/symbol/@value, ', '), $apos)"
/>
</xsl:element>
</xsl:if>
<xsl:if test="vRange/fs">
<xsl:for-each select="vRange/fs">
<xsl:element name="assert">
<xsl:attribute name="test">
<xsl:value-of
select="concat('tei:fs[@name =', $apos, current(), $apos, ']')"/>
</xsl:attribute> Required fs of type <xsl:value-of select="current()/@type"
/>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>