Check for duplicates attribute value

Here should go questions about transforming XML with XSLT and FOP.
DanOvergaard
Posts: 22
Joined: Thu Jan 07, 2021 10:44 am

Check for duplicates attribute value

Post 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
Martin Honnen
Posts: 97
Joined: Tue Aug 19, 2014 12:04 pm

Re: Check for duplicates attribute value

Post 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.
DanOvergaard
Posts: 22
Joined: Thu Jan 07, 2021 10:44 am

Re: Check for duplicates attribute value

Post 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
Post Reply