I'm struggeling to create an unique-constraint in my XSD and trying solutions from other SO-question did bring me closer, but I could not solve my problem. The strange thing is that XMLSpy shows that my constraint is valid, but when I try to validate a XML file, which violates the constraint, using Notepad++ XML-Tools it tells me the file "is valid".
I have an element Zahlungstage
(German for "payment days"). Beneath this I have a list of single Zahlungstag
(single "payment day") with a Datum
attribute.
For each Zahlungstag
there is exactly one Wertpapiergruppe
which has an attribute called ISIN
.
What I want is a constraint, to ensure that the combination of Zahlungstag/@Datum
and Wertpapiergruppe/@ISIN
is unique.
This should be valid, as the combination is unique:
<Zahlungstage>
<Zahlungstag Datum="2020-01-01">
<Wertpapiergruppe ISIN="1">
<!-- Further elements-->
<Zahlungstag>
<Zahlungstag Datum="2020-01-01">
<Wertpapiergruppe ISIN="2">
<!-- Further elements-->
<Zahlungstag>
<Zahlungstag Datum="2020-01-02">
<Wertpapiergruppe ISIN="1">
<!-- Further elements-->
<Zahlungstag>
</Zahlungstage>
But this should not be valid, as the ISIN
with value 1
is used two times at the same Zahlungstag/@Datum
:
<Zahlungstage>
<Zahlungstag Datum="2020-01-01">
<Wertpapiergruppe ISIN="1">
<!-- Further elements-->
<Zahlungstag>
<Zahlungstag Datum="2020-01-01">
<Wertpapiergruppe ISIN="2">
<!-- Further elements-->
<Zahlungstag>
<Zahlungstag Datum="2020-01-01">
<Wertpapiergruppe ISIN="1"> <!-- Violation! -->
<!-- Further elements-->
<Zahlungstag>
</Zahlungstage>
These are the correspondening element definiton.
<xs:element name="Zahlungstage">
<xs:complexType>
<xs:sequence>
<xs:element name="Zahlungstag" type="ZahlungstagBescheinigungType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="EindeutigerWertpapiergruppenZahlungstagMA11">
<xs:selector xpath="Zahlungstag"/>
<xs:field xpath="Wertpapiergruppe/@ISIN"/>
<xs:field xpath="@Datum"/>
</xs:unique>
</xs:element>
<xs:complexType name="ZahlungstagBescheinigungType">
<xs:complexContent>
<xs:extension base="ZahlungstagBasisType">
<xs:sequence>
<!-- Further elements -->
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ZahlungstagBasisType">
<xs:sequence>
<xs:element name="Wertpapiergruppe" type="std:WertpapiergruppeType" minOccurs="1" maxOccurs="1">
</xs:element>
<!-- Further elements -->
</xs:sequence>
<xs:attribute name="Datum" type="xs:date" use="required">
</xs:attribute>
</xs:complexType>
<xs:complexType name="WertpapiergruppeType">
<xs:attribute name="ISIN" type="ISINType" use="required"> <!-- ISINType is a String with a regex -->
</xs:attribute>
<!-- Further attributes -->
</xs:complexType>