[xsd-users] Default values for optional elements.

Moss, David R (SELEX Comms) (UK Christchurch) david.r.moss at selex-comm.com
Fri Feb 3 06:26:54 EST 2006


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. 

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:

const myData_t& wrapper::getData() const
{
	// Code generated with '--defaultElement' flag so no need for
	// present()check.
	return m_parent.myData().get();
}

This also avoids the need to copy the return value.

Any thoughts or alternatives greatly appreciated (I don't want to use
attributes instead of elements).

Maybe this is already possible and I'm missing something?!

Cheers,
Dave.

Dave Moss
SELEX Communications
Grange Road
Christchurch 
Dorset  BH23 4JE
United Kingdom
Tel: + 44 (0) 1202 404841
Email: david.r.moss at seleniacomm.com






More information about the xsd-users mailing list