XSD to check if all element occurrence contains the same value in the whole XML file

145 views Asked by At

I am looking for a XSD to validate if a XML containing file elements with many payments have the same currency.

Example:

<Payments>
    <Payment>
        <PaymentDate>2020-09-28</PaymentDate>
        <Amount>11</Amount>
        <Currency>USD</Currency>
    </Payment>
    <Payment>
        <PaymentDate>2020-09-27</PaymentDate>
        <Amount>19</Amount>
        <Currency>USD</Currency>
    </Payment>
    <Payment>
        <PaymentDate>2020-09-27</PaymentDate>
        <Amount>12</Amount>
        <Currency>USD</Currency>
    </Payment>
</Payments>

The upper XML should be considered as valid because all <Currency> elements contain the same currency information.

However, the following XML should not be valid as it contains at least one payment with different currency information:

    <Payments>
        <Payment>
            <PaymentDate>2020-09-28</PaymentDate>
            <Amount>11</Amount>
            <Currency>USD</Currency>
        </Payment>
        <Payment>
            <PaymentDate>2020-09-27</PaymentDate>
            <Amount>19</Amount>
            <Currency>EUR</Currency>
        </Payment>
        <Payment>
            <PaymentDate>2020-09-27</PaymentDate>
            <Amount>12</Amount>
            <Currency>USD</Currency>
        </Payment>
    </Payments>

What should I do for my XSD? Thanks!

2

There are 2 answers

1
Michael Kay On

This needs XSD 1.1 with assertions:

<xs:assert test="count(distinct-values(.//Currency)) = 1"/>

I don't think there's any way to do it with XSD 1.0.

0
zx485 On

Just to provide a MCVE (This is not to be considered an answer):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema version="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="unqualified">

  <xs:element name="Payments">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Payment" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="PaymentDate" />
                <xs:element name="Amount" />
                <xs:element name="Currency" />
              </xs:sequence>
            </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:assert test="count(distinct-values(Payment/Currency)) = 1" />
    </xs:complexType>
  </xs:element>
  
</xs:schema>

And it doesn't work.
This is only provided as a base for others to work on this.