<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>
只是指定"类别"的" name"属性为" itemCategoryEnum"不够在这里,因为这只能确保"名称"属性不能具有枚举中列出的其他值。如果从未使用过某些枚举值,它不会抱怨。这意味着以下XML也将是合规的:
<Inventory>
<Category name="Kitchen">
<Item></Item>
<Item></Item>
<Item></Item>
</Category>
</Inventory>
这里没有与"浴室"相对应的类别。我希望这个事实被抓住错误。
-Sandeep
最简单的方法是将类别名称从属性值推广到元素名称。
失败,您可以使用XSD 1.1断言来检查约束(尽管您将负责使主张与该类型的枚举值同步)。