如何在 XSD 中定义互斥属性? [英] How to define mutually exclusive attributes in XSD?

查看:23
本文介绍了如何在 XSD 中定义互斥属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是代码片段...

 解决方案 

XSD 1.0

可以使用 xs:key 巧妙地完成.请参阅@Kachna 的回答.

请注意,如果对于 xs:key 中的多个选定值失败,某些解析器可能会允许这两个属性.过去至少有一个已知案例.>

XSD 1.1

可以使用 xs:assert:

<xs:元素名称="标签"><xs:complexType><xs:sequence/><xs:attribute name="name" type="xs:string"/><xs:attribute name="abc" use="optional" type="xs:integer"/><xs:attribute name="def" use="optional" type="xs:integer"/><xs:assert test="(@abc and not(@def)) or (not(@abc) and @def)"/></xs:complexType></xs:element></xs:schema>

First the code fragment...

<tag name="default" abc="10" def="20> <!-- not valid, abc and def should be mutually exclusive -->

<tag name="default1" abc="10"> <!-- valid -->

<tag name="default2" def="20> <!-- valid -->

What I want to do...

What can I put into my XSD so that @abc and @def cannot coexist as attributes on the same element?

So that validation would fail if they coexisted on the same element?

解决方案

XSD 1.0

Can be done with clever trick using xs:key. See @Kachna's answer.

Note that some parsers may allow both attributes if they fail to fail for multiple selected values in xs:key. There is at least one known case of this happening in the past.

XSD 1.1

Can be done using xs:assert:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="tag">
    <xs:complexType>
      <xs:sequence/>
      <xs:attribute name="name" type="xs:string"/>
      <xs:attribute name="abc" use="optional" type="xs:integer"/>      
      <xs:attribute name="def" use="optional" type="xs:integer"/>
      <xs:assert test="(@abc and not(@def)) or (not(@abc) and @def)"/>      
    </xs:complexType>
  </xs:element>
</xs:schema>

这篇关于如何在 XSD 中定义互斥属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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