Page 1 of 1

Check for duplicates attribute value

Posted: Tue Sep 06, 2022 5:42 pm
by DanOvergaard
Hi,

Is there a validation in Xslt 2.0 that can test for duplicates in an attribute

If would like to catch that there a two “DK” languageID’s in the XML below

<cac:notes>
<cbc:note languagesID=“DK”>test</cbc:note>
<cbc:note languagesID=“SE”>test</cbc:note>
<cbc:note languagesID=“UK>test</cbc:note>
<cbc:note languagesID=“DK”>test</cbc:note>
</cac:notes>

Regards,
Dan

Re: Check for duplicates attribute value

Posted: Tue Sep 06, 2022 10:33 pm
by Martin Honnen
Validation is usually done with a schema language like XSD or Schematron (which is often implemented with XSLT under the hood).

So yes, you can can of course define the languagesID attribute with an xs:ID type and you would get a validation error for your sample.
In XSLT you can use a key or use for-each-group based on the attribute value and then you could check whether the group contains more than one item. Neither keys nor for-each-group are in any way restricted on attributes but can of course be used for them like for other nodes or values.

Re: Check for duplicates attribute value

Posted: Mon Sep 12, 2022 1:44 pm
by DanOvergaard
Hi Martin,

Thanks for your feedback - I used "for-each-group" and solved the problem :D

It's a "sch:schema" that is the source that builds the XSLT validation, but I couldn't find a solution to do this test - Actually I find that more complex validations is hard to solved in a sch:schema, but it might be my lack of knowledge

To solve the problem original in XSL ver. 1.0 the test below was used but it's not allowed in XSL 2.0
<sch:report test="local-name(following-sibling::*) = local-name(current()) and following-sibling::*/@languageID = self::*/@languageID">Error message</sch:report>


Regards,
Dan