首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Xml架构中向简单类型添加属性或向复杂类型添加限制

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

Stack Overflow用户
提问于 2009-03-09 13:52:37
回答 2查看 59.5K关注 0票数 67

问题如下:

我有以下XML片段:

代码语言:javascript
复制
<time format="minutes">11:60</time>

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

代码语言:javascript
复制
<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,我可以添加一个属性,但不能添加限制;如果我创建一个简单类型,我可以添加限制,但不能添加属性。有没有办法绕过这个问题。这不是一个非常奇怪的限制,或者说是吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-03-09 14:11:27

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

代码语言:javascript
复制
<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>
票数 123
EN

Stack Overflow用户

发布于 2017-04-28 17:33:29

我想提出我的例子,更详细地解释在添加属性时混合使用继承类型和限制的实际要求。

这是定义继承类型的地方(在我的例子中,它是基于xs:string的类型,应用了字段长度为1024的限制)。你不能把它作为你的字段的标准类型,因为你也要给你的字段添加一个“属性”。

代码语言:javascript
复制
  <xs:simpleType name="string1024Type">
    <xs:restriction base="xs:string">
      <xs:maxLength value="1024"/>
    </xs:restriction>
  </xs:simpleType>

这是根据私有类型(在我的例子中是"string1024Type")和附加的必要属性来定义元素的地方:

代码语言:javascript
复制
<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>

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/626319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档