我遇到了一个遗留的 XSD,它有一堆像......
<xs:element minOccurs="1"
maxOccurs="1"
default="true"
name="Ready" type="xs:boolean" />
...其中 minOccurs 和 maxOccurrs 都等于 1,并且有一个默认值。由于 XML 文档无论如何都需要包含一些值,因此在此处使用默认属性有什么好处吗?
最佳答案
来自spec :
The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.
我认为这意味着一个空的 <Ready />
根据您的架构,节点将默认为 TRUE 值。
关于c# - 当 minOccurs 属性 > 1 时使用默认属性有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1750476/