限制在 XSD 断言中使用属性 [英] Restrict use of an attribute in an XSD assertion

查看:29
本文介绍了限制在 XSD 断言中使用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 XSD:

<xs:element name="color">
    <xs:complexType>
        <xs:attribute name="type">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="white"/>
                    <xs:enumeration value="black"/>
                    <xs:enumeration value="red"/>
                    <xs:enumeration value="green"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="number" use="optional/>
    </xs:complexType>
</xs:element>

我将如何编写断言以便

  1. 如果<color>@type白色或黑色,它也不能有@number属性,
  2. 如果 @type 红色或绿色,它也必须有 @number 属性.
  1. if <color> has @type white or black, it cannot also have the @number attribute, and
  2. if <color> has @type red or green, it must also have the @number attribute.

推荐答案

XSD 1.1

<?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" 
    elementFormDefault="qualified"
    vc:minVersion="1.1">
    <xs:element name="color">
        <xs:complexType>
            <xs:attribute name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="white"/>
                        <xs:enumeration value="black"/>
                        <xs:enumeration value="red"/>
                        <xs:enumeration value="green"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="number" use="optional"/>
            <xs:assert test="   (@type = ('white','black') and not(@number))
                             or (@type = ('red','green') and @number)"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

这篇关于限制在 XSD 断言中使用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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