cda-schematron-validator 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jshintrc +96 -96
- package/.vscode/launch.json +17 -17
- package/README.md +132 -132
- package/includeExternalDocument.js +108 -108
- package/package.json +4 -10
- package/parseSchematron.js +121 -121
- package/test/test.sch +46 -46
- package/test/test.xml +79 -79
- package/test/tests.js +141 -141
- package/testAssertion.js +72 -72
- package/validator.js +233 -228
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
// jshint node:true
|
|
2
|
-
// jshint shadow:true
|
|
3
|
-
module.exports = modifyTest;
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const xpath = require('xpath');
|
|
7
|
-
const dom = require('@xmldom/xmldom').DOMParser;
|
|
8
|
-
const path_module = require('path');
|
|
9
|
-
|
|
10
|
-
let loadedExternalDocuments = Object.create({});
|
|
11
|
-
|
|
12
|
-
function modifyTest(test, resourceDir) {
|
|
13
|
-
|
|
14
|
-
let matches = /=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
15
|
-
while (matches) {
|
|
16
|
-
|
|
17
|
-
// String processing to select the non-regular predicate expression
|
|
18
|
-
let equalInd = test.indexOf(matches[0]);
|
|
19
|
-
let start = equalInd;
|
|
20
|
-
let bracketDepth = 0;
|
|
21
|
-
for (let i = equalInd; i >= 0; i--) {
|
|
22
|
-
if (!bracketDepth && (test[i] === '[' || test[i] === ' ')) {
|
|
23
|
-
start = i + 1;
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
if (test[i] === ']') {
|
|
27
|
-
bracketDepth++;
|
|
28
|
-
}
|
|
29
|
-
else if (test[i] === '[') {
|
|
30
|
-
bracketDepth--;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let end = test.length;
|
|
35
|
-
bracketDepth = 0;
|
|
36
|
-
for (let i = start + matches[0].length; i < test.length; i++) {
|
|
37
|
-
if (!bracketDepth && (test[i] === ']' || test[i] === ' ')) {
|
|
38
|
-
end = i;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
if (test[i] === '[') {
|
|
42
|
-
bracketDepth++;
|
|
43
|
-
}
|
|
44
|
-
else if (test[i] === ']') {
|
|
45
|
-
bracketDepth--;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let predicate = test.slice(start, end);
|
|
50
|
-
|
|
51
|
-
// Load external doc (load from "cache" if already loaded)
|
|
52
|
-
let filepath = matches[1].slice(1, -1);
|
|
53
|
-
let externalDoc;
|
|
54
|
-
if (!loadedExternalDocuments[filepath]) {
|
|
55
|
-
let externalXml = null;
|
|
56
|
-
try {
|
|
57
|
-
externalXml = fs.readFileSync(path_module.join(resourceDir, filepath), 'utf-8').toString();
|
|
58
|
-
}
|
|
59
|
-
catch (err) {
|
|
60
|
-
throw new Error('No such file \'' + filepath + '\'');
|
|
61
|
-
}
|
|
62
|
-
externalDoc = new dom().parseFromString(externalXml);
|
|
63
|
-
loadedExternalDocuments[filepath] = externalDoc;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
externalDoc = loadedExternalDocuments[filepath];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
let externalXpath = test.slice(equalInd + matches[0].length, end);
|
|
70
|
-
|
|
71
|
-
// Extract namespaces
|
|
72
|
-
let defaultNamespaceKey = /([^(<>.\/)]+):[^(<>.\/)]+/.exec(externalXpath)[1];
|
|
73
|
-
let externalNamespaceMap = externalDoc.lastChild._nsMap;
|
|
74
|
-
let namespaceMap = {};
|
|
75
|
-
for (let key in externalNamespaceMap) {
|
|
76
|
-
if (externalNamespaceMap.hasOwnProperty(key)) {
|
|
77
|
-
if (key) {
|
|
78
|
-
namespaceMap[key] = externalNamespaceMap[key];
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
namespaceMap[defaultNamespaceKey] = externalNamespaceMap[''];
|
|
83
|
-
|
|
84
|
-
let externalSelect = xpath.useNamespaces(namespaceMap);
|
|
85
|
-
|
|
86
|
-
// Create new predicate from extract values
|
|
87
|
-
let values = [];
|
|
88
|
-
let externalResults = externalSelect(externalXpath, externalDoc);
|
|
89
|
-
for (let i = 0; i < externalResults.length; i++) {
|
|
90
|
-
values.push(externalResults[i].value);
|
|
91
|
-
}
|
|
92
|
-
let lhv = predicate.slice(0, predicate.indexOf('=document('));
|
|
93
|
-
let newPredicate = '(';
|
|
94
|
-
for (let i = 0; i < values.length; i++) {
|
|
95
|
-
newPredicate += lhv + '=\'' + values[i] + '\'';
|
|
96
|
-
if (i < values.length - 1) {
|
|
97
|
-
newPredicate += ' or ';
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
newPredicate += ')';
|
|
101
|
-
|
|
102
|
-
// Replace test
|
|
103
|
-
test = test.slice(0, start) + newPredicate + test.slice(end);
|
|
104
|
-
|
|
105
|
-
matches = /@[^\[\]]+=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return test;
|
|
1
|
+
// jshint node:true
|
|
2
|
+
// jshint shadow:true
|
|
3
|
+
module.exports = modifyTest;
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const xpath = require('xpath');
|
|
7
|
+
const dom = require('@xmldom/xmldom').DOMParser;
|
|
8
|
+
const path_module = require('path');
|
|
9
|
+
|
|
10
|
+
let loadedExternalDocuments = Object.create({});
|
|
11
|
+
|
|
12
|
+
function modifyTest(test, resourceDir) {
|
|
13
|
+
|
|
14
|
+
let matches = /=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
15
|
+
while (matches) {
|
|
16
|
+
|
|
17
|
+
// String processing to select the non-regular predicate expression
|
|
18
|
+
let equalInd = test.indexOf(matches[0]);
|
|
19
|
+
let start = equalInd;
|
|
20
|
+
let bracketDepth = 0;
|
|
21
|
+
for (let i = equalInd; i >= 0; i--) {
|
|
22
|
+
if (!bracketDepth && (test[i] === '[' || test[i] === ' ')) {
|
|
23
|
+
start = i + 1;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
if (test[i] === ']') {
|
|
27
|
+
bracketDepth++;
|
|
28
|
+
}
|
|
29
|
+
else if (test[i] === '[') {
|
|
30
|
+
bracketDepth--;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let end = test.length;
|
|
35
|
+
bracketDepth = 0;
|
|
36
|
+
for (let i = start + matches[0].length; i < test.length; i++) {
|
|
37
|
+
if (!bracketDepth && (test[i] === ']' || test[i] === ' ')) {
|
|
38
|
+
end = i;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
if (test[i] === '[') {
|
|
42
|
+
bracketDepth++;
|
|
43
|
+
}
|
|
44
|
+
else if (test[i] === ']') {
|
|
45
|
+
bracketDepth--;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let predicate = test.slice(start, end);
|
|
50
|
+
|
|
51
|
+
// Load external doc (load from "cache" if already loaded)
|
|
52
|
+
let filepath = matches[1].slice(1, -1);
|
|
53
|
+
let externalDoc;
|
|
54
|
+
if (!loadedExternalDocuments[filepath]) {
|
|
55
|
+
let externalXml = null;
|
|
56
|
+
try {
|
|
57
|
+
externalXml = fs.readFileSync(path_module.join(resourceDir, filepath), 'utf-8').toString();
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
throw new Error('No such file \'' + filepath + '\'');
|
|
61
|
+
}
|
|
62
|
+
externalDoc = new dom().parseFromString(externalXml);
|
|
63
|
+
loadedExternalDocuments[filepath] = externalDoc;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
externalDoc = loadedExternalDocuments[filepath];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let externalXpath = test.slice(equalInd + matches[0].length, end);
|
|
70
|
+
|
|
71
|
+
// Extract namespaces
|
|
72
|
+
let defaultNamespaceKey = /([^(<>.\/)]+):[^(<>.\/)]+/.exec(externalXpath)[1];
|
|
73
|
+
let externalNamespaceMap = externalDoc.lastChild._nsMap;
|
|
74
|
+
let namespaceMap = {};
|
|
75
|
+
for (let key in externalNamespaceMap) {
|
|
76
|
+
if (externalNamespaceMap.hasOwnProperty(key)) {
|
|
77
|
+
if (key) {
|
|
78
|
+
namespaceMap[key] = externalNamespaceMap[key];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
namespaceMap[defaultNamespaceKey] = externalNamespaceMap[''];
|
|
83
|
+
|
|
84
|
+
let externalSelect = xpath.useNamespaces(namespaceMap);
|
|
85
|
+
|
|
86
|
+
// Create new predicate from extract values
|
|
87
|
+
let values = [];
|
|
88
|
+
let externalResults = externalSelect(externalXpath, externalDoc);
|
|
89
|
+
for (let i = 0; i < externalResults.length; i++) {
|
|
90
|
+
values.push(externalResults[i].value);
|
|
91
|
+
}
|
|
92
|
+
let lhv = predicate.slice(0, predicate.indexOf('=document('));
|
|
93
|
+
let newPredicate = '(';
|
|
94
|
+
for (let i = 0; i < values.length; i++) {
|
|
95
|
+
newPredicate += lhv + '=\'' + values[i] + '\'';
|
|
96
|
+
if (i < values.length - 1) {
|
|
97
|
+
newPredicate += ' or ';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
newPredicate += ')';
|
|
101
|
+
|
|
102
|
+
// Replace test
|
|
103
|
+
test = test.slice(0, start) + newPredicate + test.slice(end);
|
|
104
|
+
|
|
105
|
+
matches = /@[^\[\]]+=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return test;
|
|
109
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cda-schematron-validator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Fork of Eric Wadkins' javascript implementation of schematron testing for C-CDA XML documents. This includes bug fixes and some house keeping.",
|
|
5
5
|
"main": "validator.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,15 +12,8 @@
|
|
|
12
12
|
},
|
|
13
13
|
"author": "Eric Wadkins",
|
|
14
14
|
"contributors": [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"url" : "http://www.ericwadkins.com/"
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"name" : "Priyaranjan (Raj) Tokachichu",
|
|
21
|
-
"email" : "priyaranjan@priyaranjan.com",
|
|
22
|
-
"url" : "https://github.com/priyaranjan-tokachichu"
|
|
23
|
-
}
|
|
15
|
+
"Eric Wadkins (http://www.ericwadkins.com/)",
|
|
16
|
+
"Priyaranjan <priyaranjan@priyaranjan.com> (Raj)"
|
|
24
17
|
],
|
|
25
18
|
"license": "MIT",
|
|
26
19
|
"bugs": {
|
|
@@ -36,6 +29,7 @@
|
|
|
36
29
|
},
|
|
37
30
|
"dependencies": {
|
|
38
31
|
"@xmldom/xmldom": "~0.7.2",
|
|
32
|
+
"lodash.get": "~4.4.2",
|
|
39
33
|
"xpath": "~0.0.32"
|
|
40
34
|
},
|
|
41
35
|
"directories": {
|
package/parseSchematron.js
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
// jshint node:true
|
|
2
|
-
// jshint shadow:true
|
|
3
|
-
module.exports = parseSchematron;
|
|
4
|
-
|
|
5
|
-
const xpath = require('xpath');
|
|
6
|
-
|
|
7
|
-
function parseSchematron(doc) {
|
|
8
|
-
// Extract data from schematron
|
|
9
|
-
let schematronData = extract(doc);
|
|
10
|
-
|
|
11
|
-
return schematronData;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function extract(doc) {
|
|
15
|
-
let namespaceMap = Object.create({});
|
|
16
|
-
let patternLevelMap = Object.create({});
|
|
17
|
-
let patternRuleMap = Object.create({});
|
|
18
|
-
let ruleAssertionMap = Object.create({});
|
|
19
|
-
|
|
20
|
-
//// Namespace mapping
|
|
21
|
-
let namespaces = xpath.select('//*[local-name()="ns"]', doc);
|
|
22
|
-
for (let i = 0; i < namespaces.length; i++) {
|
|
23
|
-
namespaceMap[namespaces[i].getAttribute('prefix')] = namespaces[i].getAttribute('uri');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//// Pattern to level mapping
|
|
27
|
-
|
|
28
|
-
// Find errors phases
|
|
29
|
-
let errorPhase = xpath.select('//*[local-name()="phase" and @id="errors"]', doc);
|
|
30
|
-
|
|
31
|
-
// Store error patterns
|
|
32
|
-
if (errorPhase.length) {
|
|
33
|
-
for (let i = 0; i < errorPhase[0].childNodes.length; i++) {
|
|
34
|
-
if (errorPhase[0].childNodes[i].localName === 'active') {
|
|
35
|
-
patternLevelMap[errorPhase[0].childNodes[i].getAttribute('pattern')] = 'error';
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Find errors phases
|
|
41
|
-
let warningPhase = xpath.select('//*[local-name()="phase" and @id="warnings"]', doc);
|
|
42
|
-
|
|
43
|
-
// Store warning patterns
|
|
44
|
-
if (warningPhase.length) {
|
|
45
|
-
for (let i = 0; i < warningPhase[0].childNodes.length; i++) {
|
|
46
|
-
if (warningPhase[0].childNodes[i].localName === 'active') {
|
|
47
|
-
patternLevelMap[warningPhase[0].childNodes[i].getAttribute('pattern')] = 'warning';
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//// Pattern to rule and rule to assertion mapping
|
|
53
|
-
|
|
54
|
-
// Find patterns
|
|
55
|
-
let patterns = xpath.select('//*[local-name()="pattern"]', doc);
|
|
56
|
-
|
|
57
|
-
// Map patterns to rules
|
|
58
|
-
for (let i = 0; i < patterns.length; i++) {
|
|
59
|
-
let patternId = patterns[i].getAttribute('id');
|
|
60
|
-
let defaultLevel = patternLevelMap[patternId] || 'warning';
|
|
61
|
-
patternRuleMap[patternId] = [];
|
|
62
|
-
let rules = xpath.select('./*[local-name()="rule"]', patterns[i]);
|
|
63
|
-
for (let j = 0; j < rules.length; j++) {
|
|
64
|
-
patternRuleMap[patternId].push(rules[j].getAttribute('id'));
|
|
65
|
-
ruleAssertionMap[rules[j].getAttribute('id')] = {
|
|
66
|
-
abstract: parseAbstract(rules[j].getAttribute('abstract')),
|
|
67
|
-
context: parseContext(rules[j].getAttribute('context')),
|
|
68
|
-
assertionsAndExtensions: getAssertionsAndExtensions(rules[j], defaultLevel)
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
namespaceMap: namespaceMap,
|
|
75
|
-
patternRuleMap: patternRuleMap,
|
|
76
|
-
ruleAssertionMap: ruleAssertionMap
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
81
|
-
let assertionsAndExtensions = [];
|
|
82
|
-
|
|
83
|
-
// Find and store assertions
|
|
84
|
-
let assertions = xpath.select('./*[local-name()="assert"]', rule);
|
|
85
|
-
for (let i = 0; i < assertions.length; i++) {
|
|
86
|
-
let description = assertions[i].childNodes[0] ? assertions[i].childNodes[0].data : '';
|
|
87
|
-
let level = defaultLevel;
|
|
88
|
-
if (description.indexOf('SHALL') !== -1
|
|
89
|
-
&& (description.indexOf('SHOULD') === -1 || description.indexOf('SHALL') < description.indexOf('SHOULD'))) {
|
|
90
|
-
level = 'error';
|
|
91
|
-
}
|
|
92
|
-
assertionsAndExtensions.push({
|
|
93
|
-
type: 'assertion',
|
|
94
|
-
level: level,
|
|
95
|
-
id: assertions[i].getAttribute('id'),
|
|
96
|
-
test: assertions[i].getAttribute('test'),
|
|
97
|
-
description: description
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Find and store extensions
|
|
102
|
-
let extensions = xpath.select('./*[local-name()="extends"]', rule);
|
|
103
|
-
for (let i = 0; i < extensions.length; i++) {
|
|
104
|
-
assertionsAndExtensions.push({
|
|
105
|
-
type: 'extension',
|
|
106
|
-
rule: extensions[i].getAttribute('rule'),
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return assertionsAndExtensions;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function parseAbstract(str) {
|
|
114
|
-
if (str === 'true' || str === 'yes') {
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function parseContext(str) {
|
|
121
|
-
return str || null;
|
|
1
|
+
// jshint node:true
|
|
2
|
+
// jshint shadow:true
|
|
3
|
+
module.exports = parseSchematron;
|
|
4
|
+
|
|
5
|
+
const xpath = require('xpath');
|
|
6
|
+
|
|
7
|
+
function parseSchematron(doc) {
|
|
8
|
+
// Extract data from schematron
|
|
9
|
+
let schematronData = extract(doc);
|
|
10
|
+
|
|
11
|
+
return schematronData;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function extract(doc) {
|
|
15
|
+
let namespaceMap = Object.create({});
|
|
16
|
+
let patternLevelMap = Object.create({});
|
|
17
|
+
let patternRuleMap = Object.create({});
|
|
18
|
+
let ruleAssertionMap = Object.create({});
|
|
19
|
+
|
|
20
|
+
//// Namespace mapping
|
|
21
|
+
let namespaces = xpath.select('//*[local-name()="ns"]', doc);
|
|
22
|
+
for (let i = 0; i < namespaces.length; i++) {
|
|
23
|
+
namespaceMap[namespaces[i].getAttribute('prefix')] = namespaces[i].getAttribute('uri');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//// Pattern to level mapping
|
|
27
|
+
|
|
28
|
+
// Find errors phases
|
|
29
|
+
let errorPhase = xpath.select('//*[local-name()="phase" and @id="errors"]', doc);
|
|
30
|
+
|
|
31
|
+
// Store error patterns
|
|
32
|
+
if (errorPhase.length) {
|
|
33
|
+
for (let i = 0; i < errorPhase[0].childNodes.length; i++) {
|
|
34
|
+
if (errorPhase[0].childNodes[i].localName === 'active') {
|
|
35
|
+
patternLevelMap[errorPhase[0].childNodes[i].getAttribute('pattern')] = 'error';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Find errors phases
|
|
41
|
+
let warningPhase = xpath.select('//*[local-name()="phase" and @id="warnings"]', doc);
|
|
42
|
+
|
|
43
|
+
// Store warning patterns
|
|
44
|
+
if (warningPhase.length) {
|
|
45
|
+
for (let i = 0; i < warningPhase[0].childNodes.length; i++) {
|
|
46
|
+
if (warningPhase[0].childNodes[i].localName === 'active') {
|
|
47
|
+
patternLevelMap[warningPhase[0].childNodes[i].getAttribute('pattern')] = 'warning';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
//// Pattern to rule and rule to assertion mapping
|
|
53
|
+
|
|
54
|
+
// Find patterns
|
|
55
|
+
let patterns = xpath.select('//*[local-name()="pattern"]', doc);
|
|
56
|
+
|
|
57
|
+
// Map patterns to rules
|
|
58
|
+
for (let i = 0; i < patterns.length; i++) {
|
|
59
|
+
let patternId = patterns[i].getAttribute('id');
|
|
60
|
+
let defaultLevel = patternLevelMap[patternId] || 'warning';
|
|
61
|
+
patternRuleMap[patternId] = [];
|
|
62
|
+
let rules = xpath.select('./*[local-name()="rule"]', patterns[i]);
|
|
63
|
+
for (let j = 0; j < rules.length; j++) {
|
|
64
|
+
patternRuleMap[patternId].push(rules[j].getAttribute('id'));
|
|
65
|
+
ruleAssertionMap[rules[j].getAttribute('id')] = {
|
|
66
|
+
abstract: parseAbstract(rules[j].getAttribute('abstract')),
|
|
67
|
+
context: parseContext(rules[j].getAttribute('context')),
|
|
68
|
+
assertionsAndExtensions: getAssertionsAndExtensions(rules[j], defaultLevel)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
namespaceMap: namespaceMap,
|
|
75
|
+
patternRuleMap: patternRuleMap,
|
|
76
|
+
ruleAssertionMap: ruleAssertionMap
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
81
|
+
let assertionsAndExtensions = [];
|
|
82
|
+
|
|
83
|
+
// Find and store assertions
|
|
84
|
+
let assertions = xpath.select('./*[local-name()="assert"]', rule);
|
|
85
|
+
for (let i = 0; i < assertions.length; i++) {
|
|
86
|
+
let description = assertions[i].childNodes[0] ? assertions[i].childNodes[0].data : '';
|
|
87
|
+
let level = defaultLevel;
|
|
88
|
+
if (description.indexOf('SHALL') !== -1
|
|
89
|
+
&& (description.indexOf('SHOULD') === -1 || description.indexOf('SHALL') < description.indexOf('SHOULD'))) {
|
|
90
|
+
level = 'error';
|
|
91
|
+
}
|
|
92
|
+
assertionsAndExtensions.push({
|
|
93
|
+
type: 'assertion',
|
|
94
|
+
level: level,
|
|
95
|
+
id: assertions[i].getAttribute('id'),
|
|
96
|
+
test: assertions[i].getAttribute('test'),
|
|
97
|
+
description: description
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Find and store extensions
|
|
102
|
+
let extensions = xpath.select('./*[local-name()="extends"]', rule);
|
|
103
|
+
for (let i = 0; i < extensions.length; i++) {
|
|
104
|
+
assertionsAndExtensions.push({
|
|
105
|
+
type: 'extension',
|
|
106
|
+
rule: extensions[i].getAttribute('rule'),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return assertionsAndExtensions;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function parseAbstract(str) {
|
|
114
|
+
if (str === 'true' || str === 'yes') {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function parseContext(str) {
|
|
121
|
+
return str || null;
|
|
122
122
|
}
|
package/test/test.sch
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<sch:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
|
|
3
|
-
<sch:ns prefix="dh" uri="urn:dh" />
|
|
4
|
-
<sch:phase id="errors">
|
|
5
|
-
<sch:active pattern="A" />
|
|
6
|
-
<sch:active pattern="B" />
|
|
7
|
-
<sch:active pattern="C" />
|
|
8
|
-
<sch:active pattern="E" />
|
|
9
|
-
</sch:phase>
|
|
10
|
-
<sch:phase id="warnings">
|
|
11
|
-
<sch:active pattern="D" />
|
|
12
|
-
</sch:phase>
|
|
13
|
-
<sch:pattern id="A">
|
|
14
|
-
<sch:rule id="A-rule" context="dh:test[@root='A']">
|
|
15
|
-
<sch:assert id="A-assert" test="count(dh:element)=1">Should only have one element</sch:assert>
|
|
16
|
-
</sch:rule>
|
|
17
|
-
</sch:pattern>
|
|
18
|
-
<sch:pattern id="B">
|
|
19
|
-
<sch:rule id="B-rule-1" context="dh:test[@root]">
|
|
20
|
-
<sch:assert id="B-assert-2" test="count(dh:element)=1">Each section should have 1 element</sch:assert>
|
|
21
|
-
</sch:rule>
|
|
22
|
-
<sch:rule id="B-rule-2">
|
|
23
|
-
<sch:assert id="B-assert-1" test="count(//dh:element)=18">Should have 18 elements total element</sch:assert>
|
|
24
|
-
</sch:rule>
|
|
25
|
-
</sch:pattern>
|
|
26
|
-
<sch:pattern id="C">
|
|
27
|
-
<sch:rule id="C-rule-1">
|
|
28
|
-
<sch:assert id="C-assert-1" test="//dh:element[@attribute]">Each element should have the attribute attribute</sch:assert>
|
|
29
|
-
</sch:rule>
|
|
30
|
-
<sch:rule id="C-rule-2" context="dh:test[@root]">
|
|
31
|
-
<sch:assert id="C-assert-2" test="dh:element[@attribute]">Each element should have the attribute attribute</sch:assert>
|
|
32
|
-
</sch:rule>
|
|
33
|
-
</sch:pattern>
|
|
34
|
-
<sch:pattern id="D">
|
|
35
|
-
<sch:rule id="D-rule-1">
|
|
36
|
-
<sch:assert id="D-assert-1" test="//dh:element[count(dh:child)=1]">Each element should have the attribute attribute</sch:assert>
|
|
37
|
-
</sch:rule>
|
|
38
|
-
<sch:rule id="D-rule-2" context="dh:test[@root]">
|
|
39
|
-
<sch:assert id="D-assert-2" test="dh:element[count(dh:child)=1]">Each element should have the attribute attribute</sch:assert>
|
|
40
|
-
</sch:rule>
|
|
41
|
-
</sch:pattern>
|
|
42
|
-
<sch:pattern id="E">
|
|
43
|
-
<sch:rule id="E-rule-1">
|
|
44
|
-
<sch:assert id="E-assert-1" test="not (dh:element[count(dh:child)=1])">This is an invalid test</sch:assert>
|
|
45
|
-
</sch:rule>
|
|
46
|
-
</sch:pattern>
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<sch:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
|
|
3
|
+
<sch:ns prefix="dh" uri="urn:dh" />
|
|
4
|
+
<sch:phase id="errors">
|
|
5
|
+
<sch:active pattern="A" />
|
|
6
|
+
<sch:active pattern="B" />
|
|
7
|
+
<sch:active pattern="C" />
|
|
8
|
+
<sch:active pattern="E" />
|
|
9
|
+
</sch:phase>
|
|
10
|
+
<sch:phase id="warnings">
|
|
11
|
+
<sch:active pattern="D" />
|
|
12
|
+
</sch:phase>
|
|
13
|
+
<sch:pattern id="A">
|
|
14
|
+
<sch:rule id="A-rule" context="dh:test[@root='A']">
|
|
15
|
+
<sch:assert id="A-assert" test="count(dh:element)=1">Should only have one element</sch:assert>
|
|
16
|
+
</sch:rule>
|
|
17
|
+
</sch:pattern>
|
|
18
|
+
<sch:pattern id="B">
|
|
19
|
+
<sch:rule id="B-rule-1" context="dh:test[@root]">
|
|
20
|
+
<sch:assert id="B-assert-2" test="count(dh:element)=1">Each section should have 1 element</sch:assert>
|
|
21
|
+
</sch:rule>
|
|
22
|
+
<sch:rule id="B-rule-2">
|
|
23
|
+
<sch:assert id="B-assert-1" test="count(//dh:element)=18">Should have 18 elements total element</sch:assert>
|
|
24
|
+
</sch:rule>
|
|
25
|
+
</sch:pattern>
|
|
26
|
+
<sch:pattern id="C">
|
|
27
|
+
<sch:rule id="C-rule-1">
|
|
28
|
+
<sch:assert id="C-assert-1" test="//dh:element[@attribute]">Each element should have the attribute attribute</sch:assert>
|
|
29
|
+
</sch:rule>
|
|
30
|
+
<sch:rule id="C-rule-2" context="dh:test[@root]">
|
|
31
|
+
<sch:assert id="C-assert-2" test="dh:element[@attribute]">Each element should have the attribute attribute</sch:assert>
|
|
32
|
+
</sch:rule>
|
|
33
|
+
</sch:pattern>
|
|
34
|
+
<sch:pattern id="D">
|
|
35
|
+
<sch:rule id="D-rule-1">
|
|
36
|
+
<sch:assert id="D-assert-1" test="//dh:element[count(dh:child)=1]">Each element should have the attribute attribute</sch:assert>
|
|
37
|
+
</sch:rule>
|
|
38
|
+
<sch:rule id="D-rule-2" context="dh:test[@root]">
|
|
39
|
+
<sch:assert id="D-assert-2" test="dh:element[count(dh:child)=1]">Each element should have the attribute attribute</sch:assert>
|
|
40
|
+
</sch:rule>
|
|
41
|
+
</sch:pattern>
|
|
42
|
+
<sch:pattern id="E">
|
|
43
|
+
<sch:rule id="E-rule-1">
|
|
44
|
+
<sch:assert id="E-assert-1" test="not (dh:element[count(dh:child)=1])">This is an invalid test</sch:assert>
|
|
45
|
+
</sch:rule>
|
|
46
|
+
</sch:pattern>
|
|
47
47
|
</sch:schema>
|