Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xsd

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?

like image 464
lumbric Avatar asked Feb 13 '14 13:02

lumbric


1 Answers

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"/>
like image 160
jtyreman Avatar answered Nov 01 '22 15:11

jtyreman