【问题标题】:Restrict use of an attribute in an XSD assertion限制在 XSD 断言中使用属性
【发布时间】:2016-05-12 12:27:01
【问题描述】:

给定 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. 如果&lt;color&gt;@type白色或黑色,它不能同时有@number属性,
  2. 如果&lt;color&gt;@type 红色或绿色,它也必须有@number 属性。

【问题讨论】:

    标签: xml xsd assertions xsd-1.1


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多