Unique combination of element / attribute value in schematron

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

Unique combination of element / attribute value in schematron

Post by Iwicka »

Hi,

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


<brandName>
<languageSpecificBrandName language='en'>name1</languageSpecificBrandName>
<languageSpecificBrandName language='fr'>name2</languageSpecificBrandName>
<languageSpecificBrandName language='de'>name2</languageSpecificBrandName>
</brandName>
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>
</sch:rule>
But it always gives an error, so there must be something wrong with it.

Could you give me some suggestion?

Thank you,

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

Re: Unique combination of element / attribute value in schematron

Post by tavy »

Hello,

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.
</sch:assert>
</sch:rule>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply