Restrict use of an attribute in an XSD assertion

筅森魡賤 提交于 2019-12-11 12:58:37

问题


Given XSD:

<xs:element name="color">
    <xs:complexType>
        <xs:attribute name="type">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="white"/>
                    <xs:enumeration value="black"/>
                    <xs:enumeration value="red"/>
                    <xs:enumeration value="green"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="number" use="optional/>
    </xs:complexType>
</xs:element>

How would I write an assertion so that

  1. if <color> has @type white or black, it cannot also have the @number attribute, and
  2. if <color> has @type red or green, it must also have the @number attribute.

回答1:


XSD 1.1

<?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"
    vc:minVersion="1.1">
    <xs:element name="color">
        <xs:complexType>
            <xs:attribute name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="white"/>
                        <xs:enumeration value="black"/>
                        <xs:enumeration value="red"/>
                        <xs:enumeration value="green"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="number" use="optional"/>
            <xs:assert test="   (@type = ('white','black') and not(@number))
                             or (@type = ('red','green') and @number)"/>
        </xs:complexType>
    </xs:element>
</xs:schema>


来源:https://stackoverflow.com/questions/37186870/restrict-use-of-an-attribute-in-an-xsd-assertion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!