Schematron & Check for Unique attribute

This should cover W3C XML Schema, Relax NG and DTD related problems.
jjhernan
Posts: 4

Schematron & Check for Unique attribute

Wed Dec 18, 2013 4:47 am

...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>
jjhernan
Posts: 4

Re: Schematron & Check for Unique attribute

Wed Dec 18, 2013 5:11 am

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>


Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests