Page 1 of 1

Schematron - Counting number of elements+attributes

Posted: Thu Aug 03, 2017 1:57 pm
by Iwicka
Hi,

I have a strange problem in schematron - I need to create a role that an element with certain attribute value should occur in the XML exactly once. I created the following rule, but it doesn't show an error when the number of elements with 'en' attributes value is more than 1. However, if I change the constrain to e.g. 3, then it does show en error when there are only 2 of them. What am I doing wrong?

My rule:

Code: Select all

    <sch:pattern>
<sch:rule context="//description/functionalName/@languageCode">
<sch:assert test="count(.='en') = 1">The value of Type must be set to a valid GS1 document name</sch:assert>
</sch:rule>
</sch:pattern>

This doesn't work either:

Code: Select all

    <sch:pattern>
<sch:rule context="//description">
<sch:assert test="count(functionalName/@languageCode='en') = 1">The value of Type must be set to a valid GS1 document name</sch:assert>
</sch:rule>
</sch:pattern>
This XML instance should be incorrect - the name in one language should appear only once:

Code: Select all


<description>
<functionalName languageCode='sv'/>
<functionalName languageCode='sv'/>
</description>

Thanks in advance for any help,

Ewa

Re: Schematron - Counting number of elements+attributes

Posted: Thu Aug 03, 2017 4:33 pm
by tavy
Hello,

You can create a rule for the description element that checks the number of functionalName elements that have the @languageCode equal with 'en', something like this:

Code: Select all


<sch:pattern>
<sch:rule context="description">
<sch:assert test="count(functionalName[@languageCode= 'en']) = 1">The value of Type must be set to a valid GS1 document name</sch:assert>
</sch:rule>
</sch:pattern>
Best Regards,
Octavian

Re: Schematron - Counting number of elements+attributes

Posted: Thu Aug 03, 2017 6:17 pm
by Iwicka
It finally works, Thank you Octavian! :D