我正在寻找一个XSD来验证包含许多付款的文件元素的XML是否具有相同的货币。
示例:
<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>
上层XML应被视为有效,因为所有<Currency>
元素都包含相同的货币信息。
但是,以下XML不应有效,因为它至少包含一个具有不同货币信息的付款:
<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>
我应该为我的XSD做什么?谢谢
这需要带有断言的XSD 1.1:
<xs:assert test="count(distinct-values(.//Currency)) = 1"/>
我认为XSD 1.0没有任何方法可以做到这一点。
只是为了提供MCVE(这不被视为答案(:
<?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>
但它不起作用
这只是为其他人提供的基础。