Unique combination of element / attribute value in schematron

Questions about XML that are not covered by the other forums should go here.
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

Unique combination of element / attribute value in schematron

Post by Iwicka » Mon Aug 07, 2017 7:41 pm


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,


Posts: 193
Joined: Thu Jul 01, 2004 12:29 pm

Re: Unique combination of element / attribute value in schematron

Post by tavy » Tue Aug 08, 2017 10:52 am


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,
Octavian Nadolu
<oXygen/> XML Editor

Post Reply