Can an xsd:enumeration tag be made mandatory/required?

房东的猫 提交于 2019-12-12 19:41:00

问题


Short-form question: Can an xsd:enumeration tag have a required attribute, as in any tag that uses this enumeration MUST use the specific enumeration value at least once?

Details: For example, let's say that I have already defined a fruit xml tag in my xsd. The fruit tag has an attribute whose value is the enumeration FruitType. It is define as such:

<xsd:simpleType name="FruitType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Apple" />
<xsd:enumeration value="Banana" />
<xsd:enumeration value="Peach" />
<xsd:enumeration value="Orange" />
</xsd:restriction>
</xsd:simpleType>

I want to make the Apple enumeration value required such that the user must have at least one fruit tag with the attribute Apple. Is it possible to use such a tag in xsd enumeration? I thought that maybe we can put use="required" or minOccurs="1" in the xsd:enumeration tag. Please let me know.

Thank you!


回答1:


What do you mean by "the user must have at least one fruit tag with the attribute Apple". Do you mean this value must appear somewhere in each instance document if the document is to be valid? Or within some narrower scope (say within a FruitBasket element)? In both cases, that's not a property of the enumeration type, it's a constraint on the element or document in question.

I don't think there's any way of doing this in XSD 1.0. In XSD 1.1, like most things, it can be done with assertions. For example

<xs:element name="FruitBasket">
  ...
  <xs:assert test=".//Fruit = 'Apple'"/>
</xs:element>

XSD 1.1 is currently implemented in Xerces and Saxon.



来源:https://stackoverflow.com/questions/6472264/can-an-xsdenumeration-tag-be-made-mandatory-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!