How to define a default value for an option element using XSD?

橙三吉。 提交于 2020-02-20 09:00:25

问题


What I like to do: I want to specify an option tag in the schema, for example:

<xsd:element name="my_element" type="my_type" minOccurs="0" maxOccurs="1"/>

If the element does not occur at all, there should be a default value for this parameter. Of course I could define this default value in my code, which calls the XML parser. But I think the correct place to specify the default value would be in the *.xsd schema file (since the default value is part of the interface defined by the schema).

Unfortunately this does not seem to be easy. The "default" attribute has a different effect: "if it does not appear it is not provided; if it does appear and it is empty, its value is the default value" (from http://www.w3.org/TR/xmlschema-0/#ref36).

Other links I've found discussing this issue:

  • http://codesynthesis.com/pipermail/xsd-users/2006-February/000209.html
  • http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#A

Is there a solution to this problem? Or should I give up?


回答1:


simple elements may have a default value OR a fixed value specified.

A default value is automatically assigned to the element when no other value is specified.

In the following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element, and you cannot specify another value.

In the following example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>


来源:https://stackoverflow.com/questions/21755856/how-to-define-a-default-value-for-an-option-element-using-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!