XML 架构:为 complexType 设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1060190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:34:50  来源:igfitidea点击:

XML Schema: Setting a default value for a complexType?

xmlschemaxsd

提问by Adam Plumb

Let's say I want to set up a generic complexType like so:

假设我想像这样设置一个通用的 complexType:

<xs:complexType name="button">
    <xs:sequence>
        <xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="1"/>
        <xs:element name="href" type="xs:string" minOccurs="0" maxOccurs="1"/>
        <xs:element name="label" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

And I want to reference that complexType in various places in my schema file like so:

我想在我的架构文件中的不同位置引用那个 complexType ,如下所示:

<xs:element name="someButton" type="button" />

Is it possible for me to set default values for the button sub-elements through the someButton element? (I.e. if I want someButton to have a default label of "Go" or a default href of "index.html")

我可以通过 someButton 元素为按钮子元素设置默认值吗?(即,如果我希望 someButton 具有“Go”的默认标签或“index.html”的默认 href)

Basically... right now I have something like

基本上......现在我有类似的东西

<Field Name="State" DataSourceField="State" />

and I'm trying to remove the redundancy in as simple a manner as possible.

我试图以尽可能简单的方式消除冗余。

回答by 13ren

No, only for simple values. But maybe you can use them to do what you want, by giving default values for all the simple parts of your complex Type. However, it works better for attributes than for the elements you have (because "Default attribute values apply when attributes are missing, and default element values apply when elements are empty" - see below). Attributes are themselves optional by default:

不,仅适用于简单值。但是也许您可以使用它们来做您想做的事,通过为复杂类型的所有简单部分提供默认值。但是,它对属性比对您拥有的元素更有效(因为“属性缺失时应用默认属性值,元素为空时应用默认元素值” - 见下文)。默认情况下,属性本身是可选的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="button" type="button"/>
  <xs:complexType name="button">
    <xs:attribute name="id" type="xs:string"/>
    <xs:attribute name="href" type="xs:string" default="index.html"/>
    <xs:attribute name="label" type="xs:string" default="Go"/>
  </xs:complexType>
</xs:schema>

<button id="1"/>

Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.

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.[emphasis added]

属性和元素的默认值都是使用 default 属性声明的,尽管这个属性在每种情况下都有略微不同的结果。当使用默认值声明属性时,该属性的值是在实例文档中作为该属性值出现的任何值;如果该属性未出现在实例文档中,则模式处理器为该属性提供一个与默认属性相同的值。请注意,属性的默认值仅在属性本身是可选的情况下才有意义,因此同时指定默认值和除可选值以外的任何值都是错误的。

模式处理器对默认元素的处理略有不同。当使用默认值声明元素时,元素的值是实例文档中作为元素内容出现的任何值;如果元素出现时没有任何内容,模式处理器会为元素提供一个与默认属性相同的值。但是,如果该元素未出现在实例文档中,则模式处理器根本不提供该元素。总之,元素和属性默认值之间的区别可以表述为:当属性缺失时应用默认属性值,当元素为空时应用默认元素值。[强调]

http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints

http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints