You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为含子节点序列的car元素添加带枚举约束的condition属性?

修正XSD以实现car元素的condition枚举属性约束

你之前的核心问题是误用了complexType来定义属性约束——你要限制的是属性值的枚举范围,不需要复杂类型,直接定义一个simpleType就能满足需求。

修正后的完整XSD代码

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- 定义枚举简单类型,限定condition属性的可选值 -->
  <xs:simpleType name="CarCondition">
    <xs:restriction base="xs:string">
      <xs:enumeration value="new"/>
      <xs:enumeration value="used"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="car">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="brand" type="xs:string"/>
        <xs:element name="engine" type="xs:string"/>
      </xs:sequence>
      <!-- 直接引用枚举简单类型约束属性值 -->
      <xs:attribute name="condition" type="CarCondition" />
      <!-- 若要求属性必填,可添加 use="required" -->
    </xs:complexType>
  </xs:element>

  <xs:element name="test">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="car" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

关键说明

  1. 新增的CarCondition是简单类型,通过restriction限定属性值只能是new或used,完全匹配你的枚举需求
  2. car元素的condition属性直接关联这个简单类型,无需多余的复杂类型包装
  3. 你的XML实例(condition="new")完全符合这个XSD的约束

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:05:05