0

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>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
bish
  • 3,381
  • 9
  • 48
  • 69
  • 1
    Maybe you need to update your notepad++ xml tools extension because for me it is working as it should be. the example that you defined as valid is indeed valid, the invalid does give an error like duplicate key for the unique identity constrained. just make sure you remove the xsi:nonamespace declaration from the content so you need to give the location of the xsd to notepad++. if you leave it in notepad++ will find it valid. – martijn Nov 25 '20 at 07:22
  • @martijn Which version do you use? I'm at 2.4.11 x64 (which is quite old as I just saw) – bish Nov 25 '20 at 08:38
  • notepad++ V7.9 (32bit) xml tools 3.0.4.2 – martijn Nov 25 '20 at 08:46
  • 1
    I agree with Martijn, your schema looks fine and I think the problem is with your tools or the way you are using them. – Michael Kay Nov 25 '20 at 10:19
  • As I'm not allowed to update the Plugin by myself I'll try to find another way. Validated my file with JAVA and the unique constraint does also not be claimed as violated :( I'll keep this up to date – bish Nov 26 '20 at 08:49

1 Answers1

0

According to the comments it seems that the constraint was good. After a long talk with a teammate, he remembered a similar problem and found the SO question 22353302.

It turns out, that the constriant is correct, but as the XSD is not the root XSD of the later validation, the validation looks into the wrong namespaces.

After I changed my definiton to explizit namespaces it worked well.

bish
  • 3,381
  • 9
  • 48
  • 69