本文介绍了共现约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XML Schema 1.1 编写 XSD 时遇到困难.

I am having difficulty writing an XSD I'm working on using XML Schema 1.1.

我有一个名为 PaymentMethod 的元素,它是C"或F":如果 PaymentMethod = "C",则为支票.如果 PaymentMethod = "F",则为资金转账.

I have one element named PaymentMethod that is either "C" or "F":If PaymentMethod = "C", then it's a check.If PaymentMethod = "F", then it's a fund transfer.

如何使 BankingInfo(BankName、TransitNo、AccountNo、AccountType)成为支票的可选和资金转帐的必填项?

How do I make BankingInfo (BankName, TransitNo, AccountNo, AccountType) optional for a check and mandatory for a fund transfer?

请参阅下面的代码片段.

See below for a snippet of my code.

<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 节点相同级别的断言,因为断言适用于相同级别或更低级别,但无法向上导航到父级.断言测试应检查帐户类型的值,并确保您希望必填"的字段确实具有值.

You will need to add an assert at the same level as the BankingInfo node because asserts apply at the same level or lower level but can't navigate up to a parent level. The assert test should check the value of account type as well as ensuring the fields that you want to be "required" actually have values.

要么

<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and count(/root/BankingInfo) > 0)"/>

或者,如果您想要一些银行信息而不是所有字段,您可以执行这样的操作

Or, if you want some bank info to be required but not all the fields you could do something like this

<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)"/>

以上两个都假设了一些类似于下面的结构(提供的部分不是一个足够完整的例子,无法将断言放在正确的级别)

Both of the above assume some structure similar to the below (the provided sections aren't a complete enough example to put the assert in at the right level)

<?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>

这篇关于共现约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 10:09