是否可以定义具有一系列节点的XML模式,每个枚举值一个?

时间:2012-10-25 09:12:45

标签: xsd

<xs:simpleType name="ItemCategoryEnum">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Kitchen"></xs:enumeration>
    <xs:enumeration value="Bathroom"></xs:enumeration>
  </xs:restriction>
</xs:simpleType>

是否可以定义一个应该具有尽可能多的元素“库存” 节点,名为“类别”,因为“ItemCategoryEnum”有可能的枚举值?

因此,通过上面的示例,兼容的XML应该如下所示:

<Inventory>
  <Category name="Kitchen">
    <Item></Item>
    <Item></Item>
    <Item></Item>
  </Category>
  <Category name="Bathroom">
    <Item></Item>
    <Item></Item>
    <Item></Item>
  </Category>
</Inventory> 

仅指定类型为“ItemCategoryEnum”的“类别”的“名称”属性是不够的 这里因为只能确保“name”属性不能具有枚举中列出的任何其他值。如果从未使用某些枚举值,它不会抱怨。这意味着以下XML也将符合:

<Inventory>
  <Category name="Kitchen">
    <Item></Item>
    <Item></Item>
    <Item></Item>
  </Category>
</Inventory>

此处没有与“浴室”对应的类别。我希望这个事实被抓住 错误。

-Sandeep

1 个答案:

答案 0 :(得分:1)

最简单的方法是将类别名称从属性值提升为元素名称。

如果不这样做,您可以使用XSD 1.1断言来检查约束(尽管您将负责保持断言与类型的枚举值保持同步)。