我们是否可以根据 XSD 中任何其他元素的值使元素成为可选或强制元素 [英] Can we make a element optional or mandatory depending on value of any other element in XSD

查看:12
本文介绍了我们是否可以根据 XSD 中任何其他元素的值使元素成为可选或强制元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XSD 中,我有一个元素 x,它取值 (a,b)

In XSD i have one element x which takes values (a,b)

还有一个元素y

有没有什么方法可以使用 XSD,这样当 x 的值为 b 时,我可以将 y 设为强制,如果 x 的值为 a,我可以将 y 设为可选

Is there any way using XSD so that i can make y as mandatory when value of x is b and optional if value of x is a

推荐答案

看看XML 架构第 0 部分:入门第二版2.2.1 出现约束.

上面写着:

一般来说,当 minOccurs 的值为 1 或更大时,需要一个元素出现.元素可能出现的最大次数由其声明中的 maxOccurs 属性的值决定.该值可以是一个正整数,例如 41,或者是无界的术语,表示没有最大出现次数.minOccurs 和 maxOccurs 属性的默认值都是 1.

In general, an element is required to appear when the value of minOccurs is 1 or more. The maximum number of times an element may appear is determined by the value of a maxOccurs attribute in its declaration. This value may be a positive integer such as 41, or the term unbounded to indicate there is no maximum number of occurrences. The default value for both the minOccurs and the maxOccurs attributes is 1.

由于无法从 XML Schema 中的其他模式内容推导出一个属性值,所以您想要实现的是不可能(使用 XML Schema).

Because an attribute value cannot be deduced from other schema content in XML Schema, what you want to achieve is impossible (with XML Schema).

(我不知道 Relax NG 是否可以做到这一点,但您可以调查它是否支持此类内容-依赖定义.)

(I don't know if Relax NG can do this, but you may investigate if it supports such content-dependent definitions.)

这篇关于我们是否可以根据 XSD 中任何其他元素的值使元素成为可选或强制元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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