flazz-schematron 0.0.0 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Francesco Lazzarino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ Copyright © 2009 Francesco Lazzarino.
2
+
3
+ See LICENSE.txt for terms.
data/bin/stron ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'optparse'
4
+ require 'libxml'
5
+ require 'schematron'
6
+
7
+ include LibXML
8
+
9
+
10
+ puts "Usage: validate [schematron] [instance]" if ARGV.size != 2
11
+
12
+ # use the line numbers
13
+ XML.default_line_numbers = true
14
+
15
+ # Get sch and xml from command line
16
+ schema_doc = XML::Document.file ARGV[0]
17
+ instance_doc = XML::Document.file ARGV[1]
18
+
19
+ stron = Schematron::Schema.new schema_doc
20
+ stron.validate(instance_doc).each do |error|
21
+ puts '%s "%s" on line %d: %s' % [
22
+ error[:type],
23
+ error[:name],
24
+ error[:line],
25
+ error[:message]
26
+ ]
27
+ end
@@ -0,0 +1,295 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><?xar XSLT?>
2
+
3
+ <!--
4
+ OVERVIEW - iso_abstract_expand.xsl
5
+
6
+ This is a preprocessor for ISO Schematron, which implements abstract patterns.
7
+ It also
8
+ * extracts a particular schema using an ID, where there are multiple
9
+ schemas, such as when they are embedded in the same NVDL script
10
+ * experimentally, allows parameter recognition and substitution inside
11
+ text as well as @context, @test, & @select.
12
+
13
+
14
+ This should be used after iso-dsdl-include.xsl and before the skeleton or
15
+ meta-stylesheet (e.g. iso-svrl.xsl) . It only requires XSLT 1.
16
+
17
+ Each kind of inclusion can be turned off (or on) on the command line.
18
+
19
+ -->
20
+ <!--
21
+ VERSION INFORMATION
22
+ 2008-09-18 RJ
23
+ * move out param test from iso:schema template to work with XSLT 1. (Noah Fontes)
24
+
25
+ 2008-07-29 RJ
26
+ * Create. Pull out as distinct XSL in its own namespace from old iso_pre_pro.xsl
27
+ * Put everything in private namespace
28
+ * Rewrite replace_substring named template so that copyright is clear
29
+
30
+ 2008-07-24 RJ
31
+ * correct abstract patterns so for correct names: param/@name and
32
+ param/@value
33
+
34
+ 2007-01-12 RJ
35
+ * Use ISO namespace
36
+ * Use pattern/@id not pattern/@name
37
+ * Add Oliver Becker's suggests from old Schematron-love-in list for <copy>
38
+ * Add XT -ism?
39
+ 2003 RJ
40
+ * Original written for old namespace
41
+ * http://www.topologi.com/resources/iso-pre-pro.xsl
42
+ -->
43
+ <!--
44
+ LEGAL INFORMATION
45
+
46
+ Copyright (c) 2000-2008 Rick Jelliffe and Academia Sinica Computing Center, Taiwan
47
+
48
+ This software is provided 'as-is', without any express or implied warranty.
49
+ In no event will the authors be held liable for any damages arising from
50
+ the use of this software.
51
+
52
+ Permission is granted to anyone to use this software for any purpose,
53
+ including commercial applications, and to alter it and redistribute it freely,
54
+ subject to the following restrictions:
55
+
56
+ 1. The origin of this software must not be misrepresented; you must not claim
57
+ that you wrote the original software. If you use this software in a product,
58
+ an acknowledgment in the product documentation would be appreciated but is
59
+ not required.
60
+
61
+ 2. Altered source versions must be plainly marked as such, and must not be
62
+ misrepresented as being the original software.
63
+
64
+ 3. This notice may not be removed or altered from any source distribution.
65
+ -->
66
+ <xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
67
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
68
+ xmlns:iso="http://purl.oclc.org/dsdl/schematron"
69
+ xmlns:nvdl="http://purl.oclc.org/dsdl/nvdl"
70
+
71
+
72
+ xmlns:iae="http://www.schematron.com/namespace/iae"
73
+
74
+ >
75
+
76
+ <xslt:param name="schema-id"></xslt:param>
77
+
78
+
79
+ <!-- Driver for the mode -->
80
+ <xsl:template match="/">
81
+ <xsl:apply-templates select="." mode="iae:go" />
82
+ </xsl:template>
83
+
84
+
85
+ <!-- ================================================================================== -->
86
+ <!-- Normal processing rules -->
87
+ <!-- ================================================================================== -->
88
+ <!-- Output only the selected schema -->
89
+ <xslt:template match="iso:schema" >
90
+ <xsl:if test="string-length($schema-id) =0 or @id= $schema-id ">
91
+ <xslt:copy>
92
+ <xslt:copy-of select="@*" />
93
+ <xslt:apply-templates mode="iae:go" />
94
+ </xslt:copy>
95
+ </xsl:if>
96
+ </xslt:template>
97
+
98
+
99
+ <!-- Strip out any foreign elements above the Schematron schema .
100
+ -->
101
+ <xslt:template match="*[not(ancestor-or-self::iso:*)]" mode="iae:go" >
102
+ <xslt:apply-templates mode="iae:go" />
103
+ </xslt:template>
104
+
105
+
106
+ <!-- ================================================================================== -->
107
+ <!-- Handle Schematron abstract pattern preprocessing -->
108
+ <!-- abstract-to-real calls
109
+ do-pattern calls
110
+ macro-expand calls
111
+ multi-macro-expand
112
+ replace-substring -->
113
+ <!-- ================================================================================== -->
114
+
115
+ <!--
116
+ Abstract patterns allow you to say, for example
117
+
118
+ <pattern name="htmlTable" is-a="table">
119
+ <param name="row" value="html:tr"/>
120
+ <param name="cell" value="html:td" />
121
+ <param name="table" value="html:table" />
122
+ </pattern>
123
+
124
+ For a good introduction, see Uche Ogbujii's article for IBM DeveloperWorks
125
+ "Discover the flexibility of Schematron abstract patterns"
126
+ http://www-128.ibm.com/developerworks/xml/library/x-stron.html
127
+ However, note that ISO Schematron uses @name and @value attributes on
128
+ the iso:param element, and @id not @name on the pattern element.
129
+
130
+ -->
131
+
132
+ <!-- Suppress declarations of abstract patterns -->
133
+ <xslt:template match="iso:pattern[@abstract='true']" mode="iae:go" >
134
+ <xslt:comment>Suppressed abstract pattern <xslt:value-of select="@id"/> was here</xslt:comment>
135
+ </xslt:template>
136
+
137
+
138
+ <!-- Suppress uses of abstract patterns -->
139
+ <xslt:template match="iso:pattern[@is-a]" mode="iae:go" >
140
+ <xslt:comment>Start pattern based on abstract <xslt:value-of select="@is-a"/></xslt:comment>
141
+
142
+ <xslt:call-template name="iae:abstract-to-real" >
143
+ <xslt:with-param name="caller" select="@id" />
144
+ <xslt:with-param name="is-a" select="@is-a" />
145
+ </xslt:call-template>
146
+
147
+ </xslt:template>
148
+
149
+
150
+
151
+ <!-- output everything else unchanged -->
152
+ <xslt:template match="*" priority="-1" mode="iae:go" >
153
+ <xslt:copy>
154
+ <xslt:copy-of select="@*" />
155
+ <xslt:apply-templates mode="iae:go"/>
156
+ </xslt:copy>
157
+ </xslt:template>
158
+
159
+ <!-- Templates for macro expansion of abstract patterns -->
160
+ <!-- Sets up the initial conditions for the recursive call -->
161
+ <xslt:template name="iae:macro-expand">
162
+ <xslt:param name="caller"/>
163
+ <xslt:param name="text" />
164
+ <xslt:call-template name="iae:multi-macro-expand">
165
+ <xslt:with-param name="caller" select="$caller"/>
166
+ <xslt:with-param name="text" select="$text"/>
167
+ <xslt:with-param name="paramNumber" select="1"/>
168
+ </xslt:call-template>
169
+
170
+ </xslt:template>
171
+
172
+ <!-- Template to replace the current parameter and then
173
+ recurse to replace subsequent parameters. -->
174
+
175
+ <xslt:template name="iae:multi-macro-expand">
176
+ <xslt:param name="caller"/>
177
+ <xslt:param name="text" />
178
+ <xslt:param name="paramNumber" />
179
+
180
+
181
+ <xslt:choose>
182
+ <xslt:when test="//iso:pattern[@id=$caller]/iso:param[ $paramNumber]">
183
+
184
+ <xslt:call-template name="iae:multi-macro-expand">
185
+ <xslt:with-param name="caller" select="$caller"/>
186
+ <xslt:with-param name="paramNumber" select="$paramNumber + 1"/>
187
+ <xslt:with-param name="text" >
188
+ <xslt:call-template name="iae:replace-substring">
189
+ <xslt:with-param name="original" select="$text"/>
190
+ <xslt:with-param name="substring"
191
+ select="concat('$', //iso:pattern[@id=$caller]/iso:param[ $paramNumber ]/@name)"/>
192
+ <xslt:with-param name="replacement"
193
+ select="//iso:pattern[@id=$caller]/iso:param[ $paramNumber ]/@value"/>
194
+ </xslt:call-template>
195
+ </xslt:with-param>
196
+ </xslt:call-template>
197
+ </xslt:when>
198
+ <xslt:otherwise><xslt:value-of select="$text" /></xslt:otherwise>
199
+
200
+ </xslt:choose>
201
+ </xslt:template>
202
+
203
+
204
+ <!-- generate the real pattern from an abstract pattern + parameters-->
205
+ <xslt:template name="iae:abstract-to-real" >
206
+ <xslt:param name="caller"/>
207
+ <xslt:param name="is-a" />
208
+ <xslt:for-each select="//iso:pattern[@id= $is-a]">
209
+ <xslt:copy>
210
+
211
+ <xslt:choose>
212
+ <xslt:when test=" string-length( $caller ) = 0">
213
+ <xslt:attribute name="id"><xslt:value-of select="concat( generate-id(.) , $is-a)" /></xslt:attribute>
214
+ </xslt:when>
215
+ <xslt:otherwise>
216
+ <xslt:attribute name="id"><xslt:value-of select="$caller" /></xslt:attribute>
217
+ </xslt:otherwise>
218
+ </xslt:choose>
219
+
220
+ <xslt:apply-templates select="*|text()" mode="iae:do-pattern" >
221
+ <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
222
+ </xslt:apply-templates>
223
+
224
+ </xslt:copy>
225
+ </xslt:for-each>
226
+ </xslt:template>
227
+
228
+
229
+ <!-- Generate a non-abstract pattern -->
230
+ <xslt:template mode="iae:do-pattern" match="*">
231
+ <xslt:param name="caller"/>
232
+ <xslt:copy>
233
+ <xslt:for-each select="@*[name()='test' or name()='context' or name()='select']">
234
+ <xslt:attribute name="{name()}">
235
+ <xslt:call-template name="iae:macro-expand">
236
+ <xslt:with-param name="text"><xslt:value-of select="."/></xslt:with-param>
237
+ <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
238
+ </xslt:call-template>
239
+ </xslt:attribute>
240
+ </xslt:for-each>
241
+ <xslt:copy-of select="@*[name()!='test'][name()!='context'][name()!='select']" />
242
+ <xsl:for-each select="node()">
243
+ <xsl:choose>
244
+ <!-- Experiment: replace macros in text as well, to allow parameterized assertions
245
+ and so on, without having to have spurious <iso:value-of> calls and multiple
246
+ delimiting -->
247
+ <xsl:when test="self::text()">
248
+ <xslt:call-template name="iae:macro-expand">
249
+ <xslt:with-param name="text"><xslt:value-of select="."/></xslt:with-param>
250
+ <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
251
+ </xslt:call-template>
252
+ </xsl:when>
253
+ <xsl:otherwise>
254
+ <xslt:apply-templates select="." mode="iae:do-pattern">
255
+ <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
256
+ </xslt:apply-templates>
257
+ </xsl:otherwise>
258
+ </xsl:choose>
259
+ </xsl:for-each>
260
+ </xslt:copy>
261
+ </xslt:template>
262
+
263
+ <!-- UTILITIES -->
264
+ <!-- Simple version of replace-substring function -->
265
+ <xslt:template name="iae:replace-substring">
266
+ <xslt:param name="original" />
267
+ <xslt:param name="substring" />
268
+ <xslt:param name="replacement" select="''"/>
269
+
270
+ <xsl:choose>
271
+ <xsl:when test="not($original)" />
272
+ <xsl:when test="not(string($substring))">
273
+ <xsl:value-of select="$original" />
274
+ </xsl:when>
275
+ <xsl:when test="contains($original, $substring)">
276
+ <xsl:variable name="before" select="substring-before($original, $substring)" />
277
+ <xsl:variable name="after" select="substring-after($original, $substring)" />
278
+
279
+ <xsl:value-of select="$before" />
280
+ <xsl:value-of select="$replacement" />
281
+ <!-- recursion -->
282
+ <xsl:call-template name="iae:replace-substring">
283
+ <xsl:with-param name="original" select="$after" />
284
+ <xsl:with-param name="substring" select="$substring" />
285
+ <xsl:with-param name="replacement" select="$replacement" />
286
+ </xsl:call-template>
287
+ </xsl:when>
288
+ <xsl:otherwise>
289
+ <!-- no substitution -->
290
+ <xsl:value-of select="$original" />
291
+ </xsl:otherwise>
292
+ </xsl:choose>
293
+ </xslt:template>
294
+
295
+ </xslt:stylesheet>