-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-xml.xpl
243 lines (206 loc) · 8.71 KB
/
validate-xml.xpl
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:cx="http://xmlcalabash.com/ns/extensions" xmlns:f="http://www.faustedition.net/ns"
xmlns:pxf="http://exproc.org/proposed/steps/file"
xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
xmlns:l="http://xproc.org/library" type="f:validate-xmls" name="main" version="1.0">
<p:option name="target" select="'../../../target/'"/>
<p:option name="_target" select="resolve-uri($target)"/>
<p:option name="xmlroot" select="'../../../data/xml/transcript'"></p:option>
<p:option name="_xmlroot" select="resolve-uri($xmlroot)"/>
<p:option name="rng" select="''"/>
<p:option name="schematron" select="''"/>
<p:option name="xsd" select="''"/>
<p:option name="report-name" select="'faust-tei'"/>
<p:option name="report-title" select="concat('Validation ', $report-name)"/>
<p:option name="linkroot"/>
<p:option name="exclude-filter" select="''"/>
<p:input port="source"><p:empty/></p:input>
<p:input port="parameters" kind="parameter"/>
<p:output port="result" primary="true" sequence="true"><p:empty/></p:output>
<p:serialization port="result" indent="true"/>
<p:import href="http://xproc.org/library/recursive-directory-list.xpl"/>
<p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
<p:variable name="errors-xml" select="resolve-uri(concat('report/', $report-name, '.xml'), $_target)"/>
<p:variable name="report" select="resolve-uri(concat('report/', $report-name, '.html'), $_target)"/>
<cx:message>
<p:with-option name="message" select="concat('=== ', $report-title, ' ===',
' target=', $target, '; resolved to ', $_target,
' xmlroot=', $xmlroot, '; resolved to ', $_xmlroot,
' rng=', $rng,
' xsd=', $xsd,
' schematron=', $schematron,
' report=', $report
)"></p:with-option>
</cx:message>
<p:load name="load-schema">
<p:with-option name="href" select="resolve-uri(if ($rng) then $rng else $xsd)"/>
</p:load>
<p:choose name="load-schematron">
<p:when test="$schematron != ''">
<p:output port="result"/>
<p:load>
<p:with-option name="href" select="resolve-uri($schematron)"/>
</p:load>
</p:when>
<p:otherwise>
<p:output port="result"/>
<p:identity><p:input port="source"><p:empty/></p:input></p:identity>
</p:otherwise>
</p:choose>
<l:recursive-directory-list>
<p:with-option name="path" select="$_xmlroot"/>
<p:with-option name="exclude-filter" select="$exclude-filter"/>
</l:recursive-directory-list>
<cx:message>
<p:with-option name="message" select="concat('Validating ', count(//c:file), ' files, this may take a while ...')"/>
</cx:message>
<p:for-each>
<p:iteration-source select="//c:file"/>
<p:variable name="filename" select="p:resolve-uri(/c:file/@name)"/>
<!-- <cx:message>
<p:with-option name="message" select="concat('Validating ', $filename)"></p:with-option>
</cx:message>
-->
<p:load>
<p:with-option name="href" select="$filename"/>
</p:load>
<p:try>
<p:group>
<p:choose>
<p:when test="$rng != ''">
<p:validate-with-relax-ng assert-valid="true">
<p:input port="schema">
<p:pipe port="result" step="load-schema"/>
</p:input>
</p:validate-with-relax-ng>
</p:when>
<p:otherwise>
<!-- <cx:message>
<p:with-option name="message" select="concat('Validating ', $filename, ' with ', $xsd)"/>
</cx:message>
--> <p:validate-with-xml-schema assert-valid="true">
<p:input port="schema">
<p:pipe port="result" step="load-schema"></p:pipe>
</p:input>
</p:validate-with-xml-schema>
</p:otherwise>
</p:choose>
<p:choose>
<p:when test="$schematron != ''">
<p:validate-with-schematron name="schematron" assert-valid="false">
<p:input port="schema"><p:pipe port="result" step="load-schematron"/></p:input>
</p:validate-with-schematron>
<!-- We don't need the original XML -> work with report -->
<p:identity>
<p:input port="source"><p:pipe port="report" step="schematron"/></p:input>
</p:identity>
<p:choose>
<p:when test="//svrl:failed-assert">
<p:wrap wrapper="f:validation-error" match="/"/>
</p:when>
<p:otherwise>
<p:identity><p:input port="source"><p:inline><f:valid-document/></p:inline></p:input></p:identity>
</p:otherwise>
</p:choose>
</p:when>
<p:otherwise>
<p:identity><p:input port="source"><p:inline><f:valid-document/></p:inline></p:input></p:identity>
</p:otherwise>
</p:choose>
</p:group>
<p:catch name="validate-catch">
<!-- Work on the actual error document -->
<p:identity>
<p:input port="source"><p:pipe port="error" step="validate-catch"/></p:input>
</p:identity>
<!-- Markup the (plain-text) jing error message -->
<p:xslt>
<p:with-param name="filename" select="$filename"/>
<p:input port="stylesheet"><p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="filename"/>
<xsl:template match="/">
<xsl:variable name="all-errors">
<xsl:apply-templates select="//c:error"/>
</xsl:variable>
<f:validation-error filename="{$filename}">
<xsl:sequence select="$all-errors//c:error[not(@systemId) or @systemId = $filename]"/>
</f:validation-error>
</xsl:template>
<xsl:template match="c:error">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:analyze-string select="." regex="[^;]+; systemId: ([^;]+); lineNumber: \d+; columnNumber: \d+; ([^;]+?)([;.] (.+))?$">
<xsl:matching-substring>
<xsl:attribute name="systemId" select="regex-group(1)"/>
<c:message><xsl:value-of select="regex-group(2)"/></c:message>
<c:resolution><xsl:value-of select="regex-group(4)"/></c:resolution>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates mode="#current" select="@*, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</p:inline></p:input>
</p:xslt>
<!-- <cx:message>
<p:with-option name="message" select="concat('VALIDATION ERROR:', $filename, ':', string-join(distinct-values(//c:message), '; '))"/>
</cx:message>
-->
</p:catch>
</p:try>
<p:add-attribute attribute-name="filename" match="/*">
<p:with-option name="attribute-value" select="$filename"/>
</p:add-attribute>
</p:for-each>
<cx:message message="Writing aggregate report ..."/>
<p:wrap-sequence wrapper="validation-errors" name="wrap-errors-0"/>
<!-- drag ns decl up so serialization gets MUCH smaller -->
<p:xslt name="wrap-errors">
<p:input port="stylesheet">
<p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/*">
<xsl:copy>
<xsl:namespace name="f">http://www.faustedition.net/ns</xsl:namespace>
<xsl:namespace name="c">http://www.w3.org/ns/xproc-step</xsl:namespace>
<xsl:namespace name="err">http://www.w3.org/ns/xproc-error</xsl:namespace>
<xsl:namespace name="cx">http://xmlcalabash.com/ns/extensions</xsl:namespace>
<xsl:namespace name="pxf">http://exproc.org/proposed/steps/file</xsl:namespace>
<xsl:namespace name="svrl">http://purl.oclc.org/dsdl/svrl</xsl:namespace>
<xsl:namespace name="l">http://xproc.org/library</xsl:namespace>
<xsl:copy-of select="@*, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</p:inline>
</p:input>
</p:xslt>
<cx:message>
<p:with-option name="message" select="concat('Saving errors to ', $errors-xml)"/>
</cx:message>
<p:store name="store-errors" indent="true">
<p:with-option name="href" select="$errors-xml"/>
</p:store>
<p:in-scope-names name="in-scope-names"/>
<p:xslt>
<p:input port="source"><p:pipe port="result" step="wrap-errors"/></p:input>
<p:input port="stylesheet"><p:document href="validation-report.xsl"/></p:input>
<p:input port="parameters"><p:pipe port="result" step="in-scope-names"/></p:input>
<p:with-param name="linkroot" select="resolve-uri('report/xml', $_target)"/>
</p:xslt>
<cx:message>
<p:with-option name="message" select="concat('Storing report to ', $report)"/>
</cx:message>
<p:store method="xhtml" indent="true">
<p:with-option name="href" select="$report"/>
</p:store>
</p:declare-step>