Schematron: Check for unique attribute and element values

Based on customer feedback I want to post some solutions for two very common Schematron checks. The first rule is an assertion to report if all values of an attribute are unique for all elements (within the same parent). Simply open XML ValidatorBuddy, create a new Schematron schema and add the following rule in the editor:

<iso:pattern id="unique name attributes">
 <iso:rule context="people">
    <iso:assert test="count(person) = count(person[not(@name=preceding-sibling::person/@name)])">
      Not all name attributes of the person elements are unique

The second one is a check to see if all element values of a certain element (within the same parent) are unique. Add the following rule and apply it using the built-in Schematron XML validator in XML ValidatorBuddy:

<iso:pattern id="unique drawers">
  <iso:rule context="shelf">
    <iso:report test="count(drawer) != count(distinct-values(drawer))">
      Content of drawers not distinct
      <iso:value-of select="." />



Leave a Reply