Schematron & Counting Total # of Attributes

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

Schematron & Counting Total # of Attributes

Tue Dec 17, 2013 5:02 am

Hi all-

New to schematron, so thank you for your help in advance!

I have built a pattern that is suppose to count the number of attributes that are equal to a certain number and 'assert' that the total number of attributes equal to that value is equal to 1.

My rule is not working and I don't understand why. The rule is to enforce that only a single tree node is allowed in my XML document. I thought it was working with the XML document listed below, but when I set the ParentID=0 on a second Property element, the assertion does not send back the error message.

I don't have control of the XML schema, so I can't change its format.

This is the rule in my schematron file:

Code: Select all

    <sch:pattern id="Check_Root">   
        <sch:rule context="//TreeData/Property/Property[@Name='ParentID' and @Value=0]">                     
            <sch:assert test="count(@Value=0]) = 1">
                Only a single root is allowed in the tree. Check source XML document.
            </sch:assert>
        </sch:rule>
    </sch:pattern>


This is a snipit of my XML file:

Code: Select all

<TreeData Name="Organization">
   <Property Name="Unit" Value="100">
      <Property Name="Name" Value="Root"/>
      <Property Name="ParentID" Value="0"/>
   </Property>
   <Property Name="Unit" Value="200">
      <Property Name="Name" Value="File_Folder_01"/>
      <Property Name="ParentURN" Value="100"/>
   </Property>
   <Property Name="Unit" Value="300">
      <Property Name="Name" Value="File_Folder_02"/>
      <Property Name="ParentURN" Value="200"/>
   </Property>
   <Property Name="Unit" Value="400">
      <Property Name="Name" Value="File_Folder_03"/>
      <Property Name="ParentURN" Value="300"/>
   </Property>
</TreeData>
radu_pisoi
Posts: 310
Location: Craiova

Re: Schematron & Counting Total # of Attributes

Tue Dec 17, 2013 6:15 pm

Hello,

I think that the problem with your Schematron rule is the context in which the assertion is executed. This context is establish by the 'context' attribute from the rule element. So, in your case, for each Property element with @Name='ParentID' and @Value=0 you check (assert) that it has a single attribute @Value = 1.

I think that the next rule is more appropriate for your case:

Code: Select all

<sch:rule context="TreeData">                     
  <sch:assert test="count(//Property[@Name='ParentID'][@Value=0]) = 1">
    Only a single root is allowed in the tree. Check source XML document.
  </sch:assert>
</sch:rule>


In this case, we assert that there should be only a Property element with 'Name' and 'Value' attributes equals with 'ParentID' and '0' respectively. This assertion is performed in the context of the 'TreeData' element.

I hope this helps.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
jjhernan
Posts: 4

Re: Schematron & Counting Total # of Attributes

Tue Dec 17, 2013 9:20 pm

Great!

Made the appropriate changes based on your comments and it worked!

Thank you for the prompt reply!

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests