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

Unique combination of element / attribute value in schematron

Mon Aug 07, 2017 7:41 pm

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: 122

Re: Unique combination of element / attribute value in schematron

Tue Aug 08, 2017 10:52 am

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest