Schematron validation and uniqueness
I'm trying to write some Schematron rules and one of them is supposed to check, if elements are unique in the scope of parent element. So I have an example xml structure:
<abc>
<elem id="qw0">
<a>1</a>
<a>2</a>
<a>3</a>
</elem>
<elem id="qw1">
<a>1</a>
<a>2</a>
<a>3</a>
<a>3</a>
</elem>
</abc>
My rule should check if each of the element's "a" elements is unique. In this specific example, for elem with id="qw1" there are two elements "a" with value "3". This should not be allowed.
So far I've come to this kind of rule:
<iso:pattern id="doc.abc">
<iso:title>checking ABC</iso:title>
<开发者_如何学运维iso:rule context="elem">
<iso:assert test="count(a[. = current()]) = 1">TACs should be unique.</iso:assert>
</iso:rule>
</iso:pattern>
But this does not work, as it kind of looks through the whole document, not just the direct children of elem.
If you are using a Schematron processor with an underlying XSLT/XPath 2.0 engine, and want to make the context of the rule the <elem> element you could use:
<sch:pattern> <sch:rule context="elem"> <sch:report test="count(a) != count(distinct-values(a))"> Values not distinct</sch:report> </sch:rule> </sch:pattern>
I found out that this may be solved with the following rule:
<iso:pattern id="doc.abc">
<iso:title>checking ABC</iso:title>
<iso:rule context="a">
<iso:assert test="count(parent::node()/a[. = current()) = 1">TACs should be unique.</iso:assert>
</iso:rule>
</iso:pattern>
But this fires up the rule for every a element.
It would be more elegant to fire it for every elem, no a.
精彩评论