Vote Count: For the instance document to be
valid, the sum of the Candidate values must be 100.
Different Type of Checking
Note that the checking needed for the Vote Count is a fundamentally different
type of checking than we did in the tutorial on co-constraint checking or the
tutorial on cardinality checking.
There we "compared data values", and "checked existence of", respectively. Here the data is validated by applying an algorithm.
Rule
Here is a Schematron rule which implements the Vote Count algorithm:
<sch:rule context="ElectionResultsByPercentage">
<sch:assert test="sum(Candidate) = 100">
The sum of the election results must be 100
</sch:assert>
</sch:rule>
Read as: Within the context of the <ElectionResultsByPercentage> element (the root element),
I assert that the sum of all the <Candidate> values must be 100.
Pattern
The rule on the previous slide is the only rule needed to implement the Vote Count, so
let's embed it within a pattern element:
<sch:pattern name="Vote Count">
<sch:p>The election results must add up to 100%.</sch:p>
Validate this (valid) XML instance document against the Schematron schema:
valid-document.xml (example03 folder)
Validate this (invalid) XML instance document against the Schematron schema:
invalid-document.xml (example03 folder)
Example: Checksum Validation
Consider this XML instance document:
<xml version="1.0"?>
<Election>
<ResultsByPercentage>
<Candidate name="John">61</Candidate>
<Candidate name="Sara">24</Candidate>
<Candidate name="Bill">15</Candidate>
</ResultsByPercentage>
<DocumentNumber>4389023232</DocumentNumber>
</Election>
Vote Count & Checksum: For the instance document to be
valid, the sum of the Candidate values must be 100, and the DocumentNumber's checksum digit
must equal the sum of the preceding nine digits multiplied by their position, mod 9.
Checksum
Here's how to validate the checksum digit:
<sch:pattern name="Checksum">
<sch:rule context="DocumentNumber">
<sch:assert test="(
(substring(.,1,1) * 1) +
(substring(.,2,1) * 2) +
(substring(.,3,1) * 3) +
(substring(.,4,1) * 4) +
(substring(.,5,1) * 5) +
(substring(.,6,1) * 6) +
(substring(.,7,1) * 7) +
(substring(.,8,1) * 8) +
(substring(.,9,1) * 9)
) mod 9 = substring(.,10,1)">
The checksum (i.e. the tenth digit) must equal the sum of the preceding nine digits multiplied by their position, mod 9.
Currently check-election-results.sch (lab03 folder)
assumes that all election results are integers, with no roundoff errors, and must add up to exactly 100.
Revise it so that the election results can be decimal values, and the sum of the results are acceptable
if it is 100 ±0.9
Validate valid-document.xml (lab03 folder)
against your Schematron schema.