问题
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