本文介绍了根据 xsd 中的父母年龄验证孩子的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像下面这样的 xml
I have an xml like below
<parents>
<mother name="MMM" age="55" />
<children>
<child name="CCC" gender="male" age="25" />
</children>
</parents>
要验证母亲的年龄或孩子的年龄,我们可以像下面这样编写 xsd
To validate mother's age or Child's age we can write the xsd like below
<xs:element name="mother">
<xs:complexType>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
但是如果我要验证孩子的年龄,哪个不应该超过母亲的年龄?
But If I want to validate the child's age, which should not be more than mother's age?
推荐答案
来自 Wiki:
XSD 不提供任何工具来声明一个的值或存在属性取决于其他属性的值或存在(所谓的共现约束).
这正是您想要的:根据母亲的年龄值限制孩子的年龄值.因此,不幸的是,不可能使用 XML Schema 1.0.
不过,可以用 XML Schema 1.1 来做这样的事情(非常粗略的例子,只是为了展示概念):
<xs:assert test="@age < ../../mother[@age]"/>
这篇关于根据 xsd 中的父母年龄验证孩子的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!