元素之间的 XML 模式约束 [英] XML schema constraint between elements

查看:23
本文介绍了元素之间的 XML 模式约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个如下所示的 XML 元素:

Suppose I have an XML element which looks like this:

<foo>
    <a>2</a>
    <b>5</b>
</foo>

我想用架构 (.xsd) 表达以下约束:

I would like to express the following constraints with a schema (.xsd):

  1. 只有一个孩子 和只有一个孩子 ,并且没有其他孩子.

  1. <foo> has exactly one child <a> and exactly one child <b>, and no other children.

的值是区间 [1, 10] 内的整数.

The values of <a> and <b> are integers in interval [1, 10].

的值小于或等于 的值.

The value of <a> is less than or equal to the value of <b>.

我知道如何使用模式表达约束 1 和 2,但不知道如何使用模式 3.

I know how to express constraints 1 and 2 with a schema, but not 3.

有什么帮助吗?

推荐答案

对于第 3 点,您可以添加以下约束:

For the 3rd point you can add the following constraint:

<xs:complexType name="foo">
    <xs:sequence>
        <!-- or all if the order is not important -->
        <xs:element name="a" type="xs:integer" maxOccurs="1" minOccurs="1">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1"/>
                    <xs:maxInclusive value="10"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="b" type="xs:integer" maxOccurs="1" minOccurs="1">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1"/>
                    <xs:maxInclusive value="10"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
    <xs:assert test="a &lt;= b"/> <!-- your 3rd constraint -->
</xs:complexType>

这篇关于元素之间的 XML 模式约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆