【问题标题】:XSD: how to use restriction on a group of key-value?XSD:如何对一组键值使用限制?
【发布时间】:2019-03-22 06:59:22
【问题描述】:

我的实际 xml 是这样的:

<gender code="2" display="female />

性别代码是1-男,2-女。现在我已经有了这样的 xsd:

<xs:element name="gender">
    <xs:complexType>
        <xs:attribute name="code" type="xs:int">
            <xs:simpleType>
                <xs:restriction base="int">
                    <xs:enumeration value="1"/>
                    <xs:enumeration value="2"/>
                </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        <xs:attribute name="displayName" type="xs:string">
            <xs:simpleType>
                <xs:restriction>
                    <xs:enumeration value="male"/>
                    <xs:enumeration value="female"/>
                </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
    </xs:complexType>
</xs:element>

但是对于这个xml也是有效的:

<gender code="2" display="male />

我正在寻找 xs:restriction,但它似乎没有帮助。还有其他方法可以修复我的 xsd 吗?

【问题讨论】:

  • 我不明白,你为什么要为这些冗余(非标准化)元素而烦恼。 code 已经包含了基本信息,而display 似乎只是为了美化人类消费;我看不出将它包含在计划中的理由,更不用说将每个人元素单独包含在内。

标签: xml xsd xsd-validation


【解决方案1】:

要对多个属性施加约束,您需要 XSD 1.1 断言:

<xs:assert test="(@code=1 and @display='male') 
                or (@code=2 and @display='female')"/>

作为xs:element 的子代。

但并非所有架构处理器都支持 XSD 1.1。

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多