简体   繁体   English

是否可以定义XSD复杂类型以基于枚举属性的值来验证属性使用?

[英]Is it possible to define an XSD Complex Type to validate attribute use based on the value of an enumerated attribute?

The following XSD defines a complex type which can either have class="BIT" and a size, OR class="CHAR" and an encoding. 以下XSD定义了一个复杂类型,该类型可以具有class =“ BIT”和大小,或者具有class =“ CHAR”和编码。 The elements this type will be used for will not have any content. 此类型将用于的元素将没有任何内容。

<xs:complexType>
    <xs:attribute name="class">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="CHAR"/>
                <xs:enumeration value="BIT"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="size" type="xs:integer" />
    <xs:attribute name="encoding" type="xs:string" />
</xs:complexType>

Is there a way I can change this complex type definition so size is required if the class is BIT or encoding is required if the class is CHAR? 有没有一种方法可以更改此复杂的类型定义,因此,如果类是BIT,则需要大小;如果类是CHAR,则需要编码?

The existence of the size or encoding attributes should be mutually exclusive, having an encoding for BIT or having a size for CHAR would be meaningless in the context it is being used. 大小或编码属性的存在应该是互斥的,在使用BIT的情况下,对BIT进行编码或对CHAR进行大小处理将毫无意义。

If it is permissible to alter the schema the following should work but an explicit xsi:type attribute will be needed for elements of the base type in the XML instances of this schema. 如果允许更改架构,则以下内容应该起作用,但是此架构的XML实例中基本类型的元素将需要显式的xsi:type属性。 The way it works is a base type has been created with the class attribute, extended to include the size and encoding attributes, and then restricted. 它的工作方式是使用class属性创建基本类型,将其扩展为包括size和encoding属性,然后对其进行限制。 Elements can be declared in the schema as the base type and instances can use the restricted types. 元素可以在架构中声明为基本类型,实例可以使用受限类型。

<!-- Base Read Mode Simple Type -->

<xs:simpleType name="MyReadModeType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="CHAR"/>
        <xs:enumeration value="BIT"/>
    </xs:restriction>
</xs:simpleType>

<!-- Char Read Mode Simple Type -->

<xs:simpleType name="MyCharReadModeType">
    <xs:restriction base="tns:MyReadModeType">
        <xs:enumeration value="CHAR"/>
    </xs:restriction>
</xs:simpleType>

<!-- Bit Read Mode Simple Type -->

<xs:simpleType name="MyBitReadModeType">
    <xs:restriction base="tns:MyReadModeType">
        <xs:enumeration value="BIT"/>
    </xs:restriction>
</xs:simpleType>

<!-- Set Read Mode Base Type -->

<xs:complexType name="MySetReadModeType" abstract="true">
    <xs:attribute name="class" type="tns:MyReadModeType" />
</xs:complexType>

<!-- Set Char Mode Extension -->

<xs:complexType name="MyBaseSetCharReadModeType" abstract="true">
    <xs:complexContent>
        <xs:extension base="tns:MySetReadModeType">
            <xs:attribute name="size" type="tns:MyReadModeSizeType" use="prohibited" />
            <xs:attribute name="encoding" type="xs:string" use="required"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<!-- Set Char Mode Type -->

<xs:complexType name="charMode">
    <xs:complexContent>
        <xs:restriction base="tns:MyBaseSetCharReadModeType">
            <xs:attribute name="size" type="tns:MyReadModeSizeType" use="prohibited" />
            <xs:attribute name="encoding" type="xs:string" use="required"/>
            <xs:attribute name="class" type="tns:MyCharReadModeType" use="optional" default="CHAR" />
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

<!-- Set Bit Mode Extension -->

<xs:complexType name="MyBaseSetBitReadModeType" abstract="true">
    <xs:complexContent>
        <xs:extension base="tns:MySetReadModeType">
            <xs:attribute name="size" type="tns:MyReadModeSizeType" use="required" />
            <xs:attribute name="encoding" type="xs:string" use="prohibited"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<!-- Set Bit Mode Type -->

<xs:complexType name="bitMode">
    <xs:complexContent>
        <xs:restriction base="tns:MyBaseSetBitReadModeType">
            <xs:attribute name="size" type="tns:MyReadModeSizeType" use="required" />
            <xs:attribute name="encoding" type="xs:string" use="prohibited"/>
            <xs:attribute name="class" type="tns:MyBitReadModeType" use="optional" default="BIT"/>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM