Page 1 of 1

Unique combination of element / attribute value in schematron

Posted: Mon Aug 07, 2017 7:41 pm
by Iwicka

I have a problem that seems to be quite common, but none of the examples I found, actually works for me. The rule needs to say that the combination of element and attribute value must beunique. There can be only one instance of a name per language, but the name can be the same for different languages:

Code: Select all

<languageSpecificBrandName language='en'>name1</languageSpecificBrandName>
<languageSpecificBrandName language='fr'>name2</languageSpecificBrandName>
<languageSpecificBrandName language='de'>name2</languageSpecificBrandName>

The rule I created is the following:

Code: Select all

        <sch:rule context="//brandName">
            <sch:assert test="count(languageSpecificBrandName/@languageCode[.=current()])=1">Error</sch:assert>

But it always gives an error, so there must be something wrong with it.

Could you give me some suggestion?

Thank you,


Re: Unique combination of element / attribute value in schematron

Posted: Tue Aug 08, 2017 10:52 am
by tavy

You can create a rule that for each languageSpecificBrandName element verifies that there is no previous sibling with the same name and language code. The rule must be something like this:

Code: Select all

<sch:rule context="languageSpecificBrandName">
    <sch:let name="currentLang" value="@language"/>
    <sch:let name="currentName" value="text()"/>
    <sch:assert test="count(preceding-sibling::languageSpecificBrandName[@language=$currentLang and text() = $currentName])=0">
        Combination lang of '<sch:value-of select="$currentLang"/>' and name '<sch:value-of select="$currentName"/>' already used.

Best Regards,