Restrict attribute value depending on value of another attribute

醉酒当歌 提交于 2019-12-01 07:39:36

问题


Consider this XSD:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Test">
        <xs:complexType>
            <xs:attribute name="X">
                <xs:simpleType>
                    <xs:restriction>
                        <xs:enumeration value="One"/>
                        <xs:enumeration value="Two"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="Y">
                <xs:simpleType>
                    <!-- Choose how to restrict the value based on the value of the X attribute. -->
                    <xs:restriction base="if X=One then List1 else List2"/>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="List1">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Option1"/>
            <xs:enumeration value="Option2"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="List2">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Option3"/>
            <xs:enumeration value="Option4"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

So for the attribute Test/@Y, I want to restrict the possible values based on the value of Test/@X. If X is One, then Y can must be in List1, otherwise, Y must be in List2. Is this possible?


回答1:


Check this question about co-occurrence constraints: XSD: Define attributes based on the value of previous attribute

Summarizing:

  • Possible in XSD 1.1
  • Not possible in XSD 1.0


来源:https://stackoverflow.com/questions/11743510/restrict-attribute-value-depending-on-value-of-another-attribute

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