【问题标题】:Restrict attribute value depending on value of another attribute根据另一个属性的值限制属性值
【发布时间】:2012-07-31 15:23:13
【问题描述】:

考虑一下这个 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>

所以对于属性Test/@Y,我想根据Test/@X的值来限制可能的值。如果XOne,则Y 必须在List1 中,否则Y 必须在List2 中。这可能吗?

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    检查这个关于共现约束的问题:XSD: Define attributes based on the value of previous attribute

    总结:

    • 可以在 XSD 1.1 中使用
    • 在 XSD 1.0 中不可能

    【讨论】:

    • 感谢您的链接,Google 很难找到这个链接,因为它很难用简单的语言表达。现在我知道共现是我需要的查询。
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多