XML Schema:为 complexType 设置默认值? [英] XML Schema: Setting a default value for a complexType?

查看:26
本文介绍了XML Schema:为 complexType 设置默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<xs:complexType name="button"><xs:序列><xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="1"/><xs:元素名称="href" type="xs:string" minOccurs="0" maxOccurs="1"/><xs:element name="label" type="xs:string" minOccurs="0" maxOccurs="1"/></xs:序列></xs:complexType>

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

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

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

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

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

我正在尝试以尽可能简单的方式消除冗余.

解决方案

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:元素名称=按钮"类型=按钮"/><xs:complexType name="按钮"><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:模式><按钮 id="1"/>

<块引用>

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

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

http://www.w3.org/TR/xmlschema-0/#发生约束

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

<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:

<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")

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.

解决方案

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]

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

这篇关于XML Schema:为 complexType 设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆