限制 xsd 中的空元素 [英] Restricting empty elements in xsd

查看:37
本文介绍了限制 xsd 中的空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法防止在您的 xml 中使用 <myElement/> 形式的空元素?换句话说,您能否在 xsd 中指定 无效?

Is there a way to prevent empty elements of the form <myElement/> being used in your xml? In other words, can you specify in your xsd that <myElement/> is invalid?

使用 nillable="false" 不起作用,minOccurs="1" 也不起作用 - 这两者都允许.

Using nillable="false" doesn't work, nor does minOccurs="1" - both of those allow <myElement/>.

推荐答案

如果你想完全阻止元素出现,你可以用 maxOccurs="0" 标记它.我猜这不是你想要的,所以如果你想确保总是有属性附加到复杂元素,那么你必须指定 usage="required"在至少一个属性上或使用属性组.如果 myElement 是一个简单的类型并且你想确保它有一个值,那么你总是可以限制它的类型.如果你想要一个非零字符串,那么你可以这样做:

If you're trying to prevent the element from appearing at all, you can mark it with maxOccurs="0". I'm guessing this isn't what you're after, so if you're trying to make sure there are always attributes attached to the complex element, then you have to specify usage="required" on at least one of the attributes or use an attribute group. If myElement is a simple type and you want to make sure it has a value, then you could always restrict the type of it. If you want a non-zero string, then you could do:

<xsd:element name="myElement">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

这篇关于限制 xsd 中的空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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