[英]XSD to check if all element occurrence contains the same value in the whole XML file
I am looking for a XSD to validate if a XML containing file elements with many payments have the same currency.我正在寻找一个 XSD 来验证包含许多付款的文件元素的 XML 是否具有相同的货币。
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.应将上层 XML 视为有效,因为所有
<Currency>
元素都包含相同的货币信息。
However, the following XML should not be valid as it contains at least one payment with different currency information:但是,以下 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>
What should I do for my XSD?我应该为我的 XSD 做什么? Thanks!
谢谢!
This needs XSD 1.1 with assertions:这需要带有断言的 XSD 1.1:
<xs:assert test="count(distinct-values(.//Currency)) = 1"/>
I don't think there's any way to do it with XSD 1.0.我认为 XSD 1.0 没有办法做到这一点。
Just to provide a MCVE (This is not to be considered an answer):只是为了提供 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>
And it doesn't work.它不起作用。
This is only provided as a base for others to work on this.这仅作为其他人进行此工作的基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.