问题如下:

我有以下XML代码段:

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

问题是我无法同时添加属性和限制。属性格式只能包含分钟,小时和秒。时间有restrictionpattern \ 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>

其他提示

我想提出我的示例,更详细地解释在添加属性时将继承类型与限制混合的实际需要。

这是您定义继承类型的位置(在我的情况下,它是xs:基于字符串的字段,应用了字段长度1024限制)。你不能将它作为你的领域的标准类型,因为你要添加一个“属性”。也是你的领域。

  <xs:simpleType name="string1024Type">
    <xs:restriction base="xs:string">
      <xs:maxLength value="1024"/>
    </xs:restriction>
  </xs:simpleType>

这是根据您的私有类型(在我的情况下是“string1024Type”)定义元素的位置,并附加必要的属性:

<xs:element maxOccurs="unbounded" name="event">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="string1024Type">
        <xs:attribute default="list" name="node" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

这两个块可能位于架构的完全独立的位置。重要的一点是再次遵循 - 你不能混合继承和归因于相同的元素定义。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top