[英]Co-Occurrence Constraint
我在編寫使用XML Schema 1.1的XSD時遇到困難。
我有一個名為PaymentMethod的元素,它是“ C”或“ F”:如果PaymentMethod =“ C”,則為支票。 如果PaymentMethod =“ F”,則為資金轉帳。
如何使BankingInfo(BankName,TransitNo,AccountNo,AccountType)對於支票是可選的,對於資金轉賬是必選的?
請參閱下面的代碼片段。
<xs:element name="PaymentMethod">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="C" />
<xs:enumeration value="F" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:complexType name="BankingInfo" minOccurs="0">
<xs:sequence>
<xs:element name="TransitNo" type="xs:string" />
<xs:element name="AccountNo" type="xs:string" />
<xs:element name="AccountType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="CHK" />
<xs:enumeration value="SAV" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BankName" type="xs:string" />
</xs:sequence>
</xs:complexType>
您將需要在與BankingInfo節點相同的級別上添加一個斷言,因為斷言適用於同一級別或更低的級別,但無法向上導航到父級別。 斷言測試應檢查帳戶類型的值,並確保要“必需”的字段實際具有值。
要么
<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and count(/root/BankingInfo) > 0)"/>
或者,如果您需要一些銀行信息,但不要求所有字段,可以執行以下操作
<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and string-length(/root/BankingInfo/TransitNo) > 0 and string-length(/root/BankingInfo/AccountNo) > 0)"/>
上面的兩個方法都假定與下面的結構類似(所提供的部分還不夠完整,無法將assert置於正確的級別)
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="PaymentMethod">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="C"/>
<xs:enumeration value="F"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BankingInfo" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="TransitNo" type="xs:string"/>
<xs:element name="AccountNo" type="xs:string"/>
<xs:element name="AccountType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="CHK"/>
<xs:enumeration value="SAV"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BankName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and count(/root/BankingInfo) > 0)"/>
</xs:complexType>
</xs:element>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.