xml - 限制在 XSD 断言中使用属性

标签 xml xsd assertions xsd-1.1

给定 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. 如果 <color>@type红色或绿色,它还必须具有 @number属性。

最佳答案

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>

关于xml - 限制在 XSD 断言中使用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186870/

相关文章:

xml - 使用 XML::Simple 时如何删除魔数(Magic Number)?

java - Android:如何为应用程序创建一组演示幻灯片,以便在用户第一次打开应用程序时向用户展示?

xml - 如何编写 xpath 来选择文本中包含特殊字符(如撇号)的节点?

java - 将 XML 属性的值限制为不同属性/元素的值

java - 如何在 jUnit 中比较 bundle 和 collectionassert?

c++ - 家谱软件中的循环

java - Jackson 没有大写 XML 元素

java - 使用 jaxb 从 xsd 中选择生成 java 类

c# - 如何将 XSD 文件转换为 C# 类

java - 断言集合 "Contains at least one non-null element"