【问题标题】:Add attributes to a simpletype or restriction to a complextype in Xml Schema将属性添加到简单类型或限制到 Xml 架构中的复杂类型
【发布时间】:2010-10-12 04:43:55
【问题描述】:

问题如下:

我有以下 XML sn-p:

<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 restriction


    【解决方案1】:

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

    <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>
    

    【讨论】:

      【解决方案2】:

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

      这是您定义继承类型的地方(在我的情况下,它是基于 xs:string 的类型,并应用了字段长度 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>
      

      这两个块可以位于架构的完全不同的位置。重要的一点只是再次关注 - 你不能在同一个元素定义中混合继承和属性。

      【讨论】:

        猜你喜欢
        • 2012-04-19
        • 1970-01-01
        • 1970-01-01
        • 2019-10-30
        • 1970-01-01
        • 2011-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多