Page 1 of 1

Schematron & Check for Unique attribute

Posted: Wed Dec 18, 2013 4:47 am
by jjhernan
...Still new to schematron... thank you in advance for your help...

I have a source XML document where I need to assert that an attribute is unique in a set of elements. I built a rule to enforce this, but its not working. I searched online and tried to follow some examples I found, but still no dice...

This is my source XML:

Code: Select all


<TreeView>
<DataSets>
<DataSet Name="Folders">
<Property Name="Folder" Value="101">
<Property Name="Name" Value="Cars"/>
<Property Name="Role" Value="a"/>
</Property>
<Property Name="Folder" Value="102">
<Property Name="Name" Value="Boats"/>
<Property Name="Role" Value="b"/>
</Property>
<Property Name="Folder" Value="103">
<Property Name="Name" Value="Bikes"/>
<Property Name="Role" Value="c"/>
</Property>
<Property Name="Folder" Value="104">
<Property Name="Name" Value="Trains"/>
<Property Name="Role" Value="d"/>
</Property>
<DataSet>
</DataSets>
</TreeView>

This is my rule:

Code: Select all


<sch:pattern id="Check_Unique_Roles">    
<sch:rule context="//DataSets/DataSet[@Name='Folders']">
<sch:assert test=" count(Property/Property[@Name='Role']) =
count(Property/Property[@Name='Role' and not(@Value=preceding-sibling/Property[@Name='Role']/@Value)])">
Roles for all Folders must be unique.
</sch:assert>
</sch:rule>
</sch:pattern>

Re: Schematron & Check for Unique attribute

Posted: Wed Dec 18, 2013 5:11 am
by jjhernan
Just came up with a working solution...

Code: Select all


    <xsl:key name="getAllRoles" match="//DataSets/DataSet[@Name='Folders']/Property/Property[@Name='Role']" use="@Value"/>

<sch:pattern id="CheckOrganization_UniqueRole">
<sch:rule context="//DataSets/DataSet[@Name='Folders']/Property">
<sch:assert test="count(key('getAllRoles',Property/@Value)) = 1" role="warning">
Warning: Roles for Folders should be unique.
</sch:assert>
</sch:rule>
</sch:pattern>