XSD:缺失元素的默认值 [英] XSD: default value of a missing element

查看:33
本文介绍了XSD:缺失元素的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 XML 架构中的缺失元素定义默认值.我有以下片段:

Is it possible to define a default value for a missing element in an XML Schema. I have the following snippet:

<xs:element name="protocol" nillable="false" minOccurs="0" default="ftp">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="ftp"/>
      <xs:enumeration value="webdav"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

如果我在 XML 文件中有

If I have in the XML file

<protocol>ftp</protocol>

<protocol>webdav</protocol>

它经过验证,我获得了正确的值.如果我在 XML 文件中有

it validates and I obtain the right value. If I have in the XML file

<protocol></protocol>

它也进行了验证,我获得了 ftp 的默认值.

it also validates and I obtain the default value of ftp.

我的搜索表明,当属性缺失时应用默认属性值,当元素为空时应用默认元素值.是否可以为缺失的元素设置默认值?

My searches show that default attribute values apply when attributes are missing, and default element values apply when elements are empty. Is it possible to have a default value for a missing element?

问候

兰比斯

推荐答案

没有.XSD 不提供此功能.

No. XSD doesn't provide for that.

您可以指定元素的默认值.但是一旦它丢失(当其父的内容模型允许时),对该元素的任何请求都将返回空字符串或 null(或只是一个错误).缺少的元素是不存在的元素!

You can specify the default value of an element. But once it is missing (when that's allowed by the content model of its parent), any requests to that element will return either empty string or null (or just an error). The missing element is non-existent element!

对于属性是可能的,因为属性要简单得多.元素的所有属性有效地构成了一组无序的命名简单值.没有某种属性树(带有可变结构)附加到父元素.

For attributes it is possible, because attributes are far simpler. All attributes of an element effectively constitute an unordered set of named simple values. There is no some kind of attribute tree (with the variable structure at that) attached to the parent element.

但是对于元素,事情要复杂得多.如果允许关于缺失元素的默认",那将导致很多歧义.例如,然后必须指定某种默认内容",这将是一些自动唤起代替空虚的元素序列......甚至许多可能的默认内容",当只有一些元素被明确指定而其他元素必须隐式补充它们时(默认情况下).....好吧,如果你进一步思考,事情变得令人难以置信的复杂.那时必须创建另一种语言!但是为了什么目的?

But with elements things are far more complex. If something "default" about missing elements were allowed, that would cause lots of ambiguities. For instance, some kind of "default content" would have to be specified then, which would be some sequence of elements evoked automatically in place of emptiness... or even a number of possible "default contents" each evoked when only some elements are specified explicitly and others must supplement them implicitly (by default). .... Well, if you think further on, the things are getting mind-boggling complex. Another language would have to be created then! But for what purpose?

这篇关于XSD:缺失元素的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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