简体   繁体   English

XML模式唯一的子元素

[英]XML schema unique child elements

I am trying to validate that a parent has unique child elements by the element name. 我试图通过元素名称验证父元素是否具有唯一的子元素。 I also need to validate that at least one of the possible elements exists. 我还需要验证至少存在一个可能的元素。

Example

<StorageTypes>
    <Status/>
    <Warning/>  
    <Error/>             
</StorageTypes>  

Would be valid but 会有效但是

<StorageTypes>
    <Status/>
    <Status/>
    <Warning/>  
    <Error/>             
</StorageTypes>  

and

<StorageTypes>               
</StorageTypes>  

would be invalid. 会无效的。

I have tried using xs:unique, selecting the the current node, but am unable to generically select the elements with the field element. 我已经尝试使用xs:unique,选择当前节点,但是我无法通常使用field元素选择元素。

The below acheives what I am looking to do but it is not the robust solution I am looking for. 以下是我想要做的事情,但它不是我想要的强大解决方案。 I would like to be able to add more Types with out changing the xpath for every change. 我希望能够添加更多类型,而不是为每个更改更改xpath。

<element ect.>
    <xs:unique name="something">
        <xs:selector xpath="."/>
        <xs:field xpath="Status"/>
        <xs:field xpath="Warning"/>
        <xs:field xpath="Error"/>
    </xs:unique>
</element>

Is this possible? 这可能吗? Is this good practice? 这是好习惯吗? Any advice would be appreciated. 任何意见,将不胜感激。

A generic solution based on XSD 1.0 is not possible. 不可能使用基于XSD 1.0的通用解决方案。 I don't believe your unique achieves what you want either, simply because a field's value is evaluated using text(); 我不相信你的独特性能达到你想要的,只是因为使用text()来评估一个字段的值; so the name of the tag never plays a role in this... 所以标签的名称永远不会在这个...

While you may be able to achieve something that is close to what you want, it may not work in all scenarios, or it'll lack elegance or feasability... 虽然你可能能够达到你想要的东西,但它可能不适用于所有场景,或者它缺乏优雅或可行性......

Below approaches may not be considered elegant by some... nonetheless, it could serve the same purpose. 某些人可能不会认为下面的方法是优雅的......尽管如此,它可以起到同样的作用。

For eg, if instead you use an attribute to capture the "tag" name as in: 例如,如果您使用属性来捕获“标记”名称,如下所示:

<StorageTypes>
   <Storage type="Status"/>
   <Storage type="Warning"/>
   <Storage type="Error"/>
</StorageTypes>

Then writing your unique on @type field, combined with a sequence of Storage, maxOccurs=whatever gives you strictly what you've depicted. 然后在@type字段上编写您的唯一,并结合一系列存储,maxOccurs = whatever会严格按照您的描述给出。

If instead you're looking for elements that actually have complex content, or attributes are not allowed, then you could use substitution groups to the same effect. 如果您正在寻找实际上具有复杂内容的元素,或者不允许属性,那么您可以使用替换组来实现相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM