Schematron - Counting number of elements+attributes

This should cover W3C XML Schema, Relax NG and DTD related problems.
Iwicka
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

Schematron - Counting number of elements+attributes

Post by Iwicka » Thu Aug 03, 2017 1:57 pm

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

tavy
Posts: 184
Joined: Thu Jul 01, 2004 12:29 pm

Re: Schematron - Counting number of elements+attributes

Post by tavy » Thu Aug 03, 2017 4:33 pm

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
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com

Iwicka
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

Re: Schematron - Counting number of elements+attributes

Post by Iwicka » Thu Aug 03, 2017 6:17 pm

It finally works, Thank you Octavian! :D

Post Reply