如何限制包含 xs:any 的 xs:choice? [英] How to restrict an xs:choice containing xs:any?

查看:34
本文介绍了如何限制包含 xs:any 的 xs:choice?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含此内容的 xs:complexType:

<xs:any/></xs:choice>

我知道 xs:any 意味着任何 valid 元素,但通常情况下,我不希望我的根元素在除根之外的任何地方使用,所以我可以通过说任何 除了根(或任何其他特定)元素'?

解决方案

我认为一个理想的解决方案是重构架构,以便您拥有一种不包含 <xs:any/> 和另一个带有它的 ,也就是说,如果您出于某种原因确实必须使用 if.一般来说 <xs:any/> 是一个非常强大的声明,它将验证抛出窗口,因为它几乎允许来自任何命名空间的任何内容,已经说过你可能在这里是 processContents="strict" 或 processContents="lax" 属性,所以:

意味着对于任何命名空间,它都必须针对架构进行验证,即使架构不存在或(可能更适合您的情况):

这仅在架构存在时才需要验证.

因为您正在为自己的命名空间定义架构,所以这将起作用 - 即,如果您尝试使用来自您自己/默认 namspace 的根元素 - 它会针对该命名空间对其进行验证并告诉您这是不允许的.

您可以参考 W3C 规范了解更多详情.>

If I have a xs:complexType that conatins this:

<xs:choice minOccurs='0' maxOccurs='unbounded'>
   <xs:any/>
</xs:choice>

I understand that xs:any meany any valid element, but normally, I wouldn't want my root element to be used anywhere except as root, so can I prevent that by saying 'any except the root (or any other specific) element' ?

解决方案

I think an ideal solution would be to refactor the schema so that you have one type that does not include <xs:any/> and another one with it <xs:any/>, that is if you really have to use if for some reason. In general <xs:any/> is a very strong statement that throws the validation out of the window as it pretty much allows anything from any namespace, having said that what you're probably after here is processContents="strict" or processContents="lax" attribute, so:

<xsd:any processContents="strict"/>

means that for any namespace it will have to validate against a schema, even if the schema doesn't exists or (probably better for your case):

This will only require validation if the schema exists.

Because you are defining a schema for your own namespace, this will work - ie if you try to use a root element from your own/default namspace - it will validate it against that namespace and tell you it's not allowed.

You can refer to the W3C Spec for more details.

这篇关于如何限制包含 xs:any 的 xs:choice?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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