xsd - 在 Xml 架构中向简单类型添加属性或向复杂类型添加限制

标签 xsd restriction

问题如下:

我有以下 XML 片段:

<time format="minutes">11:60</time>

问题是我无法同时添加属性和限制。属性格式只能具有值分钟、小时和秒。时间具有限制模式 \d{2}:\d{2}

<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="minutes"/>
        <xs:enumeration value="hours"/>
        <xs:enumeration value="seconds"/>
    </xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
    <xs:attribute name="format">
        <xs:simpleType>
            <xs:restriction base="formatType"/>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

如果我创建一个复杂类型的 timeType,我可以添加属性,但不能添加限制,如果我创建一个简单类型,我可以添加限制但不能添加属性。有什么办法可以解决这个问题吗?这不是一个很奇怪的限制,不是吗?

最佳答案

要添加属性,您必须通过扩展派生,要添加方面,您必须通过限制派生。因此,对于元素的子内容,必须分两步完成。该属性可以内联定义:

<xsd:simpleType name="timeValueType">
  <xsd:restriction base="xsd:token">
    <xsd:pattern value="\d{2}:\d{2}"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="timeType">
  <xsd:simpleContent>
    <xsd:extension base="timeValueType">
      <xsd:attribute name="format">
        <xsd:simpleType>
          <xsd:restriction base="xsd:token">
            <xsd:enumeration value="seconds"/>
            <xsd:enumeration value="minutes"/>
            <xsd:enumeration value="hours"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

关于xsd - 在 Xml 架构中向简单类型添加属性或向复杂类型添加限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/626319/

相关文章:

xml - 从 XSD 生成 Web 表单

java - 如何在 xsd 中查找特定标签是否在 xml 中可用?

c# - 如果 IP 在 IP 范围之间,则限制 IP

android - 纯文本输入 EditText android

c# - "The ' http ://www. w3.org/XML/1998/namespace:lang ' attribute is not declared."

xsd - 如何使用 xsd.exe 工具从 .cs 生成 .xsd?

java - JAXB 2 Maven 插件 - 强制生成 java 源代码,以使用子集合?

.net - 部署 .NET WinForms 应用程序的最恶劣的企业环境是什么?

mysql - 允许在两个 FK 列之间只分配一个值 MYSQL

c# - 构造函数限制