[xsd-users] Default values for optional elements.

Boris Kolpackov boris at codesynthesis.com
Fri Feb 3 09:46:49 EST 2006


David,

Moss, David R (SELEX Comms) (UK Christchurch) <david.r.moss at selex-comm.com> writes:

> I have a large schema that contains a lot of optional elements with
> default values specified. In code, when a query is made as to the value
> of one of these elements, I'm providing a hard-coded default (the same
> value as specified in the xsd file) for cases where the element wasn't
> present:
>
> const myData_t wrapper::getData() const
> {
> 	if( m_parent.myData().present() )
> 	{
> 		return m_parent.myData().get();
> 	}
> 	else
> 	{
> 		return someDefaultValue;
> 	}
> }
>
> This is clearly a maintenance nightmare! It also seems a bit backward as
> a default is already specified in the xsd file.

Well, that's the way XML Schema spec prescribes handling of elements with
default values. In particular, for elements (unlike attributes), there are
three cases to consider:

1. Value specified in the instance - that value is used.

2. Empty value is specified - default value is used.

3. No element specified - element not present.

The following table in the manual summarizes all the cases:

http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#A


The reason why XML Schema specifies it this way is probably because
element's name (in case of substitution groups) or the xsi:type
attribute carries type information.


> Is it possible to get the generated code to provide the default i.e. an
> optional xsd flag that meant get() would return a default value if the
> optional element wasn't present:
>
> ...
>
> Any thoughts or alternatives greatly appreciated (I don't want to use
> attributes instead of elements).

At the moment I don't see any reasons why such an option couldn't
be supported. Except that, as for attributes, when this option is
specified, optional elements with default/fixed values would be
treated as belonging to the One cardinality class because their
values will always be present (so no need to call .get ()).

I will try to implement it in the next version or two (2.0.0
or 2.0.1).


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060203/adf0c47e/attachment.pgp


More information about the xsd-users mailing list